Виснет многопоточный Socket-Server.??
Виснет на этой строке
Код:
this.responseMessage = dataInputStream.readUTF();
Запуск и коннект происходит, - смотрел с дебаггером.
А вот дальше ничего посмотреть не могу..
Вот два стандартных класса(пока что тестовый вариант).
Код:
package chess.network;
import java.io.IOException;
import java.net.ServerSocket;
public class ChessServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
boolean listening = true;
System.err.println("SERVER!: 4444.");
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(-1);
}
while (listening) {
new ChessMultiServerThread(serverSocket.accept()).start();
}
serverSocket.close();
}
}
import java.io.IOException;
import java.net.ServerSocket;
public class ChessServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
boolean listening = true;
System.err.println("SERVER!: 4444.");
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(-1);
}
while (listening) {
new ChessMultiServerThread(serverSocket.accept()).start();
}
serverSocket.close();
}
}
Код:
package chess.network;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class ChessMultiServerThread extends Thread {
private Socket socket = null;
public ChessMultiServerThread(Socket socket) {
super("KKMultiServerThread");
this.socket = socket;
}
public void run() {
try {
System.out.println("Thred");
PrintWriter out = new PrintWriter(socket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("In Stream: "+in.toString());
out.write("To Client");
System.out.println("Out Stream: "+out.toString());
out.flush();
out.close();
in.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class ChessMultiServerThread extends Thread {
private Socket socket = null;
public ChessMultiServerThread(Socket socket) {
super("KKMultiServerThread");
this.socket = socket;
}
public void run() {
try {
System.out.println("Thred");
PrintWriter out = new PrintWriter(socket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("In Stream: "+in.toString());
out.write("To Client");
System.out.println("Out Stream: "+out.toString());
out.flush();
out.close();
in.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Код:
package chess.network;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class CHClient {
private Socket clientSocket;
private InputStream inputStream;
private OutputStream outputStream;
private DataInputStream dataInputStream;
private DataOutputStream dataOutputStream;
private final String hostName = "localhost";
private final int PORT = 4444;
private String requestMessage = null;
private String responseMessage = null;
public CHClient() {
System.out.println("-CLIENT-");
try {
this.clientSocket = new Socket(this.hostName, this.PORT);
this.inputStream = clientSocket.getInputStream();
this.outputStream = clientSocket.getOutputStream();
this.dataInputStream = new DataInputStream(inputStream);
this.dataOutputStream = new DataOutputStream(outputStream);
this.requestMessage = "Lallllaaaaaa";
sendMessage(this.requestMessage);
this.responseMessage = dataInputStream.readUTF();
System.out.println(this.responseMessage);
System.out.println("ClientStopped!");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
this.inputStream.close();
this.outputStream.close();
this.clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void sendMessage(String msg) {
try {
this.dataOutputStream.writeUTF(msg);
this.dataOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class CHClient {
private Socket clientSocket;
private InputStream inputStream;
private OutputStream outputStream;
private DataInputStream dataInputStream;
private DataOutputStream dataOutputStream;
private final String hostName = "localhost";
private final int PORT = 4444;
private String requestMessage = null;
private String responseMessage = null;
public CHClient() {
System.out.println("-CLIENT-");
try {
this.clientSocket = new Socket(this.hostName, this.PORT);
this.inputStream = clientSocket.getInputStream();
this.outputStream = clientSocket.getOutputStream();
this.dataInputStream = new DataInputStream(inputStream);
this.dataOutputStream = new DataOutputStream(outputStream);
this.requestMessage = "Lallllaaaaaa";
sendMessage(this.requestMessage);
this.responseMessage = dataInputStream.readUTF();
System.out.println(this.responseMessage);
System.out.println("ClientStopped!");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
this.inputStream.close();
this.outputStream.close();
this.clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void sendMessage(String msg) {
try {
this.dataOutputStream.writeUTF(msg);
this.dataOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}