我正在开发基于WiFi Direct的应用程序来连接两个设备。
我按照Android文档中的演示,已经在StackOverflow上阅读了一些类似问题的答案。
我的工作流程如下,以便在主机I上获取客户端IP:
ServerSocket
,在预先确定的端口上从客户端接收字符串(客户端IP); 这样的事情:
@Override
public void onConnectionInfoAvailable(final WifiP2pInfo info) {
this.getView().setVisibility(View.VISIBLE);
// After the group negotiation, we assign the group owner as the file
// server. The file server is single threaded, single connection server
// socket.
if (info.groupFormed && info.isGroupOwner) {
isGroupOwner = true;
// ServerSocket that receives the message from the client
new MessageReceiveSocket(mContentView.findViewById(R.id.btn_start_client), this).execute();
new FileReceiveSocket(getActivity()).execute();
} else if (info.groupFormed) {
mPeerIP = info.groupOwnerAddress.getHostAddress();
// The other device acts as the client. In this case, we enable the
// get file button.
mContentView.findViewById(R.id.btn_start_client).setVisibility(View.VISIBLE);
new FileReceiveSocket(getActivity()).execute();
// Socket that sends the message to the owner
SocketUtils.sendMessageToServer(info.groupOwnerAddress.getHostAddress(), SocketUtils.SERVER_MESSAGE_PORT);
}
// hide the connect button
mContentView.findViewById(R.id.btn_connect).setVisibility(View.GONE);
}
我的问题是当我断开两台设备并再次连接两台设备时,我在客户端收到此错误:
java.net.ConnectException: failed to connect to /192.168.49.1 (port 8989) after 5000ms: isConnected failed: ECONNREFUSED (Connection refused)
问题必须是ServerSocket
没有被终止,但我不明白为什么或如何纠正它。