连接到服务器时客户端套接字超时 [英] Client socket times out when connecting to Server

查看:156
本文介绍了连接到服务器时客户端套接字超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem when I try to connect my physical device to my server using sockets. On the server side it does not seem to accept any connection while on the client side the socket times out. Any ideas why this is happening?

我在下面提供我的代码

服务器代码:

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):位于libcore.io.IoBridge.connect(IoBridge.java:114) 11-16 23:32:11.016:W/System.err(24213):在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) 11-16 23:32:11.026:W/System.err(24213):在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459) 11-16 23:32:11.026:W/System.err(24213):位于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(连接超时) }

11-16 23:32:11.016: W/System.err(24213): java.net.ConnectException: failed to connect to /192.168.1.116 (port 9090): connect failed: ETIMEDOUT (Connection timed out) 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): Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out) }

推荐答案

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

I can't be sure (because it does not appear in your code) but I think the server is not hosing on the same port (9090). That may be the main problem, but the server or client may also be blocked by a firewall (even if they run on the same machine).

这篇关于连接到服务器时客户端套接字超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆