Google Search

Sunday, October 30, 2011




Receiving Datagrams

The 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], 512);
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 Datagrams

Sending 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);
InetAddress getLocalHost();
Getting the local host is useful for informational purposes, but only the first two methods are actually used to send packets. Both getByName() and getAllByName() require the name of the destination host. The first method merely returns the first match it finds. The second method is needed because a computer can have more than one address. When this occurs, the computer is said to be multi-homed. The computer has one name, but multiple ways to reach it.
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:
  1. Create the datagram socket on a specific port.
  2. Call receive() to wait for incoming packets.
  3. Respond to received packets according to the agreed protocol.
  4. Go back to step 2 or continue to step 5.
  5. Close the datagram socket.

Datagram Clients

The corresponding client uses the same process with one exception: A client must initiate the conversation. The basic recipe for datagram clients is as follows:
  1. Create the datagram socket on any available port.
  2. Create the address to send to.
  3. Send the data according to the server's protocol.
  4. Wait for incoming data.
  5. Go to step 3 (send more data), step 4 (wait for incoming data), or step 6 (exit).
Close the datagram socket

D:\J2SDK\BIN>java Echodatagram_Server
Server is wating for conection…



D:\J2SDK\BIN>javac Echodatagram_Client
D:\J2SDK\BIN>java Echodatagram_Client
Enter data
Data send.
Client receive: sikkim



Post a Comment