Java 为什么我不能通过WiFi连接两台不同的笔记本电脑?
这是一个简单的客户机-服务器程序。当在一台计算机上执行时,它可以正常工作。但是,在使用WiFi连接的两台不同笔记本电脑上执行时,它不起作用。 代码如下: 服务器:Java 为什么我不能通过WiFi连接两台不同的笔记本电脑?,java,networking,Java,Networking,这是一个简单的客户机-服务器程序。当在一台计算机上执行时,它可以正常工作。但是,在使用WiFi连接的两台不同笔记本电脑上执行时,它不起作用。 代码如下: 服务器: public class Server { private static int port; private ServerSocket serverSocket; public void GreetingServer(int port) throws IOException { serverSoc
public class Server
{
private static int port;
private ServerSocket serverSocket;
public void GreetingServer(int port) throws IOException
{
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(20000);
while(true)
{
try
{
System.out.println("Waiting for client");
Socket server = serverSocket.accept();
System.out.println("Connected");
DataInputStream in =new DataInputStream(server.getInputStream());
System.out.println(in.readUTF());
DataOutputStream out = new DataOutputStream(server.getOutputStream());
out.writeUTF("Thank you for connecting to ");
server.close();
}
catch(SocketTimeoutException s)
{
System.out.println("Socket timed out!");
break;
}
catch(IOException e)
{
e.printStackTrace();
break;
}
}
}
public static void main(String [] args)
{
port=9001;
try
{
Server s=new Server();
s.GreetingServer(port);
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
客户:
public class Client
{
private static String serverName;
public static void main(String [] args)
{
String sName = "192.168.xxxx.x";
int port = 9001;
try
{
System.out.println("Connecting to " + sName
+ " on port " + port);
Socket client = new Socket(sName, port);
System.out.println("Connected");
OutputStream outToServer = client.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
out.writeUTF("Hello from " + client.getLocalSocketAddress());
InputStream inFromServer = client.getInputStream();
DataInputStream in = new DataInputStream(inFromServer);
System.out.println("Server says " + in.readUTF());
client.close();
}catch(IOException e)
{
}
}
}
有什么问题吗?请告诉我。。
提前感谢。当您运行它并通过WiFi连接时会发生什么情况?但是,在使用WiFi连接的两台不同笔记本电脑上执行时,它不工作。当使用有线连接时,它工作吗?如果不是,可能不是网络问题。运行时会出现什么错误?它无法连接。。服务器将超时。您可以在WiFi上ping其他计算机吗?