# Netzwerk Portzuweisungsproblem



## C-dric (7. Nov 2015)

Hier mein Server:

Client:

```
package version_1;

import java.io.*;
import java.net.*;

class Server extends Thread implements Runnable {

    String inFromClient = null;
    Socket connectionSocket = null;

    public Server() throws IOException {
        ServerSocket welcomeSocket = new ServerSocket(6789);

        System.out.println("Started");
        int i = 0;
        while (true) {
            this.connectionSocket = welcomeSocket.accept();
            Thread thread = new Thread(this, "KommunikationClient" + i);
            i++;
            thread.start();
        }
    }

    public static void main(String args[]) throws Exception {
        new Server();
    }

    public void run() {
        System.out.println("Eingangsnachricht");
        String capitalizedSentence;
        String clientSentence = null;
        try {
            this.inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())).readLine();
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
        System.out.println(inFromClient);
        System.out.println(connectionSocket);

        clientSentence = inFromClient.toString();
        System.out.println("Received: " + clientSentence);
        DataOutputStream outToClient = null;
        try {
            outToClient = new DataOutputStream(connectionSocket.getOutputStream());
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        capitalizedSentence = clientSentence.toUpperCase() + '\n';

        try {
            outToClient.writeBytes(capitalizedSentence);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
```


```
package version_1;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketException;
import java.util.Scanner;

class Client {
 
    public static void main(String argv[]) throws Exception {
        // Scanner initialisieren
        Scanner sc = new Scanner(System.in);

        // Variablen deklarieren
        Socket clientSocket = null;
        String modifiedSentence;

        // Verbindung herstellen
     
        try {
            clientSocket = new Socket("localhost", 6789);

        } catch (java.net.ConnectException e) {
            System.out.println("!!!Du musst zuerst den Server starten!!!\n");
            e.printStackTrace();
        }
        // Ausgabe festlegen
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
        // Eingang festlegen
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        try{
        outToServer.writeBytes(sc.nextLine() + "\n");
        }catch(SocketException e){
            System.out.println("Server wurde geschlossen");
            clientSocket.close();
            System.exit(1);
        }
        modifiedSentence = inFromServer.readLine();
        System.out.println("FROM SERVER: " + modifiedSentence);
        clientSocket.close();
    }
}
```

Dieses funktioniert jedoch wird manchmal ein Port doppelt belegt wenn ich mehrere Clients starte, sodass es dann eine Exception gibt:


```
Started
Eingangsnachricht
Eingangsnachricht
Hei du
Socket[addr=/127.0.0.1,port=57592,localport=6789]
Received: Hei du
LLL
Socket[addr=/127.0.0.1,port=57592,localport=6789]
Received: LLL
java.net.SocketException: Software caused connection abort: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:132)
    at java.io.DataOutputStream.writeBytes(DataOutputStream.java:276)
    at version_1.Server.run(Client.java:66)
    at java.lang.Thread.run(Thread.java:745)
Eingangsnachricht
Eingangsnachricht
Eingangsnachricht
Eingangsnachricht
Eingangsnachricht
Eingangsnachricht
Eingangsnachricht
sg
Socket[addr=/127.0.0.1,port=57600,localport=6789]
Received: sg
sdf
Socket[addr=/127.0.0.1,port=57600,localport=6789]
Received: sdf
sfgjk
Socket[addr=/127.0.0.1,port=57600,localport=6789]
Received: sfgjk
fsd
Socket[addr=/127.0.0.1,port=57600,localport=6789]
Received: fsd
gfd
Socket[addr=/127.0.0.1,port=57600,localport=6789]
Received: gfd
hfgs
Socket[addr=/127.0.0.1,port=57600,localport=6789]
Received: hfgs
ad
Socket[addr=/127.0.0.1,port=57600,localport=6789]
Received: ad
java.net.SocketException: Software caused connection abort: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:132)
    at java.io.DataOutputStream.writeBytes(DataOutputStream.java:276)
    at version_1.Server.run(Client.java:66)
    at java.lang.Thread.run(Thread.java:745)
Eingangsnachricht
iuzt
Socket[addr=/127.0.0.1,port=57617,localport=6789]
Received: iuzt
Eingangsnachricht
Eingangsnachricht
Eingangsnachricht
Eingangsnachricht
tu
Socket[addr=/127.0.0.1,port=57622,localport=6789]
Received: tu
sfd
java.net.SocketException: Software caused connection abort: socket write error
Socket[addr=/127.0.0.1,port=57622,localport=6789]
Received: sfd
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:132)
    at java.io.DataOutputStream.writeBytes(DataOutputStream.java:276)
    at version_1.Server.run(Client.java:66)
    at java.lang.Thread.run(Thread.java:745)
```


----------

