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

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?

I provide my code below

Server Code:

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();

}

Client Side:

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: 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)

}

解决方案

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).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值