CLIENT SERVER INTRACTION (DATAGRAM)
Receiving DatagramsThe DatagramPacket class is used to receive and send data over DatagramSocket classes. The packet class contains connection information as well as the data. As was explained earlier, datagrams are self-contained transmission units. The DatagramPacket class encapsulates these units. The following lines receive data from a datagram socket:
DatagramPacket packet = new DatagramPacket(new byte, 512);
clientSocket.receive(packet);The constructor for the packet must know where to place the received data. A 512-byte buffer is created and passed to the constructor as the first parameter. The second constructor parameter is the size of the buffer. Like the accept() method in the ServerSocket class, the receive() method blocks until data is available.
Sending DatagramsSending datagrams is really very simple; all that's needed is a complete address. Addresses are created and tracked using the InetAddress class. This class has no public constructors, but it does contain several static methods that can be used to create an instance of the class. The following list shows the public methods that create InetAddress class instances:
Public InetAddress Creation Methods
InetAddress getByName(String host);
InetAddress getAllByName(String host);
All the creation methods are marked as static. They must be called as follows:
InetAddress addr1 = InetAddress.getByName("localhost");
InetAddress addr2 = InetAddress.getAllByName("localhost");
InetAddress addr3 = InetAddress.getLocalHost();Any of these calls can throw an UnknownHostException. If a computer is not connected to a Domain Name Server (DNS), or if the host is really not found, an exception is thrown. If a computer does not have an active TCP/IP configuration, then getLocalHost() is likely to fail with this exception as well.
Once an address is determined, datagrams can be sent. The following lines transmit a string to a destination socket:
String toSend = "This is the data to send!";
byte sendbuf = new byte[ toSend.length() ];
toSend.getBytes( 0, toSend.length(), sendbuf, 0 );
DatagramPacket sendPacket = new DatagramPacket( sendbuf, sendbuf.length, addr,
clientSocket.send( sendPacket );First, the string must be converted to a byte array. The getBytes() method takes care of the conversion. Then a new DatagramPacket instance must be created. Notice the two extra parameters at the end of the constructor. Because this will be a send packet, the address and port of the destination must also be placed into the packet. An applet may know the address of its server, but how does a server know the address of its client? Remember that a datagram is like an envelope-it has a return address. When any packet is received, the return address can be extracted from the packet by using getAddress() and getPort(). This is how a server would respond to a client packet:
DatagramPacket sendPacket = new DatagramPacket( sendbuf, sendbuf.length,
recvPacket.getAddress(), recvPacket.getPort() );
serverSocket.send( sendPacket );Unlike connection-oriented operation, datagram servers are actually less complicated than the datagram client
The basic script for datagram servers is as follows:
- Create the datagram socket on a specific port.
- Call receive() to wait for incoming packets.
- Respond to received packets according to the agreed protocol.
- Go back to step 2 or continue to step 5.
- Close the datagram socket.
Datagram ClientsThe corresponding client uses the same process with one exception: A client must initiate the conversation. The basic recipe for datagram clients is as follows:
- Create the datagram socket on any available port.
- Create the address to send to.
- Send the data according to the server's protocol.
- Wait for incoming data.
- Go to step 3 (send more data), step 4 (wait for incoming data), or step 6 (exit).
Close the datagram socket
Server is wating for conection…THE SERVER: smit
Client receive: sikkim