这是这个问题的延续,因为它回答了我的原始问题,但它没有解决错误。
问题:
- 如何修复挂在这条线上的代码 inStream.readline()
我的意图:
- 这是一个线程,将循环检查是否有 outMessage,如果有,它将发送消息。
- 接下来,它将检查流内是否有任何内容,如果有,它会将其发送到我的主要活动中的处理程序。
- 最后,它会休眠 1 秒钟,然后再次检查。
- 这应该允许我多次读/写,而无需关闭和打开套接字。
问题:
- 它的阅读和写作更好,但仍然无法正常工作
现在发生了什么:
- 如果 outMessage 用一个值初始化,则在与服务器连接时,套接字:
- 写入并刷新值(服务器接收和响应)
- 更新 outMessage 的值(根据我的硬编码方式为 null 或“x”)
- 读取并显示来自服务器的响应消息
- 重新进入下一个循环
- 如果我将 outMessage 设置为 null,它会跳过 if 语句正确然后挂起;否则,如果我将 outMessage 设置为一个字符串(比如说“x”),它会遍历整个 if 语句,然后挂起。
- 它挂起的代码是inStream.readline()调用之一(我目前有一个注释掉)。
附加信息: - 连接后,我可以在“发送”框中输入,提交(更新 outMessage 值),然后断开连接。重新连接后,它将读取该值并再次执行序列,直到它卡在同一条线上。
自引用问题以来的更改: - 将 outMessage 和 connectionStatus 都设为“易失性” - 在必要的地方添加了行尾分隔符。
代码:
public void run() {
while (connectionStatus != TCP_SOCKET_STATUS_CONNECTED) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
while (connectionStatus == TCP_SOCKET_STATUS_CONNECTED) {
try {
if (outMessage != null){
OutStream.writeBytes(outMessage + "\n");
OutStream.flush();
sendMessageToAllUI(0, MAINACTIVITY_SET_TEXT_STATE, "appendText" , "OUT TO SERVER: " + outMessage);
outMessage = "x";
}
Thread.sleep(100);
// if (InStream.readLine().length() > 0) {
String modifiedSentence = InStream.readLine();
sendMessageToAllUI(0, MAINACTIVITY_SET_TEXT_STATE, "appendText" , "IN FROM SERVER: " + modifiedSentence);
// }
Thread.sleep(1000);
} catch (IOException e) {
connectionLost();
break;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
制作套接字的线程:
public void run() {
setName("AttemptConnectionThread");
connectionStatus = TCP_SOCKET_STATUS_CONNECTING;
try {
SocketAddress sockaddr = new InetSocketAddress(serverIP, port);
tempSocketClient = new Socket(); // Create an unbound socket
// This method will block no more than timeoutMs. If the timeout occurs, SocketTimeoutException is thrown.
tempSocketClient.connect(sockaddr, timeoutMs);
OutStream = new DataOutputStream(tempSocketClient.getOutputStream());
InStream = new BufferedReader(new InputStreamReader(tempSocketClient.getInputStream()));
socketClient = tempSocketClient;
socketClient.setTcpNoDelay(true);
connected();
} catch (UnknownHostException e) {
connectionFailed();
} catch (SocketTimeoutException e) {
connectionFailed();
} catch (IOException e) {
// Close the socket
try {
tempSocketClient.close();
} catch (IOException e2) {
}
connectionFailed();
return;
}
}
服务器:
public static void main(String[] args) throws IOException {
String clientSentence;
String capitalizedSentence;
try {
ServerSocket welcomeSocket = new ServerSocket(8888);
SERVERIP = getLocalIpAddress();
System.out.println("Connected and waiting for client input!\n Listening on IP: " + SERVERIP +"\n\n");
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
while(true)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
clientSentence = inFromClient.readLine();
System.out.println("clientSentance == " + clientSentence);
String ip = connectionSocket.getInetAddress().toString().substring(1);
if(clientSentence != null)
{
System.out.println("In from client ("+ip+")("+ System.currentTimeMillis() +"): "+clientSentence);
capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence + '\n');
System.out.println("Out to client ("+ip+"): "+capitalizedSentence);
}
}
} catch (IOException e) {
//if server is already running, it will not open new port but instead re-print the open ports information
SERVERIP = getLocalIpAddress();
System.out.println("Connected and waiting for client input!\n");
System.out.println("Listening on IP: " + SERVERIP +"\n\n");
}
}
提前致谢!
编辑:
- 更新后添加服务器代码
- 我尝试为套接字设置 SoTimout,但把它拿回来了