Article Source: ASPCN Author: Sun Wen
Java Connection-oriented classes
Sockets has two main modes of operation: connection-oriented and connectionless. Connection-oriented sockets operations are like a phone call, They have to establish a connection and a call to one person. All things arrive in the same order as they set out. A connectionless sockets operation is like a mail delivery, and there is no guarantee that multiple messages may arrive in the same order as they were when they started.
Which model to use is the need of the mail application. If reliability is more important, it's better to use a connection-oriented operation. For example, file servers require the correctness and order of their data. If some data is lost, the effectiveness of the system will be lost. Some servers, For example, send some chunks of data intermittently. If the data is lost, the server does not want to send it again. Because when the data arrives, it may be obsolete. Ensuring that the data is ordered and correct requires additional operational memory consumption, and additional costs will reduce the system's response rate.
Connectionless operations use datagram protocols. A datagram is a self-contained unit that contains all of the information for this post. Think of it as an envelope, it has the destination address and the content to send. The socket in this mode does not need to connect a socket for a purpose, It simply throws out the datagram. Connectionless operations are fast and efficient, but data security is poor.
Connection-oriented operations use the TCP protocol. A socket in this mode must obtain a connection to the socket of the destination before sending the data. Once the connection is established, sockets can use a stream interface: Open-read-write-close. All messages sent are received in the same order at the other end. Connection-oriented operations are less efficient than connectionless operations, but the data is more secure.
Sun has always been a supporter of network building, so it is not surprising to support sockets in Java. In fact, Java reduces the difficulty of building a sockets program. Each transport mode is encapsulated into a different class. The class for the connection will be discussed first.
There are two forms of connection-oriented classes in Java, which are the client and server side. This is the simplest part of the client, so let's talk about it first.
Listing 9.1 lists a simple client-side program. It sends a request to a server, retrieves an HTML document, and displays it on the console.
9.1 A simple socket client
Import java.io.*;
Import java.net.*;
/**
* A simple program to retrieve an HTML page from the server
* Note: Merlin is the name of the local machine
*/
public class Simplewebclient {
public static void Main (String args[])
{
Try
{
Open a Client socket connection
Socket CLIENTSOCKET1 = new socket ("Merlin", 80);
System.out.println ("Client1:" + clientSocket1);
Get a Web page
GetPage (CLIENTSOCKET1);
}
catch (Unknownhostexception Uhe)
{
System.out.println ("unknownhostexception:" + Uhe);
}
catch (IOException IoE)
{
System.err.println ("IOException:" + IoE);
}
}
/**
* Request a page through the established connection, display the response and then close the socket
*/
public static void GetPage (Socket clientsocket)
{
Try
{
Requires input and output streams
DataOutputStream outbound = new DataOutputStream (
Clientsocket.getoutputstream ());
DataInputStream inbound = new DataInputStream (
Clientsocket.getinputstream ());
Making HTTP requests to the server
Outbound.writebytes ("get/http/1.0\r\n\r\n");
read out the response
String Responseline;
while ((Responseline = Inbound.readline ())!= null)
{
Show each line.
System.out.println (Responseline);
if (Responseline.indexof ("")!=-1)
Break
}
Clear
Outbound.close ();
Inbound.close ();
Clientsocket.close ();
}
catch (IOException IoE)
{
System.out.println ("IOException:" + IoE);
}
}
}
Recall that a client sends a connection to a listening server socket. The sockets of the client is established with the socket class. The following program establishes a client socket and connects to a host:
Socket clientsocket = new socket ("Merlin", 80);
The first parameter is the name of the host you want to connect to, and the second parameter is the port number. A host name specifies the name of the destination. The port number specifies which application to receive. In our case, you must specify 80 because it is the port of the default HTTP protocol. Another well-known port is listed in Table 9.1, see:
Well-known end products:
Echo 7
Daytime 13
Daytime 13
FTP 21
Telnet 23
SMTP 25
Finger 79
HTTP 80
POP3 110
Because the socket class is connection-oriented, it provides a stream interface for reading and writing. A class in the Java.io package can be used to access a connected socket:
DataOutputStream outbound = new DataOutputStream (
Clientsocket.getoutputstream ());
DataInputStream inbound = new DataInputStream (Clientsocket.getinputstream ()
);
Once the flow is established, the general flow operation can be done:
Outbound.writebytes ("get/http/1.0\r\n\r\n");
String Responseline;
while ((Responseline = Inbound.readline ())!= null)
{
System.out.println (Responseline);
}
The above applet requests a Web page and displays it. When the program completes, the connection must be closed.
Outbound.close ();
Inbound.close ();
Clientsocket.close ();
Note the socket stream must be closed first. All of the socket streams must be closed before the socket closes. This applet is very simple, but all client programs must follow the basic steps below:
1. Establish a client socket connection.
2. Get the read and write stream of the socket.
3. Use flow.
4. Close the stream.
5. Close the socket.
Using a server-side socket is just a bit more complicated, and it will be described below.