Initial Network Process Communication

Source: Internet
Author: User
Tags server port

It can be said that we do only one thing on the web, using a variety of software to communicate with each other endlessly.

For a stand-alone system, the process has its own unique process number in the system. However, in the network environment, the process number assigned by each host individually cannot uniquely identify the process. For example, host A is assigned to a process number 5, and the number 5th process can exist in machine B, so the phrase "process 5th" is meaningless. and the operating system supports a large number of network protocols, different protocols work in different ways, address format is also different. Therefore, the inter-network process communication also solves the problem of multi-protocol recognition.

For this reason, the TCP/IP protocol establishes the concepts of IP address, port, socket (socket) and so on for inter-network process communication.

(1) IP address

An IP address is a unique identifying address for a machine that is connected to a network. The information can be routed according to the IP choice, thus locating the destination machine. This is like a postman (router) delivering parcels (information) based on the address (IP address) of your home (destination machine ).

(2) port

We know that a host with an IP address can provide many services, such as Web services, FTP services, SMTP services, and so on. For different service requests, the host needs to call different programs for processing. But how does the host know what the service request is? Obviously just knowing the IP address is not enough, because the relationship between the IP address and the network service is a one-to-many relationship. In fact, the "IP address + port number" to distinguish between different services.

A port is essentially an abstract software structure (not understood as a USB slot on a machine), and it includes some data structures and I/O (basic input and output) buffers. The system assigns a unique port number to these data structures and buffers. Different port numbers correspond to programs that provide different services. For example, Port 80th is an HTTP port, and when you want to request an HTTP service for the host, we send the request information to the data structure of port 80th on the specified IP. At this time the host will listen to Port 80th has a service request, automatically invoke the HTTP service process to service.

If the IP address is the address of your house, then the port is the door of your home (the house does have a lot of doors). Different visitors go through different doors, and the bribe-giver must have a backdoor (not to rule out the possibility of a thief turning over the window) . When you hear someone knocking at the back door, hey ...

(3) Socket socket

Sometimes, multiple applications may need to send data to the same interface at the same time. To differentiate between different application processes and connections, many computer operating systems provide an interface called a socket (socket) for applications interacting with the TCP/IP protocol.

Distinguish between the network communication and connection between different application processes, there are 3 main parameters: The destination IP address of the communication, the Transport Layer Protocol (TCP or UDP) used, and the port number used. The socket is intended to be "socket". By combining these 3 parameters with a "socket" socket binding, the application layer can and the transport layer through the socket interface, distinguish the communication from different application processes or network connections, to achieve the concurrent service of data transmission.

A socket can be seen as an endpoint in a two-program communication connection, a bridge between an application and a network driver, a socket created in an application, and a network-driven connection through bindings. Thereafter, the application sends data to the socket, which is sent to the network by the socket Network driver. After the computer receives the data related to the socket-bound IP address and port number from the network, the network driver gives the socket, and the application can extract the received data from the socket, which is how the network application sends and receives the data through the socket.

After understanding the partial concept of inter-network process communication based on TCP/IP protocol, I now want to write a Java version of the C-S communication program. Now that the application needs to make a request to the network through the socket or answer the network request. Naturally, we will create our own sockets for both client and server-side programs.

Fortunately, there is something we want in the JDK--socket and ServerSocket class. Let's take a brief look at these two classes:

The socket class is used for clients that are used to establish network connections. When the connection succeeds, a socket instance is generated at both ends of the application, manipulating the instance to complete the required session.

The ServerSocket class is used on the server side, and he can create a server socket bound to a specific port. When the port content is heard, he creates a socket object for the link.

For a network connection, sockets are equal, and there is no difference, not due to different levels on the server side or on the client. Either the socket or the ServerSocket their work is done through the SocketImpl class and its subclasses.

//client program for sending a "Hello server!" to the server sideImportjava.net.*;ImportJava.io.*; Public classclient{PrivateString ipto= "172.16.129.13";//server-side IP address    Private intport=8189;//server port number to pass         Public voidHello () {Try{                       //creates a stream socket and connects it to the specified port number for the specified IP address. Socket clientsocket=NewSocket (Ipto,port); Try{System.out.println ("Success Connection ..."); //returns the output stream for this socketOutputStream os=Clientsocket.getoutputstream (); PrintWriter PW=NewPrintWriter (OS,true); Pw.println ("Hello server!"); }            finally{clientsocket.close (); }        }Catch(IOException e) {e.printstacktrace (); }    }     Public Static voidMain (string[] args) {Client client=NewClient ();    Client.hello (); }}
//server-side programs for accepting client informationImportjava.net.*;ImportJava.io.*; Public classserver{Private intlisenerport=8189;//Listening Port    Private voidLisener () {Try{                      //Create a server-side socket for binding a specified portServerSocket serversocket=NewServerSocket (Lisenerport); //listens on the bound port for blocking until a valid connection from the client is accepted, and returns a client's socket object instance. Socket socket=serversocket.accept (); Try{                                //creating a socket input streamInputStream is=Socket.getinputstream (); BufferedReader BR=NewBufferedReader (NewInputStreamReader (IS)); //Server-side display greetings from the clientSystem.out.println (Br.readline ()); }            finally{serversocket.close (); }        }Catch(IOException e) {e.printstacktrace (); }    }     Public Static voidMain (string[] args) {Server server=NewServer ();    Server.lisener (); }}

Initial Network Process Communication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.