java - Client-Server UDP connection -
i trying send data server client 1 bit @ time. client should respond ack whenever received bit server.
what happening when client sends initial requests server, data getting passed server. when server sending required data client, data getting looped , client left in indefinite waiting hang.
i've attached code below client , server. please have @ , advise on going wrong.
client side:
import java.io.ioexception; import java.net.*; import java.util.scanner; class udpclient extends thread implements runnable { datagramsocket clientsocket; inetaddress ipaddress; public static void main(string args[]) throws exception { try { udpclient t1 = new udpclient(); thread t = new thread(t1); t.start(); } catch (exception e) { system.out.println("error!"); } } public udpclient() throws exception { this.clientsocket = new datagramsocket(); this.ipaddress = inetaddress.getbyname("localhost"); clientsocket.setsotimeout(5000); } public void run() { byte[] senddata = new byte[1024]; byte[] receivedata = new byte[1024]; string m ="1001"; string checksum = "1111"; string checksumsend = ""; string sentence=""; string sentence1=""; string datarequired=""; try { datarequired = datarequired.concat(m+":data"); senddata = datarequired.getbytes(); datagrampacket sendpacket = new datagrampacket(senddata, senddata.length, ipaddress, 9876); clientsocket.send(sendpacket); checksumsend = checksumsend.concat(checksum+":checksum"); senddata = checksumsend.getbytes(); datagrampacket sendpacket2 = new datagrampacket(senddata, senddata.length, ipaddress, 9876); clientsocket.send(sendpacket2); scanner in = new scanner(system.in); system.out.println("enter window size: "); int s = in.nextint(); string winsize=""; winsize = winsize.concat(s+":windowsize"); senddata = winsize.getbytes(); datagrampacket sendpacket3 = new datagrampacket(senddata, senddata.length, ipaddress, 9876); clientsocket.send(sendpacket3); string finished="finished"; senddata = finished.getbytes(); datagrampacket sendpacket6 = new datagrampacket(senddata, senddata.length, ipaddress, 9876); clientsocket.send(sendpacket6); { try { datagrampacket receivepacket = new datagrampacket(receivedata, receivedata.length); system.out.println("i getting executed"); clientsocket.receive(receivepacket); //after step, nothing gets executed sentence = new string(receivepacket.getdata(),0,receivepacket.getlength()); system.out.println("received server: " + sentence); if(receivepacket != null) sentence1 = "ack"; senddata = sentence1.getbytes(); datagrampacket sendpacket4 = new datagrampacket(senddata, senddata.length, ipaddress, 9876); clientsocket.send(sendpacket4); } catch (sockettimeoutexception a) { system.out.println("timed out!"); } }while(sentence!=null); } catch (ioexception e) { system.err.println(e); } { clientsocket.close(); } } }
server side:
import java.io.ioexception; import java.net.*; public class udpserver extends thread implements runnable { datagramsocket serversocket; public static void main(string args[]) throws exception { try { udpserver t1 = new udpserver(); thread t = new thread(t1); t.start(); } catch (exception e) { system.out.println("error!"); } } public udpserver() throws exception { this.serversocket = new datagramsocket(9876); } public void run() { byte[] receivedata = new byte[1024]; byte[] senddata = new byte[1024]; string checksum=""; string data = ""; string winsize = ""; string carry = "0"; string output=""; string n,o; string output1; string sentence1=""; int i,j=0; while(true) { string sentence=""; try { datagrampacket receivepacket = new datagrampacket(receivedata, receivedata.length); serversocket.receive(receivepacket); inetaddress ipaddress = receivepacket.getaddress(); sentence = new string( receivepacket.getdata(),0,receivepacket.getlength()); if(sentence.contains("checksum")) { int len = sentence.length(); for(i=0;i<len;i++) { if(sentence.charat(i)==':') { checksum = sentence.substring(0,i); } } system.out.println("checksum specified client is: " + checksum); } else if(sentence.contains("data")) { int len = sentence.length(); for(i=0;i<len;i++) { if(sentence.charat(i)==':') { data = sentence.substring(0,i); } } system.out.println("data requested client is: " + data); } else if(sentence.contains("windowsize")) { int len = sentence.length(); for(i=0;i<len;i++) { if(sentence.charat(i)==':') { winsize = sentence.substring(0,i); } } system.out.println("window size is: " + winsize); } else if(sentence.contains("finished")) { output1 = checksumadd(carry,data,checksum); output = reverse(output1); system.out.println("checksum addition before complementing digits = "+output); output = complement(output); output = reverse(output); system.out.println("checksum addition after complementing digits = "+output); int windowsize = integer.parseint(winsize); int strlen = data.length(); { for(i=j;i<(windowsize+j);i++) { if(i!=strlen) { string send = ""; n = data.substring(i,i+1); system.out.println("value of n is: "+n); send = send.concat(n+":"); o = output.substring(i,i+1); system.out.println("value of o is: "+o); send = send.concat(o); senddata = send.getbytes(); datagrampacket sendpacket1 = new datagrampacket(senddata, senddata.length, ipaddress, 9876); serversocket.send(sendpacket1); } else break; } j+=windowsize; datagrampacket receivepacket2 = new datagrampacket(receivedata, receivedata.length); serversocket.receive(receivepacket2); sentence1 = new string( receivepacket2.getdata(),0,receivepacket2.getlength()); system.out.println("sentence 1 is: "+sentence1); //to removed. used testing purposes }while(i!=strlen); } } catch (ioexception e) { system.err.println(e); } } } public static string complement(string output) { string temp=""; int len = output.length(); for(int i=len;i>0;i--) { string t = output.substring(i-1,i); if(t.equals("0")) temp = temp.concat("1"); else if(t.equals("1")) temp = temp.concat("0"); } return temp; } public static string reverse(string output) { string temp=""; int len = output.length(); for(int i=len;i>0;i--) { string t = output.substring(i-1,i); temp = temp.concat(t); } return temp; } public static string checksumadd(string carry, string data,string checksum) { int strlen = data.length(); int flag=0; string output=""; string output2=""; string n,che; string sum = null; for(int i=strlen;i>0;i--) { n=data.substring(i-1,i); che = checksum.substring(i-1,i); if(n.equals("0") && che.equals("0") && carry.equals("0")) { sum = "0"; carry = "0"; } else if(n.equals("0") && che.equals("0") && carry.equals("1")) { sum = "1"; carry = "0"; } else if(n.equals("0") && che.equals("1") && carry.equals("0")) { sum = "1"; carry = "0"; } else if(n.equals("0") && che.equals("1") && carry.equals("1")) { sum = "0"; carry = "1"; } else if(n.equals("1") && che.equals("0") && carry.equals("0")) { sum = "1"; carry = "0"; } else if(n.equals("1") && che.equals("0") && carry.equals("1")) { sum = "0"; carry = "1"; } else if(n.equals("1") && che.equals("1") && carry.equals("0")) { sum = "0"; carry = "1"; } else if(n.equals("1") && che.equals("1") && carry.equals("1")) { sum = "1"; carry = "1"; } output = output.concat(sum); } if(carry.equals("1")) { n = output.substring(0,1); if(n.equals("0")) { sum = "1"; carry = "0"; } else if(n.equals("1")) { sum = "0"; carry = "1"; } output2 = output2.concat(sum); for(int i=strlen-1;i>0;i--) { n=data.substring(i-1,i); if(n.equals("0") && carry.equals("0")) { sum = "0"; carry = "0"; } else if(n.equals("0") && carry.equals("1")) { sum = "1"; carry = "0"; } else if(n.equals("1") && carry.equals("0")) { sum = "1"; carry = "0"; } else if(n.equals("1") && carry.equals("1")) { sum = "0"; carry = "1"; } output2 = output2.concat(sum); } flag = 1; } if (flag==1) return output2; return output; } }
if client doesn't receive response server, it's because server doesn't send response.
serversocket.send(sendpacket1); called if(i!=strlen)
have tried output text in console inside if block see if/when enter loop?
Comments
Post a Comment