1 TCP Sockets Basics
Sockets is a programmatic abstraction that is a handle to the network's communication connection to another application. Sockets programming isolates user code from the underlying implementation of the TCP/IP protocol stack, allowing users the flexibility to implement their own programming.
TCP sockets based on TCP protocol requires four aspects of data:
(1) The IP address of the local system.
(2) The TCP port number that the local application is using.
(3) The IP address of the remote system that communicates.
(4) The TCP port number of the remote system response for the communication.
The application model for TCP sockets is typically the client/server model, where a server waits for a remote computer to request a resource on a specific port, giving a response. The client program binds to this port and establishes a reliable connection for communication.
The connection-oriented operation uses the TCP protocol, in which the socket must obtain a connection to the destination socket before sending the data. Once the connection is established, the socket can use a stream interface: open-read-write-off. All send
Information is received in the same order on the other end. Connection-oriented operations are less efficient than non-connected operations, but the data is more secure. Because the socket is used on both sides, the use of the client and server side is slightly different.
(1) The client requests the socket in the following steps:
① establishing a client socket connection;
② gets the read and write stream of the socket;
③ use flow;
④ closing the stream;
⑤ close the socket.
Using a server-side socket request is a bit trickier than using a client socket request. The server is not actively establishing a connection. Instead, the server passively listens to the client for instructions and then serves them.
(2) The server-side socket to complete the following basic steps:
① set up a server socket and start listening;
② use the Accept () method to obtain a new connection;
③ establish input and output streams;
④ a session on an existing agreement;
⑤ close the client stream and socket;
⑥ back to ② or go to ⑦;
⑦ shut down the server socket.
1.1 InetAddress Class
The InetAddress object represents an Internet host address. This host address can be identified by name and IP address. The name is the hostname, for example, the native name is snowing, the string type, and the IP address is 192.100.100.43, which is a four-byte number and is written in the form of a.b.c.d. Several common methods of the InetAddress class are as follows:
public static inetaddress Getbyname (String host) throws Unknownhostexception can get the IP address of the host by name.
Public String gethostaddress () Returns the string format of the IP address.
Public String GetHostName () returns the IP address Host name.
public static inetaddress Getlocalhost () throws Unknownhostexception returns the native address in the format encapsulated by the InetAddress class.
Public String toString () goes Change to string format.
Example: A test applied to an InetAddress object. Gets the native address and converts it to a string output, outputting the local host name and IP address.
1 Importjava.net.*;2 Public classFindhost3 {4 Public Static voidMain (string[] args)5 {6 Try7 {8InetAddress h =inetaddress.getlocalhost ();9System.out.println ("toString ():" +h.tostring ());TenSystem.out.println ("GetHostName ():" +h.gethostname ()); OneSystem.out.println ("Gethostaddress ():" +h.gethostaddress ()); A } - Catch(unknownhostexception e) - { the System.out.println (E.getmessage ()); - } - } -}View Code
1.2 Socket Class
Socket is the TCP/IP protocol encapsulation, the socket itself is not a protocol, but a call interface (API), through the socket, we can use the TCP/IP protocol. The advent of sockets only makes it easier for programmers to use the TCP/IP protocol stack and
has been an abstraction of the TCP/IP protocol, thus forming some of the most basic function interfaces we know.
The socket class is used to implement the client socket. There are three common ways to construct these:
The public socket () creates a non-connected socket.
The public socket (inetaddress address, int port) creates a streaming socket that connects it to the host and port ports indicated by the Inetdadress class.
The public socket (String host, int port) creates a streaming socket and connects it to a specific port port on a specific host.
1.2.1 Establishes the client program, accesses the URL http://www.xjtu.edu.cn, and returns the first page of the URL to the file xjtu.html.
1. Steps to establish a program
(1) Establish a socket connection to http://www.xjtu.edu.cn with Port 80.
Socket clientsocket = new socket ("www.xjtu.edu.cn", 80);
(2) Initialize the stream of bytes. The data transmission through the connection is realized by the input and output stream, and the writing file is realized by the file input and output stream. The various stream objects are initialized as follows:
DataOutputStream outbound = new DataOutputStream (Clientsocket.getoutputstream ());
DataInputStream inbound = new DataInputStream (Clientsocket.getinputstream ());
InputStreamReader InS = new InputStreamReader (inbound);
File F = new file ("xjtu.html");
FileOutputStream fOut = new FileOutputStream (f);
PrintStream p = new PrintStream (fOut);
(3) Send the request.
Outbound.writebytes ("get/http/1.0\r\n\r\n");
(4) After the data is returned, the file is written in a loop.
int C;
while ((c = Ins.read ())! =-1)
P.print ((char) c);
(5) Close the stream.
Ins.close ();
Outbound.close ();
Inbound.close ();
Clientsocket.close ();
java-Network Technology