I'm trying to write a simple TCP client server connection. The server spawns a thread for each new client connection, and each thread talks to a client. I'm using DataInputStream and DataOutputStream classes, upon dis.readUTF() the server thread comes to a halt. I tried using BufferedReader and PrintStream/Printwriter, still the same issue. Please look for System.out.println("not here now"), that line preceding it blocks the execution.
/*
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();
}
}
}