Source: aspcn Author: Sun Wen
Java connection-oriented class
Sockets has two main operations: connection-oriented and connectionless. connection-oriented sockets operations are like a phone. They must establish a connection and a person call. the order of all things at arrival is the same as that at their departure. the connectionless sockets operation is like a mail delivery, with no guarantee. The order of arrival of multiple mails may be different from that of departure.
Which mode is used depends on the mail application. if reliability is more important, connection-oriented operations will be better. for example, the file server needs the correctness and orderliness of their data. if some data is lost, the system's validity will be lost. some servers, such as sending some data blocks intermittently. if the data is lost, the server does not want to resend it again. because when the data arrives, it may be out of date. to ensure data orderliness and correctness requires additional memory consumption for operations, and additional costs will reduce the system response rate.
Connectionless operations use the datagram protocol. A datagram is an independent unit that contains all the information about this delivery. think of it as an envelope with a destination address and content to be sent. in this mode, the socket does not need to connect to a destination socket, but simply outputs a datagram. connectionless operations are fast and efficient, but data security is poor.
Connection-oriented operations use the TCP protocol. in this mode, the socket must connect to the destination socket before sending data. once the connection is established, sockets can use a stream interface: Open-read-write-close. all sent messages will be received at the other end in the same order. connection-oriented operations are more efficient than non-connection operations, but data security is higher.
SUN has always been a supporter of network construction, so it is not surprising to support sockets in Java. in fact, Java reduces the difficulty of creating a sockets program. each transmission mode is encapsulated in different classes. connection-oriented classes will be discussed first.
In Java, connection-oriented classes have two forms: the client and the server. The client is the simplest part, so we will discuss it first.
List 9.1 lists a simple client 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 for retrieving 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 );
// Obtain a webpage
GetPage (clientSocket1 );
}
Catch (UnknownHostException uhe)
{
System. out. println ("UnknownHostException:" + uhe );
}
Catch (IOException ioe)
{
System. err. println ("IOException:" + ioe );
}
}
/**
* A page is requested by establishing a connection. The response is displayed and the socket is closed.
*/
Public static void getPage (Socket clientSocket)
{
Try
{
// The input and output streams are required.
DataOutputStream outbound = new DataOutputStream (
ClientSocket. getOutputStream ());
DataInputStream inbound = new DataInputStream (
ClientSocket. getInputStream ());
// Send an HTTP request to the server
Outbound. writeBytes ("GET/HTTP/1.0 \ r \ n ");
// Read the response
String responseLine;
While (responseLine = inbound. readLine ())! = Null)
{
// Display each row
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 server socket being monitored. the client's sockets is created using 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 target name. the port number specifies the application that receives the message. in our case, we must specify 80 because it is the default HTTP port. the other well-known ports are listed in table 9.1:
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. classes 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 a stream is created, a general stream operation can be performed:
Outbound. writeBytes ("GET/HTTP/1.0 \ r \ n );
String responseLine;
While (responseLine = inbound. readLine ())! = Null)
{
System. out. println (responseLine );
}
The above applet requests a WEB page and displays it. After the program is completed, the connection must be closed.
Outbound. close ();
Inbound. close ();
ClientSocket. close ();
Note that the socket stream must be closed first. All socket streams must be closed before the socket is closed. 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 streams of the socket.
3. Use stream.
4. Close the stream.
5. Disable socket.
Using a server-side socket is a little complicated. It will be discussed below.