有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Javasocket在windows上不起作用

我想制作一个程序,在同一个wifi中连接两台设备,所以我尝试使用插座

我在我的pc上运行服务器代码,在安卓设备上运行客户端代码。 问题是服务器在windows上不工作,但在linux上工作。 我断开了所有防火墙、windows和avast的连接,但我仍然有同样的问题。 我曾尝试使用linux机器作为服务器,windows作为客户端,它工作得非常好

当我尝试获取socket“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);
}

共 (1) 个答案

  1. # 1 楼答案

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

    编辑:

    在windows上启动服务器并打开cmd。exe:

    netstat -an|findstr "2002"
    

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

    telnet 192.168.1.131 2002
    

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