Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


You first build a string (theStringToSend) that contains the string you want to send. Then you create a byte array that is as long as your string. You use the String class’s length() method to do this. The PORT constant is used to store the port number, 6969.

The getBytes() instance method of the java.lang.String class converts strings into byte array form. You store this in your byte array theByteArray.

The getByName() method of InetAddress converts a string into an Internet address in the form that the DatagramPacket constructor accepts. In this case the string is a simple one: localhost.

Next, an instance of DatagramSocket is constructed with no arguments, meaning that Java will attempt to use any available port. Finally, the packet is sent using the send() method of the DatagramSocket.

Receiving a Datagram Packet

The packet is on its way, so it is time to receive it. Listing 40.5 shows the receiving program.

Listing 40.5 DatagramReceive.javaA Prototype Program to Receive a Datagram Packet


import java.net.*;

public class DatagramReceive {
  static final int PORT = 6969;
  public static void main( String args[] ) throws Exception {
    String theReceiveString;
    byte[] theReceiveBuffer = new byte[ 2048 ];

    // Make a packet to receive into...
    DatagramPacket theReceivePacket =
      new DatagramPacket( theReceiveBuffer, theReceiveBuffer.length );

    // Make a socket to listen on...
    DatagramSocket theReceiveSocket = new DatagramSocket( PORT );

    // Receive a packet...
    theReceiveSocket.receive( theReceivePacket );

    // Convert the packet to a string...
    theReceiveString =
      new String( theReceiveBuffer, 0, theReceivePacket.getLength() );

    // Print out the string...
    System.out.println( theReceiveString );

    //  Close the socket...
    theReceiveSocket.close();
    }
}

The DatagramReceive class, like the DatagramSend class, uses the DatagramSocket and DatagramPacket classes from java.net. First, make a buffer large enough to hold the message. The buffer in this example (theReceiveBuffer) is a 2K byte array. Your buffer size may vary. Just make sure it will hold the largest packet you will receive.

You then make a new datagram packet. Note that the receive program already knows its IP address and port, and so it can use the two-argument form of the constructor. The new DatagramSocket is set up to receive data at port 6969.

The receive() method of Datagram receives the packet as a byte array. The String (theReceiveString) is constructed out of the byte array and it is displayed on the user’s screen. Finally, the socket is closed—a good practice, freeing memory rather than waiting for Java’s garbage collection.


To alternatively get the IP address of the host you are running on, call the getLocalHost() and getAddress() methods of the class java.net.InetAddress. First, getLocalHost() returns an InetAddress object. Then you use the getAddress() method, which returns a byte array consisting of the four bytes of the IP address. Here’s an example:
InetAddress theLocalIPAddress = InetAddress.getLocalHost();
byte[] theLocalIPAddressDecoded = theLocalIPAddress.getAddress();
If the IP address of the machine on which your program is running on is 221.111.112.23, then
theLocalIPAddressDecoded[0] = 221
theLocalIPAddressDecoded[1] = 111
theLocalIPAddressDecoded[2] = 112
theLocalIPAddressDecoded[3] = 23

Customized Network Solutions

The Internet provides no transactional security whatsoever, nor do the socket-oriented communications methods discussed so far verify whether any particular request for reading or writing data is coming from a source that should have such access. To do this, you need a customized network protocol.

These protocols sit as a layer between your network protocol (that is, TCP/IP) and your application. They encrypt outgoing packets and decrypt incoming packets while verifying that you are still talking to whom you think you are talking.

Netscape has proposed the Secure Sockets Layer (SSL) protocol. SSL is a protocol that resides between the services—such as Telnet, FTP, and HTTP—and the TCP/IP connection sessions that are illustrated in this chapter. SSL would check that the client and server networks are valid, provide data encryption, and ensure that the message does not contain any embedded commands or programs. SSL would thus provide for secure transactions to occur across the Internet.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.