aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes A client Server application using UDP Sockets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "A client Server application using UDP Sockets" Watch "A client Server application using UDP Sockets" New topic
Author

A client Server application using UDP Sockets

sopranos mamo
Greenhorn

Joined: Oct 26, 2012
Posts: 4
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

    Joined: Oct 26, 2012
    Posts: 4
    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;

    }
     
    Consider Paul's rocket mass heater.
     
    subject: A client Server application using UDP Sockets