UDP socket chat application in java -


i'm trying create simple chat application in java using udp sockets. there 1 server , multiple clients.

right code :

server :

package chat;  import java.io.*; import java.net.*; import java.util.hashmap; import java.util.set;  class user{     inetaddress addr;     int port;      user(inetaddress a,int p){         addr = a;         port = p;     } }    public class server {     private static final int port = 27012;     private static datagramsocket sckt;     private static datagrampacket in,out;     private static byte[] buffer;     private static hashmap<string,user> users;     public static void main(string[] args) {         try{             system.out.println("opening port...");             sckt = new datagramsocket(port);             users = new hashmap<string,user>();          }         catch(socketexception e){             system.out.println("port connection failed!");             system.exit(1);         }          handleclient();     }         private static void sendmsg(inetaddress addr, int port, string msg){             try{             out = new datagrampacket(msg.getbytes(),msg.length(),addr,port);             sckt.send(out);             }catch(ioexception e){                 e.printstacktrace();             }         }     private static void handleclient(){         try{             string msgin,msgout="",sendernick;             do{             buffer = new byte[256];             in = new datagrampacket(buffer,buffer.length);             sckt.receive(in);             inetaddress clientaddress = in.getaddress();             int clientport = in.getport();             msgin = new string(in.getdata(),0,in.getlength());             //print msgin             //system.out.println(msgin);                         sendernick = msgin.substring(0,msgin.indexof(" "));                         msgin = msgin.substring(msgin.indexof(" ")+1);             if(msgin.equals("/connect")){                 //string nick = msgin.substring(msgin.indexof(" ") + 1);                 system.out.println(sendernick);                 if(users.containskey(sendernick)){                     msgout = "nick in use!";                 }                 else{                     users.put(sendernick, new user(clientaddress,clientport));                     msgout = "connected!";                 }                                 sendmsg(clientaddress,clientport,msgout);             }                         else if(msgin.equals("/list")){                             set usernames;                             usernames = users.keyset();                             msgout = "users : \n";                             msgout += usernames.tostring();                             sendmsg(clientaddress,clientport,msgout);                         }                         else if(msgin.startswith("/msg")){                             string tmp = msgin.substring(msgin.indexof(" ")+1);                             string receivername = tmp.substring(0,tmp.indexof(" "));                             string message = tmp.substring(tmp.indexof(" ")+1);                              if(!users.containskey(receivername)){                                 msgout = "user " + receivername + " not found!";                                 sendmsg(clientaddress,clientport,msgout);                             }                             else{                                 user receiver = users.get(receivername);                                 msgout = "message "+  sendernick  +" : "+message;                                 sendmsg(clientaddress,clientport,"message sent!");                                 sendmsg(receiver.addr,receiver.port,msgout);                             }                          }                         else if(msgin.startswith("/nick")){                             string newnick = msgin.substring(msgin.indexof(" ")+1);                             if(users.containskey(newnick)){                                 msgout = "nick in use!";                             }                             else{                                 users.put(newnick,users.get(sendernick));                                 users.remove(sendernick);                                 msgout = "nick changed!";                             }                             sendmsg(clientaddress,clientport,msgout);                         }                         else if(msgin.equals("/disconnect")){                             users.remove(sendernick);                         }              //out = new datagrampacket(msgout.getbytes(),msgout.length(),clientaddress,clientport);             //sckt.send(out);             }while(true);         }         catch(ioexception e){             e.printstacktrace();         }         finally{             system.out.println("closing connection.");             sckt.close();         }     }  } 

client :

package chat;  import java.io.*; import java.net.*; import java.util.scanner;  public class client {      private static inetaddress host;     private static final int port = 27012;     private static datagramsocket sckt;     private static datagrampacket in, out;     private static byte[] buffer;     private static string nick;      static class messagelistener implements runnable {         //datagrampacket in;         //byte[] buffer;         string reply;         public void run() {             do{             try{             buffer = new byte[256];             in = new datagrampacket(buffer,buffer.length);             sckt.receive(in);             reply = new string(in.getdata(),0,in.getlength());             system.out.println("server> "+reply);             }             catch(ioexception e){                 e.printstacktrace();             }             }while(true);         }     }      public static void main(string[] args) {         try {             host = inetaddress.getlocalhost();          } catch (unknownhostexception e) {             system.out.println("host not found!");             system.exit(1);         }         connect();      }      private static void sendmsg(string msg) {         try {             out = new datagrampacket(msg.getbytes(), msg.length(), host, port);             sckt.send(out);         } catch (ioexception e) {             e.printstacktrace();         }     }      private static void connect() {         scanner sc = new scanner(system.in);          try {             buffer = new byte[256];             sckt = new datagramsocket();             string reply = "";             {                 system.out.println("name: ");                 nick = sc.nextline();                 sendmsg(nick + " /connect");                 in = new datagrampacket(buffer, buffer.length);                 sckt.receive(in);                 reply = new string(in.getdata(), 0, in.getlength());                 system.out.println("server> " + reply);             } while (!reply.equals("connected!"));             accessserver();         } catch (ioexception e) {             e.printstacktrace();         }     }      private static void accessserver() {         try {             sckt = new datagramsocket();             scanner sc = new scanner(system.in);             string msg = "", reply = "";             thread myt = new thread(new messagelistener());             myt.start();             {                 system.out.print("enter message: ");                 msg = sc.nextline();                 if (!msg.equals("/quit")) {                     buffer = new byte[256];                      sendmsg(nick + " " + msg);                   } else {                     sendmsg(nick + " /disconnect");                 }             } while (!msg.equals("/quit"));              sc.close();         } catch (ioexception e) {             e.printstacktrace();         } {             system.out.println("connection closing...");             sckt.close();         }      } } 

i want able send message single user or users problem can't receive message on client because stands client receives message after sends 1 server. thought use thread continuosly listens messages server doesn't work. what's easiest way go ?

you attempting use received datagram immediately, instead of waiting thread created receive datagram , populate in variable. either handle received datagram directly in thread's code, or signal main thread when datagram received using wait() / notify() scheme.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -