|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
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 classs 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 PacketThe 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 users screen. Finally, the socket is closeda good practice, freeing memory rather than waiting for Javas garbage collection.
Customized Network SolutionsThe 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 servicessuch as Telnet, FTP, and HTTPand 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.
|
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. |