java - Udp how to get integer from the server? -


i shall implement udp-based client/server application , im new in world of java. server implements simple request-acknowledge protocol upon udp protocol. received string identifier (command) optional parameters server returns specified result. example "thread" returns random integer number between 1 , 5 , length returns random integer number between 5 , 25.

the questions did implementation right , how can fix problem (length nr between 1 , 5). think problem in server class if statements not checked. there other way send comand server tried here sending strings trying check if statements?

and thats code (server):

public class server {  public static void main(string[] args) throws ioexception, interruptedexception  {      datagramsocket skt;     try {         skt = new datagramsocket(1252);         byte [] buffer = new byte[1000];          while(true){             datagrampacket request = new datagrampacket(buffer, buffer.length);             skt.receive(request);             string arraymsg = new string(request.getdata());             system.out.println(arraymsg);              //check if client asks threadnr             if(arraymsg.equals("thread")){             int threadnumber = (int) (math.random()*5)+1;             string threadnum  = integer.tostring(threadnumber);             byte [] b = threadnum.getbytes();             datagrampacket reply = new datagrampacket(b, b.length, request.getaddress(), request.getport());             skt.send(reply);;}              //check if client asks lengthnr             else if(arraymsg.equals("length")){             int lengthnumber = (int) (math.random()*25)+5;             string lengthnum = integer.tostring(lengthnumber);             byte [] b = lengthnum.getbytes();             datagrampacket reply = new datagrampacket(b,  b.length,request.getaddress(), request.getport());             skt.send(reply);}                }}     catch (socketexception e) {         system.out.println("udp port 9876 occupied.");         system.exit(1);     }}} 

thats client:

public class client {  public static void main(string[] args) throws unknownhostexception, ioexception {      datagramsocket skt = new datagramsocket();     inetaddress host = inetaddress.getbyname("localhost");     int serversocket = 1252;     byte [] senddata = new byte [1000];     byte [] getdata = new byte[1000];      //thread send request     string th = "thread";     senddata = th.getbytes();     datagrampacket requestth = new datagrampacket(senddata, senddata.length, host, serversocket);     skt.send(requestth);      //thread reply     datagrampacket replyth = new datagrampacket ( getdata, getdata.length);     skt.receive(replyth);     string arrayth = new string(replyth.getdata());     int threadnr=integer.parseint(arrayth.trim());          //convert thread string int , trim (whitespace)     system.out.println("thread received " + threadnr);      //length send request     string lg = "length";     senddata = lg.getbytes();     datagrampacket requestlg = new datagrampacket(senddata, senddata.length, host, serversocket);     skt.send(requestlg);      //length reply     datagrampacket replylg = new datagrampacket ( getdata, getdata.length);     skt.receive(replylg);     string arraylg = new string(replyth.getdata());     int lengthnr=integer.parseint(arraylg.trim());          //convert thread string int , trim (whitespace)     system.out.println("length received " + lengthnr);       skt.close(); 

and this: thread received 3 length received 1

(length should between 5 , 25 , in case never bigger 5)

the code parses response first message instead of last, random 1-5. suggest using better names variables , you'll spot problem quicker.


Comments

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -