gpt4 book ai didi

java套接字编程问题

转载 作者:行者123 更新时间:2023-12-02 00:49:13 25 4
gpt4 key购买 nike

客户端:

  import java.io.*;
import java.net.*;
public class Requester
{
Socket requestSocket;
ObjectOutputStream out;
ObjectInputStream in;
String message;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Requester(){}
void run() throws IOException
{
try{
//1. creating a socket to connect to the server
requestSocket = new Socket("172.16.3.219", 2004);
System.out.println("Connected to localhost in port 2004");

//2. get Input and Output streams
out = new ObjectOutputStream(requestSocket.getOutputStream());
out.flush();
in = new ObjectInputStream(requestSocket.getInputStream());

//3: Communicating with the server
do{
try{
message = (String)in.readObject();
System.out.println("server>" + message);
message=br.readLine();
sendMessage(message);
}
catch(ClassNotFoundException classNot)
{
System.err.println("data received in unknown format");
}
}while(!message.equals("bye"));
}
catch(UnknownHostException unknownHost)
{
System.err.println("You are trying to connect to an unknown host!");
}
catch(IOException ioException)
{
ioException.printStackTrace();
}
finally
{
//4: Closing connection
try{
in.close();
out.close();
requestSocket.close();
}
catch(IOException ioException)
{
ioException.printStackTrace();
}
}
}
void sendMessage(String msg)
{
try{
out.writeObject(msg);
out.flush();
System.out.println("client1>" + msg);
}
catch(IOException ioException)
{
ioException.printStackTrace();
}
}
public static void main(String args[]) throws IOException
{
Requester client = new Requester();
client.run();
}
}

服务器:

import java.io.*;
import java.net.*;
public class Provider
{
ServerSocket providerSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Provider(){}
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

void run()
{
try{
//1. creating a server socket
providerSocket = new ServerSocket(2004, 10);

//2. Wait for connection
System.out.println("Waiting for connection");

connection = providerSocket.accept();
System.out.println("Connection received from" + connection.getInetAddress().getHostName());

//3. get Input and Output streams
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());
sendMessage("Connection successful");

//4. The two parts communicate via the input and output streams
do
{
try
{
message = (String)in.readObject();
System.out.println("client>" + message);

if (message.equals("bye"))
{
sendMessage("bye");
}
else
{
message=br.readLine();
sendMessage(message);
}
}
catch(ClassNotFoundException classnot)
{
System.err.println("Data received in unknown format");
}
}while(!message.equals("bye"));
}
catch(IOException ioException)
{
ioException.printStackTrace();
}
finally
{
//4: Closing connection
try{
in.close();
out.close();
providerSocket.close();
}
catch(IOException ioException)
{
ioException.printStackTrace();
}
}
}

void sendMessage(String msg)
{
try
{
out.writeObject(msg);
out.flush();
System.out.println("server>" + msg);
}
catch(IOException ioException)
{
ioException.printStackTrace();
}
}
public static void main(String args[])throws IOException
{
Provider server = new Provider();
while(true)
{
server.run();
}
}
}

服务器程序位于172.16.3.219。如果服务器和客户端都在一个系统(172.16.3.219)中,程序正在工作。如果将我的客户端程序放在其他系统中,让我们说172.16.3.30,我会收到异常(服务器仍然位于172.16.3.219)。我应该更改 socket() 构造函数吗?如果问题出在构造函数上,当客户端和服务器位于不同系统时,建议我使用合适的构造函数

最佳答案

代码看起来不错;您的客户端或服务器上有防火墙吗?

我试图测试发送邮件的代码。为此,我安装了本地邮件服务器,但无法连接:管理员已禁用 SMTP 端口以阻止垃圾邮件发送者。

[编辑]“连接被拒绝”意味着没有服务器在该服务器上的该端口上运行。运行 netstat -tan (Linux) 或 netstat -tn (Windows) 并查找带有 LISTEN 的行。这些是正在运行的服务器进程。输出将如下所示:

 Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN

有趣的部分是“本地地址”。它包含服务器正在监听的网络接口(interface)(IP 地址)。第一个条目监听所有网络接口(interface),而第二个条目附加到“localhost”(也称为“本地环回”)。它将忽略来自 LAN 的任何连接尝试。

确保您的服务器以 0.0.0.0:2004 列出。如果不是,请尝试:

 providerSocket = ServerSocket(2004, 10, InetAddress.getByName("0.0.0.0"));

如果不起作用,请尝试服务器网卡的公共(public)IP地址。

关于java套接字编程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3923093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com