[KRYONET] Problems with chat

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:

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) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

Network listener for CLient

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()); // invio al server che un client sta loggando
	}
	
	public void disconnected(Connection c){
		System.out.println("[CLIENT]You have disconnected.\n");
	}
	
	public void received(Connection c,Object o){
		if(o instanceof LoginAnswer){ // sei stato accettato
			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

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) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

networklistener for server


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){ // se qualcuno manda un pacchetto di questo genere
			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);
			//c.sendTCP(broadcast);
	        server.sendToAllTCP(broadcast);
		}
	}
}

packet class for both

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

Someone can help me? i’m really tired, i’m not able to understand where the problem is…i would like that the client receives packets automatically, instead it seems like that it receives packets only after i write something on…

UPDATE: the problem is the scanner methods that blocks the receiving of packets…i tried a client version without scanner, and one with scanner…the packets arrives good in the one without scanner…how can i solve the problem?

Why don’t you try the Input class of the library you’re using then?
I don’t think it’s a good idea to use the scanner class…