Jakob
Jakob

Reputation: 53

Network communication isn´t working

I made a little game. Now i want to get the highscore from my Server. The code on the client:

        private int getOnlineHighscore() {
    int highscore = 0;
    try {
        socket = new Socket("localhost", 444);
        input = socket.getInputStream();
        System.out.println(input);
        highscore = input.read();
        input.close();
        socket.close();
        input = null;
        socket = null;
    } catch (Exception e) {
        System.out.println("Verbindung fehlgeschlagen!");
    }
    System.out.println(highscore);
    return highscore;
}

And on the Server:

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

 public class ReadServer extends Thread {
 private Socket socket;

public ReadServer(Socket socket) {
    super();
    this.socket = socket;
}

public void run() {
    try {
        System.out.println(socket.getInetAddress());
        String result = "";
        try (BufferedReader br = new BufferedReader(
                new FileReader(System.getProperty("user.home") + "/AppData/Roaming/GameServer/.sg"))) {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();
            System.out.println("2");
            while (line != null) {
                sb.append(line);
                sb.append(System.lineSeparator());
                line = br.readLine();
            }
            System.out.println("3");
            result = sb.toString();
            System.out.println("3.5");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("4");
        socket.getOutputStream().write(Integer.parseInt(result));
        System.out.println(result);
    } catch (Exception e) {
    }

    try {
        socket.close();
    } catch (Exception e) {
    }
}

public static void main(String[] Args) {
    Socket socket = null;
    ServerSocket server = null;
    try {
        server = new ServerSocket(444);
        while (true) {
            socket = server.accept();
            new ReadServer(socket).start();
        }
    } catch (Exception e) {
    }

    try {
        server.close();
    } catch (Exception e) {
    }
}
}

If I run it, the client function returns: -1 The server writes in the console(not important I think): /127.0.0.1 2 3 3.5 4

How to solve the problem? I want to send an int stored on my Server to a client.

-Jakob

Upvotes: 1

Views: 31

Answers (2)

Zbynek Vyskovsky - kvr000
Zbynek Vyskovsky - kvr000

Reputation: 18825

What is the highscore stored in the file? I believe the file is empty and it fails on parsing the integer but as your catch block is empty, you don't see the exception. Put printStacktrace or rethrow.

Another problem is that OutputStream sends only bytes and therefore write method sends only low 8 bits. To send int wrap the stream with DataOutputStream and DataInputStream on the client side.

Upvotes: 0

Ramanlfc
Ramanlfc

Reputation: 8354

-1 is returned by read() to specify end of stream , make sure data to be read is being returned .

Upvotes: 1

Related Questions