我一直在尝试不同的实现方式,使这项工作,并且已经搜查的StackOverflow和Android开发的解决方案,但我没有太多的编程经验,并不能得到这个块代码都正常工作。
我的意图:
- 这是在一个线程中,将通过检查,如果有一个outMessage循环,如果有,它会发送消息。
- 接着,它会检查,如果有什么事情在插播,如果有,将其发送到处理程序在我的主要活动。
- 最后,它会睡眠1秒钟,然后再次检查。
- 这应该让我读/写多次,而无需关闭和打开插座。
问题:
- 直到我关闭套接字的outstream没有得到刷新。 冲洗()似乎没有任何效果。
我的请求:
- 请张贴得到这个代码上面(任何注释描述来解释为什么将不胜感激。链接到其他类似的问题正常工作所需的更改/答案将是巨大的帮助我学习,但我一直在寻找的一对夫妇周只是无法得到它的工作,所以请确保您还包括改变了这种代码,以如上文所述。在此先感谢工作的需要。
其他:
- 我想知道如果我的河道内及/或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();
}
}
}