Warning: Invalid argument supplied for foreach() in /home/sysbash/domains/systembash.com/public_html/wp-content/plugins/topsy/topsy.php on line 1199
A Simple Java UDP Server and UDP Client – systemBash

A Simple Java UDP Server and UDP Client

For a class I am taking, we are testing out a simple UDP Server and UDP Client to demonstrate what each one does and how sockets work. The code size is very small and give you a good idea about how a UDP Server opens up a port, and then the UDP Client sends or receives data from that port.

To compile these, install Java JDK to your system. Then compile the program with “javac UDPClient.java” – this will create a UDPClient.class. Execute the file with “java UDPClass” – leave off the .class, or you will get the error: “Exception in thread “main” java.lang.NoClassDefFoundError”.

Here is sample code for a simple Java UCP Server and Client, originally from Computer Networking: A Top Down Approach, by Kurose and Ross:

UDPServer.java:

import java.io.*;
import java.net.*;

class UDPServer
{
   public static void main(String args[]) throws Exception
      {
         DatagramSocket serverSocket = new DatagramSocket(9876);
            byte[] receiveData = new byte[1024];
            byte[] sendData = new byte[1024];
            while(true)
               {
                  DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                  serverSocket.receive(receivePacket);
                  String sentence = new String( receivePacket.getData());
                  System.out.println("RECEIVED: " + sentence);
                  InetAddress IPAddress = receivePacket.getAddress();
                  int port = receivePacket.getPort();
                  String capitalizedSentence = sentence.toUpperCase();
                  sendData = capitalizedSentence.getBytes();
                  DatagramPacket sendPacket =
                  new DatagramPacket(sendData, sendData.length, IPAddress, port);
                  serverSocket.send(sendPacket);
               }
      }
}

UDPClient.java:

import java.io.*;
import java.net.*;

class UDPClient
{
   public static void main(String args[]) throws Exception
   {
      BufferedReader inFromUser =
         new BufferedReader(new InputStreamReader(System.in));
      DatagramSocket clientSocket = new DatagramSocket();
      InetAddress IPAddress = InetAddress.getByName("localhost");
      byte[] sendData = new byte[1024];
      byte[] receiveData = new byte[1024];
      String sentence = inFromUser.readLine();
      sendData = sentence.getBytes();
      DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
      clientSocket.send(sendPacket);
      DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
      clientSocket.receive(receivePacket);
      String modifiedSentence = new String(receivePacket.getData());
      System.out.println("FROM SERVER:" + modifiedSentence);
      clientSocket.close();
   }
}
Share and Enjoy:
  • Twitter
  • del.icio.us
  • Digg
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Print
  • StumbleUpon

5 Trackbacks

You can leave a trackback using this URL: http://systembash.com/content/a-simple-java-udp-server-and-udp-client/trackback/

  1. By Dave Drager on September 17, 2008 at 10:58 am

    New blog post: A Simple Java UDP Server and UDP Client http://tinyurl.com/69ukrl

  2. By systemBash » A Simple Java TCP Server and TCP Client on September 18, 2008 at 9:01 am

    [...] up on my previous post, we also had to demonstrate a sample Java TCP Server and TCP Client. They are pretty small and give [...]

  3. By Matt Hollander on January 24, 2010 at 12:33 pm

    Just found the site where Freddy got his Client/Server http://is.gd/6VLfk #DurhamCS

  4. By Dave Drager on March 29, 2010 at 2:13 pm

    New Post: A Simple Java UDP Server and UDP Client http://bit.ly/4AwQjS

  5. By train_boy on July 29, 2010 at 5:20 am

    http://tinyurl.com/69ukrl
    A Simple Java UDP Server and UDP Client – systemBash

12 Comments

  1. jomai

    from the Computer Networking: A top Down Approach by Kurose

    Posted January 8, 2009 at 5:01 am | Permalink
  2. terry

    how to execute

    Posted August 10, 2009 at 6:53 am | Permalink
  3. jeya

    how to execute

    Posted September 7, 2009 at 6:35 am | Permalink
  4. Me

    > javac UDPClient.java UDPServer.java
    Open two command prompt windows and write in one:
    > java UDPServer
    and after this, write on another:
    > java UDPClient

    Posted October 5, 2009 at 6:46 am | Permalink
  5. edgar

    In the server, you would like to take into account the size of the received package:

    String sentence = new String(receivePacket.getData(), 0, receivePacket.getLength());

    Posted October 13, 2009 at 5:13 am | Permalink
  6. Anonymous

    its nt working on two different computer…what should i do

    Posted November 11, 2009 at 3:35 am | Permalink
  7. Nitin Verma

    Client is sending the packets to localhost, use the IP Adress or name of the m/c you are running server on.

    InetAddress IPAddress = InetAddress.getByName(“localhost”);

    Posted November 19, 2009 at 11:22 am | Permalink
  8. Craig

    How do you set the UDP port on which the server is listening?

    Posted February 8, 2010 at 3:11 pm | Permalink
  9. Craig

    Ok I found it. It’s 9876. Thanks

    Posted February 8, 2010 at 3:14 pm | Permalink
  10. vivek

    Good prgram for passing string,number etc between client &server

    Posted February 23, 2010 at 5:49 am | Permalink
  11. Vaibhav

    THANK U SO MUCH…its so much simple and easy to understand…bcoz of this i completed my project successfully…Secure TFTP with Multithreaded Server my project title…i used some code of this in my project…Again THANK U….

    Posted June 6, 2010 at 11:10 am | Permalink
  12. Revathi

    hi i want file transfer program using udp protocol in java

    Posted July 29, 2010 at 2:45 am | Permalink

Post a Comment

Your email is never shared. Required fields are marked *

*
*