这是this question的延续,因为我的原始问题得到了回答,但它并没有解决这个问题。
问题:
我的意图:
问题:
现在发生了什么:
其他信息: - 一旦连接,我可以输入“发送”框,提交(更新outMessage值),然后断开连接。重新连接后,它将读取该值并再次执行该序列,直到它卡在同一行上。
引用问题后的变化: - make outMessage和connectionStatus都'volatile' - 在必要的地方增加了行尾分隔符。
代码:
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");
}
}
提前致谢!
编辑:
答案 0 :(得分:2)
您的服务器专门设计为从客户端只接收一行并且只发回一行。看一下代码:
while (true) {
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(
new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(
connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
String ip = connectionSocket.getInetAddress().toString()
.substring(1);
System.out.println("In from client (" + ip + "): "
+ clientSentence);
if (clientSentence != null) {
capitalizedSentence = clientSentence.toUpperCase() + '\n';
System.out.println("Out to client (" + ip + "): "
+ capitalizedSentence);
outToClient.writeBytes(capitalizedSentence + "\n");
}
请注意,在循环内部,它接受一个新连接,只读取一行,然后只写一行。它不会关闭连接。它并没有理智地结束谈话。它只是停止阅读。
使用此服务器的客户端必须连接,只发送一行,只读回一行,然后客户端必须关闭连接。您的客户不这样做。为什么?因为你不知道你必须做什么。为什么?因为你没有设计......没有计划。
这就是你的具体问题。但是,请允许我强烈要求你采取巨大的退一步,彻底改变你的做法。在编写单行代码之前,请实际设计并在字节级别指定协议。协议应该说明发送了什么数据,如何分隔消息,谁发送数据,谁关闭连接等等。
否则,无法调试代码。查看上面的服务器代码,是否正确?嗯,谁知道呢。因为不清楚它应该做什么。编写客户端时,您认为服务器的行为方式不同。这个假设有效吗?服务器坏了吗?谁知道,因为没有规定服务器应该做什么。
答案 1 :(得分:1)
您需要检查是否有可用的数据:
if (InStream.available > 0) {
String modifiedSentence = InStream.readLine();
sendMessageToAllUI(0, MAINACTIVITY_SET_TEXT_STATE, "appendText" , "IN FROM SERVER: " + modifiedSentence);
}
但说实话,即使这样也不理想,因为你没有保证会收到线上的信息。如果服务器发送几个字节但从不发送行尾,那么你仍将永远阻止。生产套接字代码不应该依赖于readLine
,而是读入缓冲区并检查该缓冲区是否为行尾(或协议所需的任何条件)。
没有仔细阅读,我认为InStream
是InputStream
个实例。 InputStream
有available
。 InputStreamReader
有ready
(后者会调用InputStream.available
。只要您参考其中任何一个,就可以看到数据是否可供阅读。