首页 文章

连接到服务器时,客户端套接字超时

提问于
浏览
1

当我尝试使用套接字将物理设备连接到服务器时出现问题 . 在服务器端,它似乎不接受任何连接,而在客户端,套接字超时 . 任何想法为什么会这样?

我在下面提供我的代码

服务器代码:

public void run()
    {
        // TODO Auto-generated method stub
        try{
        gamePending = false;
        pid = 0;
        while(pid < 2){
            System.out.println("Hello from run loop on game");
            Socket tempSocket = server.accept();
            System.out.println("Client connected at " + tempSocket.getLocalPort());
            PrintWriter tempWriter = new PrintWriter(new BufferedWriter (new OutputStreamWriter(tempSocket.getOutputStream())),true);
            tempWriter.println("" + pid);

            players[pid] = new Client(tempSocket, pid, this);
            players[pid].start();
            gamePending = true;
            if(pid == 0){sendMsg(pid, "waiting for other player");}
            pid++;
        }
        }
        catch(Exception e){
            System.out.println("There has been an Error. Game will be Terminated.");
        }
        //Start new game for the next two players...
        new Game();
    }

客户端:

public void run() {
    // Connects to the Server....
    try {
        socket = new Socket("192.168.1.116", 9090);
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        in = new BufferedReader (new InputStreamReader(socket.getInputStream()));
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        out = new PrintWriter(socket.getOutputStream(),true);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

11-16 23:32:11.016:W / System.err(24213):java.net.ConnectException:无法连接到/192.168.1.116(端口9090):连接失败:ETIMEDOUT(连接超时)11-16 23 :32:11.016:W / System.err(24213):at libcore.io.IoBridge.connect(IoBridge.java:114)11-16 23:32:11.016:W / System.err(24213):at java . net.PlainSocketImpl.connect(PlainSocketImpl.java:192)11-16 23:32:11.026:W / System.err(24213):at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)11-16 23: 32:11.026:W / System.err(24213):at java.net.Socket.connect(Socket.java:842)11-16 23:32:11.026:W / System.err(24213):at vatos.locos .spheroknockout.Connection.run(Connection.java:22)11-16 23:32:11.026:W / System.err(24213):at java.lang.Thread.run(Thread.java:841)11-16 23 :32:11.026:W / System.err(24213):引起:libcore.io.ErrnoException:连接失败:ETIMEDOUT(连接超时)}

1 回答

  • 1

    我不能确定(因为它没有出现在你的代码中),但我认为服务器没有在同一个端口(9090)上进行冲洗 . 这可能是主要问题,但服务器或客户端也可能被防火墙阻止(即使它们在同一台机器上运行) .

相关问题