# LibGdx SocketExeption: Connection reset



## H4CKSprogrammer (14. Apr 2018)

Hallo,
ich bin gerade dabei mich in libgdx mit der Server/Multiplayer Programmierung bekannt zu machen. Doch ich bekomme immer denselben Fehler wenn ich meinen Client schließe und ihn somit von dem Server trenne außerdem werden die Daten nicht übergeben...
Mein Server:

```
public class Networkingmain extends ApplicationAdapter {
    SpriteBatch batch;
    Skin skin;
    Label label;
    OrthographicCamera cam;
    Stage stage;
    String data;
    String client;
    int clients = 0;
   
    @Override
    public void create () {
        batch = new SpriteBatch();
        cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        stage = new Stage();
        Gdx.input.setInputProcessor(stage);

        skin = new Skin(Gdx.files.internal("core/assets/data/uiskin.json"));

        Group group = new Group();
        group.setBounds(0,0, 600,800);
        label = new Label("Ich empfange...", skin);
        label.setPosition(300 - label.getWidth()/2, 400 - label.getHeight()/2);
        group.addActor(label);

        stage.addActor(group);

        stage.getCamera().position.set(300, 400, 0);

        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("1");
                ServerSocketHints ssh = new ServerSocketHints();
                ssh.acceptTimeout = 0;
                ServerSocket server = Gdx.net.newServerSocket(Net.Protocol.TCP, 1337, ssh);
                while (true) {
                    System.out.println("2");
                    Socket s = server.accept(null);
                    BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
                    try {
                        System.out.println("3");
                        System.out.println(br.readLine());// betroffene zeile
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();


    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.setProjectionMatrix(cam.combined);
        label.setPosition(300 - label.getWidth()/2, 400 - label.getHeight()/2);
        batch.begin();
        stage.draw();
        batch.end();
    }
   
    @Override
    public void dispose () {
        batch.dispose();
    }
}
```

Und mein client:

```
public class MyGdxGame extends ApplicationAdapter {
    TiledMap map;
    OrthogonalTiledMapRenderer maprender;
    OrthographicCamera cam;
    Player player;
    Texture img;
    TextureRegion[] [] region;
    Sprite playersprite;
    String send = "";
    SocketHints sh;
    Socket socket;
    Skin skin;
    TextButton button;
    Stage stage;
    int i;

    @Override
    public void create () {
        map = new TmxMapLoader().load("map.tmx");
        maprender = new OrthogonalTiledMapRenderer(map);

        cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        cam.position.x = 400;
        cam.position.y = 300;
        cam.update();

        /*stage = new Stage();
        Gdx.input.setInputProcessor(stage);

        skin = new Skin(Gdx.files.internal("data/uiskin.json"));

        button = new TextButton("Exit", skin);

        button.setPosition(800 - button.getWidth(), 600 - button.getHeight());

        stage.addActor(button);

        stage.getCamera().position.set(400, 300, 0);*/

        img = new Texture("man_sprite2.png");
        region = TextureRegion.split(img, img.getWidth()/4, img.getHeight()/4);
        playersprite = new Sprite(region[1] [0]);
        player = new Player(playersprite);
        player.setX(400);
        player.setY(300);


        sh = new SocketHints();
        sh.connectTimeout = 10000;
        socket = Gdx.net.newClientSocket(Net.Protocol.TCP, "localhost", 1337, sh);
        send = "x: " + player.getX();

        try {
            socket.getOutputStream().write(send.getBytes());
            System.out.println(send);
        } catch (IOException e) {
            e.printStackTrace();
        }



        /*button.addListener(new ChangeListener() {
            @Override
            public void  changed(ChangeEvent event, Actor actor) {
                System.out.println("clicked");
                send = "0";
                System.out.println(send);
            }
        });*/


    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        //Gdx.app.log("cameraposition", "x: " + cam.position.x + ", y: " + cam.position.y);


        //sendData(send + "\n");
        cam.update();
        maprender.setView(cam);
        maprender.render();

        /*maprender.getBatch().begin();
        stage.draw();
        maprender.getBatch().end();*/
        maprender.getBatch().begin();
        player.draw(maprender.getBatch());
        maprender.getBatch().end();

    }
   
    @Override
    public void dispose () {
        maprender.dispose();
        map.dispose();
        //stage.dispose();
    }

    /*public void sendData(String data){

    }*/
}
```

Die genaue Fehlermeldung:

```
java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:210)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
    at java.io.InputStreamReader.read(InputStreamReader.java:184)
    at java.io.BufferedReader.fill(BufferedReader.java:161)
    at java.io.BufferedReader.readLine(BufferedReader.java:324)
    at java.io.BufferedReader.readLine(BufferedReader.java:389)
    at com.markus.network.Networkingmain$1.run(Networkingmain.java:66)
    at java.lang.Thread.run(Thread.java:748)
```
Ich hoffe mir kann jmd helfen mein Kopf raucht schon.

Markus


----------



## H4CKSprogrammer (16. Apr 2018)

Antwort: man muss beim client ein \n hinzufügen
--------------
Abgeschlossen
--------------

Markus


----------

