Domanda

I am trying to teach myself some networking in Java using the Kryonet library. The following code is almost identical to the code in the kyronet tutorial. https://code.google.com/p/kryonet/#Running_a_server

The client is successfully sending the message "Here is the request!" to the server (the server is printing it out) however the client is not receiving any response from the server even though the server is sending one.

I've tried unsuccessfully to fix it, can anyone see or suggest a possible problem/solution with the code?

(The code follows)

Client

public class Client_test {
Client client = new Client();
public Client_test() {
    Kryo kryo = client.getKryo();
    kryo.register(SomeRequest.class);
    kryo.register(SomeResponse.class);
    client.start();
    try {
        client.connect(50000, "127.0.0.1", 54555, 54777);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    client.addListener(new Listener() {
           public void received (Connection connection, Object object) {
              if (object instanceof SomeResponse) {       
                 SomeResponse response = (SomeResponse)object;
                 System.out.println(response.text);
              }
           }
        });
    SomeRequest request = new SomeRequest();
    request.text = "Here is the request!";
    client.sendTCP(request);

}

}

Server

public class ServerGame {
Server server = new Server();

public ServerGame() {
    Kryo kryo = server.getKryo();
    kryo.register(SomeRequest.class);
    kryo.register(SomeResponse.class);
    server.start();
    try {
        server.bind(54555, 54777);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    server.addListener(new Listener() {
           public void received (Connection connection, Object object) {
              if (object instanceof SomeRequest) {
                 SomeRequest request = (SomeRequest)object;
                 System.out.println(request.text);
                 SomeResponse response = new SomeResponse();
                 response.text = "Thanks!";
                 connection.sendTCP(response);
              }
           }
        });
}

}

Response & Request classes

public class SomeRequest {
public String text;
public SomeRequest(){}
}

public class SomeResponse {
public String text;
public SomeResponse(){}
}
È stato utile?

Soluzione

After many hours watching youtube videos and sifting through the web I found the answer. Which I will post on here as it seems that quite a few people have had this problem so I would like to spread the word.

Basically the client would shut down immediately, before it could receive and output the message packet. This is because "Starting with r122, client update threads were made into daemon threads, causing the child processes to close as soon as they finish initializing.", the solution is "Maybe you could use this? new Thread(client).start();".

So basically instead of using

client.start();

to start the client thread you must use

new Thread(client).start();

Which I believe stops the thread being made into a daemon thread which therefore stops the problem.

Source: https://groups.google.com/forum/?fromgroups#!topic/kryonet-users/QTHiVmqljgE

Altri suggerimenti

Yes, inject a tool like Fiddler in between the two so you can see the traffic going back and forth. It's always easier to debug with greater transparency, more information.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top