[英]Lag between DataOutputStream and DataInputStream using an android client and python server
[英]TCP client/server program, DataInputStream / DataOutputStream issue
我正在嘗試編寫一個簡單的TCP客戶端服務器連接。 服務器為每個新客戶端連接生成一個線程,每個線程與客戶端進行通信。 我正在使用DataInputStream和DataOutputStream類,在dis.readUTF()上,服務器線程停止運行。 我嘗試使用BufferedReader和PrintStream / Printwriter,仍然是同樣的問題。 請查找System.out.println(“現在不在這里”),它前面的那行阻止執行。
/*
TCP client
*/
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class TCPClient {
public TCPClient() {
// TODO Auto-generated constructor stub
}
public static void main (String args[]) throws UnknownHostException, IOException {
Socket socket = new Socket("localhost", 9701);
DataInputStream input = new DataInputStream(socket.getInputStream());
DataOutputStream output = new DataOutputStream(socket.getOutputStream());
//char[] buffer = new char[100];
boolean stop = false;
while (!stop) {
System.out.println("here");
output.writeBytes("hello server");
String response = "-WTF-";
System.out.println("here");
response = input.readUTF();
System.out.println("not here now");
if (response == "kill") {
stop = true;
} else {
System.out.println("5");
output.writeBytes("talk to me");
System.out.println("received" + response);
}
}
socket.close();
}
}
/* TCP server */
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServer extends Thread {
final static int TCP_SERVER_PORT = 9701;
private Socket socket;
public TCPServer(Socket sock) {
// TODO Auto-generated constructor stub
socket = sock;
}
public void run() {
System.out.println(this.socket.getPort() + " working or sleeping for 5 seconds");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DataInputStream clientinp;
DataOutputStream clientout;
try {
clientinp = new DataInputStream(socket.getInputStream());
clientout = new DataOutputStream(socket.getOutputStream());
System.out.println("here");
while (true) {
System.out.println("here now");
String sentence = clientinp.readUTF();
System.out.println("not here now");
System.out.println(sentence);
clientout.writeBytes(sentence);
}
}
catch (IOException e) {
System.out.println(e.getStackTrace());
}
finally {
try {
this.socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* other logic
*/
}
public static void main(String args[]) throws IOException {
ServerSocket serversocket;
serversocket = new ServerSocket(TCP_SERVER_PORT);
while (true) {
Socket clientsocket = serversocket.accept();
new TCPServer(clientsocket).start();
}
}
}
您正在使用writeBytes
在客戶端中編寫字符串,並使用readUTF
讀取服務器中的相同字符串。
如果您查看這兩種方法的javadoc,您將看到您正在以一種格式書寫,然后在另一種格式中閱讀。 具體來說, readUTF
期望輸入以2字節字符計數開始,然后是字符的“修改的UTF-8”編碼。 但writeBytes
只為每個字符寫入1個字節。 通常, readUTF
將嘗試讀取比writeBytes
寫入更多的字節...並且套接字流將凍結。
你應該使用writeUTF
而不是writeBytes
...
斯蒂芬C是對的。 我做了一些小修改,但原因是'在另一邊使用write *時使用read *'。
這是運行代碼:
客戶:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class TCPClient {
public TCPClient() {
}
public static void main(String args[]) throws UnknownHostException, IOException {
Socket socket = new Socket("localhost", 9701);
DataInputStream input = new DataInputStream(socket.getInputStream());
DataOutputStream output = new DataOutputStream(socket.getOutputStream());
boolean stop = false;
while (!stop) {
System.out.println("client->server: hello...");
output.writeUTF("hello");
System.out.println("client: waiting...");
String response = input.readUTF();
System.out.printf("client: got response: %s\n", response);
}
socket.close();
}
}
服務器:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServer extends Thread {
final static int TCP_SERVER_PORT = 9701;
private Socket socket;
public TCPServer(Socket sock) {
socket = sock;
}
public void run() {
System.out.println(this.socket.getPort() + " working or sleeping for 5 seconds");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
DataInputStream clientinp;
DataOutputStream clientout;
try {
clientinp = new DataInputStream(socket.getInputStream());
clientout = new DataOutputStream(socket.getOutputStream());
while (true) {
System.out.println("reading...");
String sentence = clientinp.readUTF();
System.out.printf("read: %s", sentence);
clientout.writeUTF(String.format("answer: %s", sentence));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) throws IOException {
ServerSocket serversocket;
serversocket = new ServerSocket(TCP_SERVER_PORT);
while (true) {
Socket clientsocket = serversocket.accept();
new TCPServer(clientsocket).start();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.