Writing UDP Clients and Servers

o write UDP clients and servers, you have to use the DatagramSocket class. UDP is a connectionless protocol, so a UDP server doesn’t have to perform an accept() operation the way a TCP server using ServerSocket has to. Unlike Socket, you do not communicate by writing to an OutputStream and reading from an InputStream. Rather, you send datagrams using the DatagramPacket class. Each packet you receive must have a predefined size and byte buffer. Each packet you send must also have a destination address and port number associated with it. The easiest way to give you the feel for how it all works is to write a sample program. The following program connects to the UDP daytime service on a host and prints the result. The key item to pay attention to is the difference between a send and receive packet. Send packets contain address information, receive packets don’t. The raw byte data in a packet can be accessed with getData() and the length of the actual data in the byte array (as opposed to the length of the array itself), can be obtained with getLength().

import java.io.*;import java.net.*;public class Daytime {  public static final int DAYTIME_PORT = 13;  public static final String getTime(String hostname) throws IOException {    InetAddress host;    DatagramSocket socket = new DatagramSocket();    byte[] dummyData = new byte[1];    byte[] timeData  = new byte[256];    DatagramPacket sendPacket, receivePacket;    host = InetAddress.getByName(hostname);    sendPacket =       new DatagramPacket(dummyData, dummyData.length, host, DAYTIME_PORT);    receivePacket = new DatagramPacket(timeData, timeData.length);    socket.send(sendPacket);    socket.receive(receivePacket);    return      new String(receivePacket.getData(), 0, receivePacket.getLength());  }  public static void main(String[] args) {    String server = "tock.usno.navy.mil";    if(args.length == 1)      server = args[0];    else if(args.length > 1) {      System.err.println("Usage: Daytime [hostname]");      return;    }    try {      // Time should include newline, so we don't use println().      System.out.print(getTime(server));      System.out.flush();     } catch(IOException e) {      e.printStackTrace();      return;    }  }                   }
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may