0

我正在尝试制作一个程序,我想在同一个wifi中连接两个设备,所以我正在尝试使用套接字。

我在我的电脑上运行服务器代码,在安卓设备上运行客户端。问题是服务器不能在 Windows 上运行,但它在 linux 上运行。我断开了所有的防火墙、windows 和 avast,但我仍然遇到同样的问题。我尝试使用 linux 机器作为服务器,windows 作为客户端,它运行良好。

当我尝试获取套接字“Socket s = ss.accept();”时,我打印了一些打印件以查看它在哪里停止并且 Windows 中的服务器端停止。我没有收到任何错误,它只是卡在那里。

我不知道有什么问题。

服务器端

   int port = 2002;  
   try {  
            ServerSocket ss = new ServerSocket(port);

            Socket s = ss.accept();

            InputStream is = s.getInputStream();  
            ObjectInputStream ois = new ObjectInputStream(is); 

            System.out.println((String)ois.readObject());  
            is.close();  
            s.close();  
            ss.close();  
   }catch(Exception e){System.out.println(e);}  

客户端:

try{  
            String hostPortatil = "192.168.1.131";
            String host = "192.168.174.1";
            int port = 2002;
            Socket s = new Socket(hostPortatil, port);  

            OutputStream os = s.getOutputStream();

            ObjectOutputStream oos = new ObjectOutputStream(os);  

            oos.writeObject(new String("another object from the client"));  

            oos.close();  
            os.close();  
            s.close();  
}catch(Exception e){
            System.out.println(e);
}
4

4 回答 4

0

您的服务器配置为侦听端口 9999 上的连接,但客户端使用端口 2002 进行连接

编辑:

在 windows 上启动服务器并打开一个 cmd.exe:

netstat -an|findstr "2002"

您应该看到您的 java 进程正在侦听连接。如果不是,那么就有问题了。在 linux 服务器中打开一个 shell:

telnet 192.168.1.131 2002

您应该从 linux 服务器连接到 windows 服务器。再次,如果不是有些是错误的。

于 2013-09-05T15:05:45.140 回答
0

很可能您尚未将 Windows 防火墙配置为接受端口 2002 上的连接。

于 2013-09-05T15:03:58.173 回答
0

我认为这是因为您在 Windows 上运行程序的服务器端,这会将 localhost ip 分配给您的服务器端,并且该 ip 肯定与您本地 Wi-Fi 和热点连接的 ip 不同。我使用 localhost 在 Windows 上运行示例应用程序的客户端和服务器端,并且工作但没有在两个不同平台上使用我的 Wi-Fi 和热点连接的 ip。我想它可以通过将计算机变成服务器来解决!

于 2020-06-19T14:32:15.487 回答
0

accept() 方法正在等待客户端建立 TCP 连接,这就是为什么没有输出也没有错误的原因,所以问题似乎出在客户端。

于 2020-09-26T19:27:38.417 回答