2017-05-10 181 views
0

我试图使多个设备之一作为服务器或组所有者和其他客户端,我已经实现使用WiFi直接和WiFi p2p和工作正常进行连接。套接字无法连接,而使用WiFi直接连接

设备连接在一个组后,我试图使服务器和多个客户端之间的套接字连接,但我不能使用套接字连接。表示

java.net.ConnectException以下错误:未能连接到5000毫秒后/192.168.49.1(端口8988):isConnected失败:ECONNREFUSED(连接被拒绝)

SockertServer代码

@Override 
    protected Object doInBackground(Object[] params) { 
try { 
    server = new ServerSocket(8988); 
    Log.d("ServerActivity", "Server: Socket opened"); 

    Log.d("ServerActivity", server.getLocalPort() + ""); 

    Log.d("ServerActivity", server.getInetAddress() + ""); 


    Socket client = server.accept(); 
    Log.d("ServerActivity", "Server: connection done"); 

    ObjectOutputStream objectOutputStream = new ObjectOutputStream(
      client.getOutputStream() 
    ); 

    objectOutputStream.writeObject("Hie"); 

    client.close(); 
    server.close(); 

} catch (IOException e) { 
    e.printStackTrace(); 
} 


return null; 
} 

客户端的Socket

@Override 
      protected Object doInBackground(Object[] params) { 
try { 
    mSocket = new Socket(); 

    mSocket.bind(null); 
    mSocket.connect((new InetSocketAddress(getAddr, portNo)), SOCKET_TIMEOUT); 

    if (mSocket.isConnected()) { 
     Log.d("Client Activity", "Socket Connected Successfully"); 
    } else { 
     Log.d("Client Activity", "Socket not Connected "); 
    } 

    ObjectInputStream objectOutputStream = new 
      ObjectInputStream(mSocket.getInputStream()); 

    msg = (String) objectOutputStream.readObject(); 

    message.onMessageSend(msg); 
    Log.e(".......................", "Message" + msg); 

    objectOutputStream.close(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} catch (ClassNotFoundException e) { 
    e.printStackTrace(); 
} finally { 
    if (mSocket != null) { 
     if (mSocket.isConnected()) { 
      try { 
       mSocket.close(); 
      } catch (IOException e) { 
       // Give up 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

return msg; 
} 

所以任何人都可以,他lp我与这个问题。提前感谢!

+0

您不必调用'mSocket.bind(空)'。扔掉它,并尝试连接,你确定服务器仍在监听?一个日志可能会很高兴看到进一步的诊断 – Nico

回答

0

ECONNREFUSED表示尝试连接并且远程主机端口未侦听。因此,这可能是由于:

这是一个有效的IP吗?使用ifconfig或ipconfig进行检查。你可以尝试ping服务器。

它也可能是由于以下原因:

  • -The服务器无法发送响应:确保后端在IP正常工作和端口提及。

  • -SSL连接正在被阻止。

  • - 请求超时:变更请求超时

+0

我使用NSD所以它给m同行信息,即IP和端口号 –