Hi there, i'm new to java network programming. I'm starting to use Kryonet to implement in my game a multiplayer mode.
To understand things, i was tryng to make a simple chat but things doesnt work well here is the code:
I made a Client project and a Server project, both have the same setting or rather a Body Class, a NetworkListener Class and a Packet class that is the same for client and server projects.
CBody:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| import it.client.Packet.*; import java.io.IOException; import java.util.Scanner;
import com.esotericsoftware.kryo.Kryo; import com.esotericsoftware.kryonet.Client;
public class CBody { private Client client; public static Scanner scanner; public static String nickname; public CBody() throws IOException{ scanner = new Scanner(System.in); client = new Client(); NetworkListener nl = new NetworkListener(); client.addListener(nl); nl.init(client); client.setKeepAliveTCP(1000); registerPackets(); client.start(); System.out.println("Scegli il nickname: "); nickname = new String(CBody.scanner.nextLine()); System.out.println("Hai scelto il nick: "+nickname+"."); System.out.println("Enter Ip:\n"); client.connect(5000,scanner.nextLine(),54555,54777); } private void registerPackets(){ Kryo kryo = client.getKryo(); kryo.register(LoginRequest.class); kryo.register(LoginAnswer.class); kryo.register(PacketMessage.class); kryo.register(MsgBroadcast.class); } public static void main(String args[]){ try { new CBody(); } catch (IOException e) { e.printStackTrace(); } } } |
Network listener for CLient
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| import java.io.IOException;
import it.client.Packet.*; import it.client.CBody;
import com.esotericsoftware.kryonet.Client; import com.esotericsoftware.kryonet.Connection; import com.esotericsoftware.kryonet.Listener; import com.esotericsoftware.kryonet.Server;
public class NetworkListener extends Listener { private Client client; public void init(Client client){ this.client = client; } public void connected(Connection c){ System.out.println("[CLIENT]You have connected.\n"); c.sendTCP(new LoginRequest()); } public void disconnected(Connection c){ System.out.println("[CLIENT]You have disconnected.\n"); } public void received(Connection c,Object o){ if(o instanceof LoginAnswer){ System.out.println("Chat inizializzata, inizia a chattare\n"); } if(o instanceof MsgBroadcast){ System.out.println("pacchetto ricevuto\n"); String broadcast = new String(); String writer = new String(); broadcast = ((MsgBroadcast) o).text; writer = ((MsgBroadcast) o).nickname; if(CBody.nickname.equals(writer)) System.out.println("Hai scritto: "+broadcast); else { System.out.println(writer+" ha scritto :"+broadcast); } } String text = new String(); if(CBody.scanner.hasNext()){ PacketMessage msg = new PacketMessage(); msg.nickname = CBody.nickname; msg.text = CBody.scanner.nextLine(); c.sendTCP(msg); } } } |
Body for server
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| import NetServer.Packet.*;
import java.io.IOException;
import com.esotericsoftware.kryo.Kryo; import com.esotericsoftware.kryonet.Server;
public class Body { private Server server; public Body() throws IOException{ server = new Server(); NetworkListener nl = new NetworkListener(); registerPackets(); server.addListener(nl); nl.init(server); server.start(); server.bind(54555,54777); } private void registerPackets(){ Kryo kryo = server.getKryo(); kryo.register(LoginRequest.class); kryo.register(LoginAnswer.class); kryo.register(PacketMessage.class); kryo.register(MsgBroadcast.class); } public static void main(String args[]){ try { new Body(); } catch (IOException e) { e.printStackTrace(); } } } |
networklistener for server
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| public class NetworkListener extends Listener { private Server server; public void init(Server server){ this.server = server; } public void connected(Connection c){ System.out.println("[SERVER]Someone has connected.\n"); } public void disconnected(Connection c){ System.out.println("[SERVER]Someone has disconnected.\n"); } public void received(Connection c,Object o){ c.setTimeout(0); if(o instanceof LoginRequest){ LoginAnswer loginAnswer = new LoginAnswer(); loginAnswer.accepted=true; c.sendTCP(loginAnswer); } if(o instanceof PacketMessage){ String text = new String(); String nickname = new String(); text = ((PacketMessage) o).text; nickname = ((PacketMessage)o).nickname; MsgBroadcast broadcast = new MsgBroadcast(); broadcast.nickname = nickname; broadcast.text = text; System.out.println(nickname+" ha scritto: "+broadcast.text); server.sendToAllTCP(broadcast); } } } |
packet class for both
1 2 3 4 5 6
| public class Packet { public static class LoginRequest { } public static class LoginAnswer { boolean accepted = false; } public static class PacketMessage {String nickname; String text; } public static class MsgBroadcast {String nickname; String text; int a;} } |
Basically the problem is that when a client write doesnt arrive ALWAYS to every client. Sometimes it looks like as it doesnt receive the broadcast message...server works perfectly infact server-side i see every message that clients right...but i want that clients are able to see what is written by the others....help me please