Ich versuche, ein Java-Programm zu erstellen, um eine Nachricht vom Client an den Server zu senden und eine Antwort zurück mit Socket-Programmierung empfangen es kommt mit diesem Fehler auf:Binding-Server-Socket Ausnahme im Thread "Haupt" java.net.BindException: Adresse bereits verwendet: JVM_Bind Java
Exception in thread "main" java.net.BindException: Address already in use: JVM_Bind at java.net.DualStackPlainSocketImpl.bind0(Native Method) at java.net.DualStackPlainSocketImpl.socketBind(DualStackPlainSocketImpl.java:106) at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:387) at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:190) at java.net.ServerSocket.bind(ServerSocket.java:375) at java.net.ServerSocket.(ServerSocket.java:237) at java.net.ServerSocket.(ServerSocket.java:128) at Bob.main(Bob.java:9)
Client-Seite:
public static void main(String[] args) throws IOException {
InetAddress addr = InetAddress.getByName("127.0.0.1") ; //set the address of the server to be connected to. Any IP address can be set here as a string
//If the name is saved in your "etc/hosts" file, then the name can be used instead of the ip address
//Alternative method: InetAddress.getByAddress -see "stackoverflow" explanation if interested
System.out.println("addr = " + addr);
Socket socket = new Socket(addr, 8080); //attempt to connect to the server by creating a socket using the IP address and Port number of the server
try {
System.out.println("socket = " + socket);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); //create an input variable for the socket- accepts input from the socket
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true); // create an output variable for the socket- writes output to the socket
//this loop simply writes to the socket 10 times waits for a reply from the socket and then prints the reply received
for(int i = 0; i < 10; i ++) {
out.println("I am it: " + i); //write to socket
String str = in.readLine(); //read from socket
System.out.println(str); //print to console line read from socket
}
out.println("END"); //send string that indicates end of transmission
}catch(SocketException e) {
System.out.println("Server closed connection");
}finally{
System.out.println("closing...");
socket.close(); //close the connection
}
}
Server-Seite:
public static final int PORT = 8080;
public static void main(String[] args) throws IOException {
ServerSocket s = new ServerSocket(PORT); //create server socket object
System.out.println("Started: " + s);
try {
Socket socket = s.accept(); //accept a socket connection using the Server Socket created previously
try {
System.out.println("Connection accepted: "+ socket);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); //create an input variable for the socket- accepts input from the socket
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true); // create an output variable for the socket- writes output to the socket
//this loop accepts input from the socket, prints out what it receives anc echoes it back into the socket until it recives the word "END" indicating the end of transmission
while (true) {
String str = in.readLine(); //read line from socket
if (str.equals("END")) break;
System.out.println("Echoing: " + str); //print line to console
out.println("You said: " + str); //echo line read back into socket
}
}catch (SocketException e) {
System.out.println("Client closed connection");
}finally{ //close the present connection
System.out.println("closing...");
socket.close();
}
}catch(SocketException e){
System.out.println("Server socket closed unexpectedly");
}finally{ //close the server socket. i.e. stop listening for connections
s.close(); //without this line, the server would continue waiting for another connection
}
}
Sind Sie es auf der gleichen Maschine laufen? Der Client & Server-Teil verwendet den gleichen Port. –
ja ich führe es auf dem gleichen Rechner – Student
Das hat nichts mit dem Verbinden eines Clients zu tun. Es passiert, wenn Sie den Server ausführen. "SocketException" bedeutet nicht "Client-Verbindung geschlossen", und Sie prüfen nicht nach dem Ende des Streams. – EJP