Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (537)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [KRYONET] Problems with chat  (Read 1747 times)
0 Members and 1 Guest are viewing this topic.
Offline baenor

Senior Newbie





« Posted 2013-02-06 17:55:17 »

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) {
         // TODO Auto-generated catch block
        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()); // 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
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) {
         // TODO Auto-generated catch block
        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){ // 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
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
Offline baenor

Senior Newbie





« Reply #1 - Posted 2013-02-07 09:07:50 »

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?
Offline breno.inojosa

Junior Newbie





« Reply #2 - Posted 2013-03-28 21:39:05 »

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..
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (17 views)
2014-08-01 22:53:16

CogWheelz (15 views)
2014-08-01 22:51:43

CopyableCougar4 (20 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (42 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

Resources for WIP games
by CogWheelz
2014-08-01 18:19:50

List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!