Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A client Server application using UDP Sockets

 
sopranos mamo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to write a distributed Java application consisting of a client and a server that communicate over a UDP
socket. The client should send a number of random data bytes to the server, and the server should send
back a status code and the calculated Internet checksum for the received data bytes.
How do I do this?
Thanks for your help!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you'll want to start by familiarizing yourself with:

  • network communications in general and UDP in particular
  • the basics of the Java language
  • network communication in Java, such as the relevant parts of the java.net and java.io packages


  • You'll also want to ShowSomeEffort(←click) and TellTheDetails(←click) of what you tried and what specific problems you had, in order to get the most productive use out of this site.

    Welcome to the Ranch! And good luck!
     
    sopranos mamo
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank a lot bro!!!
    I was able to read UDP sockets and was able to implement it.
    please see the code snippet below, if you have any suggestions let me know
    I did it only for even number of sent bytes

    public static long calculateChecksum(byte[] buf) {
    int length = buf.length;
    if(!(length%2==0))
    {
    System.out.print("The Number of bytes sent is odd!");
    System.exit(0);
    }
    int i = 0;

    long sum = 0;
    long data;


    while (length > 1) {

    data = (((buf[i] << 8) & 0xFF00) | ((buf[i + 1]) & 0xFF));
    sum += data;
    // 1's complement carry bit correction in 16-bits (detecting sign extension)
    if ((sum & 0xFFFF0000) > 0) {
    sum = sum & 0xFFFF;
    sum += 1;
    }

    i += 2;
    length -= 2;
    }
    sum = ~sum;
    sum = sum & 0xFFFF;
    return sum;

    }
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic