0

i did try to test some stuff about Client/server communication in Java because i wanted to build a game with a client/server architecture.

So i read some articles and so on... it all worked till i tried to use the same classes and methods in a libGDX project client and the other as server was still the original java project.

than the fun started and i got class not found exceptions on the server side when the server where trying to read the object.

after that i tried to build client and server both as libGDX projects. still the same error.

the serve:

package com.mygdx.game;

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


public class Server {

    public static void main(String[] args) {
        int port = 8800;
        try {
            ServerSocket serverSocket = new ServerSocket(port);
            while (true) {
                System.out.println("raedy");
                Socket clientSocket = serverSocket.accept();
                System.out.println(clientSocket+"verbunden!");
                Worker worker = new Worker(clientSocket);
                worker.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}



package com.mygdx.game;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class Worker extends Thread{

    private Socket clientSocket;

    public  Worker(Socket clientsocket) {
        this.clientSocket = clientsocket;
    }
    @Override
    public void run() {
        System.out.println("worker started");
        try {
            ObjectInputStream in= new ObjectInputStream(clientSocket.getInputStream());
            Object obj = (Object) in.readObject();
            if(obj instanceof Message) {
            Message msg= (Message) in.readObject();
            System.out.println("Client said: "+msg.getMessage());
            }
            System.out.println("input auf"+clientSocket.getLocalPort());
            ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream());
            out.writeObject(new Message("Hello there!"));
            System.out.println("output auf"+clientSocket.getPort());
        } catch (IOException | ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

package com.mygdx.game;
import java.io.Serializable;


public class Message implements Serializable{

    private static final long serialVersionUID = 1L;

    private String message;

    public Message(String message) {
        this.message = message;
    }

    public String getMessage(){
        return message;
    }
}

the client has the the same Message class and this:

package com.mygdx.game.desktop;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.mygdx.game.MyGdxGame;

public class DesktopLauncher {
    public static void main (String[] arg) {
        int port = 8800;
        try {
            System.out.println("hello there!");
            Socket client = new  Socket("localhost", port );
            ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream());
            out.writeObject(new Message("ich bin der client"));
            System.err.println(client.getPort());

            ObjectInputStream in = new ObjectInputStream(client.getInputStream());
            Message msg= (Message) in.readObject();
            System.out.println("Server said: "+msg.getMessage());
            System.out.println(client.getLocalPort());
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
        new LwjglApplication(new MyGdxGame(), config);
    }
}

so my question is why does the same code work in normal java but when i build the same as a libGDX project and try the code there it doesn't work?

yes the message class is only for testing how a serializable object is transferred. Later i want to send game commands via the object stream.

ps: you made it till the end! thank you :D

pps: the first try in java was without

Object obj = (Object) in.readObject();
            if(obj instanceof Message)

1 Answers1

-1

I would really recommend you to use Kryonet instead of writing all the stuff from scratch yourself. It does exactly what you are trying to achieve, is mature and well tested with libGDX.

MrStahlfelge
  • 1,721
  • 2
  • 13
  • 23