我一直在使用TCP服務器/客戶端的東西一段時間。當涉及到連接多個多客戶端用戶時,我非常擅長UDP編程。我試圖做同樣的,我把使用線程但每當線程獲取到這段代碼使用線程處理多個Java TCP客戶端
String reader = (String)in.readObject();
產生一個錯誤,並且線程停止執行的代碼,但該線程仍然運行該程序的TCP服務器上保持活力。 反正這裏是完整的源代碼:
public class TestServer implements Runnable {
private Thread run, streams, connect, receive, send;
private ServerSocket socket;
private Socket conn;
private ObjectInputStream in;
private ObjectOutputStream out;
private boolean running, incomingMessage = false;
private int port;
public TestServer(int port) throws IOException {
this.port = port;
socket = new ServerSocket(port);
console("Server stated on : " + InetAddress.getLocalHost() + " : " + port);
run = new Thread(this, "Run");
run.start();
}
public void run() {
running = true;
connect();
receive();
}
private void connect() {
connect = new Thread("Connect") {
public void run() {
while(running) {
try {
conn = socket.accept();
} catch (IOException e) {
e.printStackTrace();
}
console("You are now connected" + conn.getInetAddress().toString() + " : " + conn.getPort());
try {
setupStreams();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}; connect.start();
}
private void setupStreams() throws IOException {
streams = new Thread("Streams") {
public void run() {
try {
console("Setting up Streams");
out = new ObjectOutputStream(conn.getOutputStream());
out.flush();
in = new ObjectInputStream(conn.getInputStream());
console("Streams are now setup");
incomingMessage = true;
receive.start();
} catch(IOException e) {
e.printStackTrace();
}
}
}; streams.start();
}
private void receive() {
receive = new Thread("Receive") {
public void run() {
while(incomingMessage) {
String message = "";
try {
message = (String) in.readObject();
//This is the only flaw the program
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
console("Client : " + message);
}
}
};
}
private void console(String message) {
System.out.println(message);
}
public static void main(String[] args) {
try {
new TestServer(1234);
} catch (IOException e) {
e.printStackTrace();
}
}
}
FYI不是新的這一點。錯誤是由於服務器開始接收數據包,即使沒有數據包要接收。但是因爲線程強制它接收它,所以我在線程中產生錯誤,並且不知道其他任何方法來解決這個問題。所以請幫助。提前致謝。
'incomingMessage'從未設置從初始到'FALSE'分開,所以while循環無法結束。 –
定義'生成錯誤'。 – EJP