我一直在尝试各种实现方式使其正常工作,并在StackOverflow和Android Developers上搜索解决方案,但由于我不太熟练编程,无法让这个代码块正常工作。
我的意图:
- 这是一个线程,将循环检查是否有outMessage,如果有,它将发送该消息。
- 接下来它将检查in-stream中是否有任何内容,如果有,它将将其发送到我的主要活动处理程序中。
- 最后,它将睡眠1秒,然后再次检查。
- 这应该允许我多次读写而无需关闭和打开套接字。
问题:
- outstream直到我关闭套接字才会刷新。 flush()似乎没有效果。
我的请求:
- 请发布所需更改以使此代码按照上述描述正常工作(任何解释为什么需要更改的注释都将不胜感激。链接到其他类似的问题/答案将有助于我学习,但我已经看了几周了,就是不能让它正常工作,所以请确保还包括此代码所需的更改。提前致谢。
其他:
- 我想知道我的instream和/或outstream是否需要查找行结束字符?
- 是否会在这里使用TCP_NODELAY之类的东西?
- 任何额外的信息都将非常感激。我想学好这些东西,但目前无法让任何事情正常工作。
代码:
public void run() {
while (connectionStatus == TCP_SOCKET_STATUS_CONNECTED) {
try {
if (outMessage != null){
OutStream.writeBytes(outMessage);
OutStream.flush();
outMessage = ("OUT TO SERVER: " + outMessage);
// socketClient.close();
sendMessageToAllUI(0, MAINACTIVITY_SET_TEXT_STATE, "appendText" , outMessage);
outMessage = null;
}
if (InStream != null) {
String modifiedSentence = InStream.readLine();
sendMessageToAllUI(0, MAINACTIVITY_SET_TEXT_STATE, "appendText" , "\n" + "IN FROM SERVER: " + modifiedSentence);
}
Thread.sleep(1000);
} catch (IOException e) {
connectionLost();
break;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
制作套接字的线程:
public void run() {
if(I) Log.i(LOGTAG, "Attempt Connection with IP: " + serverIP + " ...");
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) {
if(I) Log.i(LOGTAG," ...UnknownException e: e.getMessage() shows: " + e.getMessage());
connectionFailed();
} catch (SocketTimeoutException e) {
if(I) Log.i(LOGTAG," ...SocketTimoutException e: e.getMessage() shows: " + e.getMessage());
connectionFailed();
} catch (IOException e) {
if(I) Log.i(LOGTAG," ...caught on run()");
// Close the socket
try {
tempSocketClient.close();
} catch (IOException e2) {
Log.e(LOGTAG, "unable to close() socket during connection failure", e2);
}
if(I) Log.i(LOGTAG," ...IOException e: e.getMessage() shows: " + e.getMessage());
connectionFailed();
return;
}
}
我发现在线上使用的 Java 服务器,在将其移植到真实服务器之前会一直使用它。
public class Server {
private static String SERVERIP;
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String clientSentence;
String capitalizedSentence;
try {
ServerSocket welcomeSocket = new ServerSocket(8888);
getIp();
System.out.println("Connected and waiting for client input!\n");
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");
}
}
} catch (IOException e) {
// if server is already running, it will not open new port but
// instead re-print the open ports information
getIp();
System.out
.println("Server connected and waiting for client input!\n");
}
}
private static void getIp() {
InetAddress ipAddr;
try {
ipAddr = InetAddress.getLocalHost();
System.out.println("Current IP address : "
+ ipAddr.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
outMessage
在哪里创建的? - Nikolai FetissovoutMessages
标记为“易失性”,以便内存更新从一个线程传播到另一个线程。 - Nikolai Fetissov