Warum schließt dieser Port/Socket, sobald eine Verbindung von einem Client hergestellt wurde?Java Socket schließt nach Verbindung?
package app;
import java.io.*;
import java.net.*;
public class socketServer {
public static void main(String[] args) {
int port = 3333;
boolean socketBindedToPort = false;
try {
ServerSocket ServerSocketPort = new ServerSocket(port);
System.out.println("SocketServer Set Up on Port: " + port);
socketBindedToPort = true;
if(socketBindedToPort == true) {
Socket clientSocket = null;
try {
clientSocket = ServerSocketPort.accept();//This method blocks until a socket connection has been made to this port.
System.out.println("Waiting for client connection on port:" + port);
/** THE CLIENT HAS MADE A CONNECTION **/
System.out.println("CLIENT IS CONENCTED");
}
catch (IOException e) {
System.out.println("Accept failed: " + port);
System.exit(-1);
}
}
else {
System.out.println("Socket did not bind to the port:" + port);
}
}
catch(IOException e) {
System.out.println("Could not listen on port: " + port);
System.exit(-1);
}
}
}
Nur eine auf Verbindungsanfragen hören Beachten Sie, dass Ihre Druckdaten in der falschen Reihenfolge sind. Sie sollten "Warten auf Client-Verbindung ..." vor clientSocket = ServerSockPort.accept(); – Joel