Note that there are some explanatory texts on larger screens.

plurals
  1. POUDP Sockets send and receive
    primarykey
    data
    text
    <p>I am trying to implement a UDP client server. The client is sending 1032 bytes using the rdt_send function but the server receives this packet only after the client is executed twice. </p> <p>Here is the code for :</p> <p>rdt_send:</p> <pre><code>public void rdt_send(byte[] buffer, Integer mss, DatagramSocket s,Integer seqNum) throws IOException { int i,j,k; byte[] seq_num = new byte[4]; byte[] seqnum_temp = new byte[4]; byte [] checksum = new byte[2]; byte [] udp_field = new BigInteger("0101010101010101",2).toByteArray(); Integer checksum_value = (int)calculateChecksum(buffer); checksum = new BigInteger(checksum_value.toString(), 10).toByteArray(); seqnum_temp = new BigInteger(seqNum.toString(),10).toByteArray(); System.out.println(checksum_value); Integer length_temp = seqnum_temp.length; if (seqnum_temp.length&lt;4) { Integer append = 4 -seqnum_temp.length; for (i=0;i&lt;append;i++) { seq_num[i] = 0; } for (j = append, k=0 ; j&lt;4 &amp;&amp; k&lt;length_temp ; j++,k++) { seq_num[j] = seqnum_temp[k]; } System.out.println(seq_num[0]); } byte[] finalSend = new byte[mss+8]; for (i=0;i&lt;4;i++) { finalSend[i] = seq_num[i]; } for (i=4,k=0;i&lt;6 &amp;&amp; k&lt;2;i++,k++) { finalSend[i] = checksum[k]; } for (i=6,k=0;i&lt;8 &amp;&amp; k&lt;2 ;i++,k++) { finalSend[i] = udp_field[k]; } for (i=8,k=0;i&lt;finalSend.length &amp;&amp; k&lt; buffer.length;i++,k++) { finalSend[i] = buffer[k]; } for (i=0;i&lt;finalSend.length;i++) { System.out.println(finalSend[i]); } System.out.println("Got to final sending stage"); DatagramPacket p = new DatagramPacket (finalSend,finalSend.length,InetAddress.getLocalHost(),7771); s.send(p); System.out.println("Sent datagram"); } </code></pre> <p>client :</p> <pre><code>package simpleFTP; public class SimpleFTPClient { public static void main(String args[]) throws IOException{ Integer seq_num =512; Utils u = new Utils(); DatagramSocket d = new DatagramSocket(); File f =new File("C:/Users/amoolp/Desktop/client3/3333.txt"); FileInputStream fs = new FileInputStream(f); byte[] bytesRead = new byte[1024]; fs.read(bytesRead,0,1024); //DatagramPacket p = new DatagramPacket (bytesRead,bytesRead.length,InetAddress.getLocalHost(),7771); // d.send(p); u.rdt_send(bytesRead,1024,d,seq_num); } } </code></pre> <p>Server :</p> <pre><code>public class Server { public static void main (String args[]) throws IOException { DatagramSocket dg = new DatagramSocket (7771); byte[] buf = new byte[1032]; DatagramPacket packet = new DatagramPacket(buf, buf.length); System.out.println("Waiting to receive packet"); dg.receive(packet); System.out.println("Received packet"); for (int i=0; i&lt;buf.length ; i++) { System.out.println(buf[i]); } } } </code></pre> <p>Any ideas as to why this is happening?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload