Home
Username:
Password:
Java 6 Tutorials

Networking / A UDP Client & Server Pt.2




Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.


Learn More

Subtitles of the Movie

The previous lesson described a client program that used a UDP socket to send a text message wrapped in a datagram. This lesson is about a UDP server that receives text in datagram's and responds to what it reads. This is the server, it sits and waits for messages to arrive on its ports and acts according to the commands received. The first job is to create an open datagram socket; on the receiving end it is necessary to specify a port number because the client must know the port number to be able to address the server. The receiving of messages is done in a continuous loop because this server repeatedly receives and responds to messages until it receives a halt message. To receive a UDP message you have to create a datagram to hold it. This datagram message body is a block of 256 bytes, this is a drawback of the protocol, you must make the datagram large enough to hold the largest message you could possibly receive. If the incoming message is larger then the datagram to hold it the message is simply chopped off, so watch the size, its important. You can query the datagram socket and find out the size of the incoming message buffer, but that's not a limit, you can also expand it if you need to. The default size is plenty big enough for this demo. The receive method is called to get a message, this method called blocks, until a message arrives, you can set a timeout on it if you wish, but it normally makes sense for a server to wait forever for input. When the receive method returns a message, you can get its length if you need it and you can get the incoming block of data as an array of bytes and a short historical note here. This interface was developed by C programmers who would simply create a union containing all the possible data formats. In Java the format conversion is a little more complicated because you can't union the byte array with a strut. You will have to do your own data conversion. In this example all the messages are text so the array of bytes is converted to a string object and that message is displayed. The count of the number of message are incremented and the message itself is tested for meaning. If the incoming message is a request for the date a return message of the current date and time is constructed. If the incoming message is a request for a count of the number of messages a return message is constructed holding that count. If the message is a halt command the socket is closed and the server quits running otherwise the incoming message is used to form a question. To construct the outgoing message the string is converted to an array of bytes. To send a respond to the client it is necessary to get the address and the port number from the incoming datagram. A datagram is then constructed containing all the necessary parts and the send message is called to transmit the message to the client. The server program then loops back around to get the other message. Here's how the two programs run, first the server must be started, the server is now waiting for a datagram, let's start out by asking for the date and the returned date is displayed and we can do that as often as we like. We can now get a message count, so far the server has received four messages. If you send a message it doesn't recognize it responds with a question, however if you send a halt message the server stops running. In the next lesson we take a look at some of the IO operations available in Java.

Tutorial Information

Course: Java 6
Author: Arthur Griffith
SKU: 33858
ISBN: 1-934743-59-3
Release Date: 2008-02-29
Duration: 7 hrs / 92 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available