Java based on socket programming related knowledge analysis _java

Source: Internet
Author: User

One, two main problems in network programming

One is how to accurately locate the network on one or more hosts, and the other is to find the host after the reliable and efficient data transmission.

In the TCP/IP protocol, the IP layer is mainly responsible for the location of the network host, the routing of Data transmission, the IP address can uniquely determine a host on the Internet.

The TCP layer provides a reliable (TCP) or unreliable (UDP) data transmission mechanism for applications, which is the main object of network programming, and generally does not need to care about how the IP layer handles the data.

At present, the more popular network programming model is client/server (c/s) structure. That is, both sides of the communication wait for the customer to request and respond as a server. The customer applies to the server when the service is needed. The server is always running as a daemon, listening to the network port, once the customer request, will start a service process to ring should customers, while their own continue to monitor the service port, so that later customers can also be timely access to services.

Two types of transport protocols: TCP\UDP

TCP is the abbreviation of Tranfer Control Protocol, is a connection-oriented protocol to ensure reliable transmission. Through the TCP protocol transmission, obtains is a sequential error-free data stream. A connection must be established between the pair of two sockets of the sender and receiver to communicate on the basis of the TCP protocol, and when one socket (usually the server socket) waits for the connection to be made, another socket can require a connection. Once these two sockets are connected, they can transmit data in both directions, and both can send or receive operations.

UDP is the user Datagram protocol abbreviation, is a connectionless protocol, each datagram is a separate information, including the full source address or destination address, it on the network on any possible path to the destination, so can reach the destination, The time to arrive at the destination and the correctness of the content are not guaranteed.

Comparison:

UDP:
1. Full address information is given in each datagram, so there is no need to establish a connection between the sender and the receiver.
2.DP data transmission is limited in size, each transmitted datagram must be limited to 64KB.
3.DP is an unreliable protocol in which datagrams sent by the sender do not necessarily reach the receiver in the same order

TCP:
1. To connect the protocol, in the socket between the data transmission must establish a connection, so in TCP need to connect time.
2.CP Transmission data size limit, once the connection is established, both sides of the socket can be in uniform format transmission of large data.
3.CP is a reliable protocol that ensures that the receiver is fully and correctly getting all the data sent by the sender.
Application:

1, TCP in network communication has a strong vitality, such as remote Connection (Telnet) and file Transfer (FTP) all require indefinite length of data is reliably transmitted. But the reliable transmission is to pay the price, the test of the correctness of the data content must occupy the processing time of the computer and the bandwidth of the network, so the TCP transmission efficiency is not as high as UDP.

2. UDP is simple and requires less monitoring, so it is usually used for client/server applications in decentralized systems with high reliability of LAN. For example, video conferencing system, does not require audio and video data is absolutely correct, as long as the consistency can be guaranteed, in this case, the obvious use of UDP is more reasonable.

Three, Java network programming based on socket

1. What's a socket?

Two programs on the network realize the exchange of data through a two-way communication connection, one end of this two-way link is called a socket. The socket is typically used to implement a connection between the client and the service party. The socket is a very popular programming interface for the TCP/IP protocol, and a socket is uniquely determined by an IP address and a port number.

However, the type of protocol supported by the socket is not only TCP/IP, so there is no connection between the two. In the Java environment, socket programming mainly refers to the network programming based on TCP/IP protocol.

The process of 2.ocket communication

Server-side Listen (listening) whether a port has a connection request, the client sends a connect request to the server side, and the server sends back the Accept (accepted) message to the client side. A connection is established. Both the server end and the client side can communicate with each other by means of send,write.

For a fully functional socket, include the following basic structure, which includes the following four basic steps:

(1) Create socket;

(2) Open the input/out stream connected to the socket;

(3) Read/write the socket according to a certain protocol;

(4) Close the socket.

3. Build socket

Create a socket

Java provides two classes of sockets and ServerSocket in Package java.net, respectively, to represent two-way connected clients and services. This is one of two very good packaged classes and is easy to use. The method is constructed as follows:

Socket (inetaddress address, int port);

Socket (inetaddress address, int port, Boolean stream);

Socket (String host, int prot);

Socket (String host, int prot, Boolean stream);

Socket (SocketImpl impl)

Socket (String host, int port, inetaddress localaddr, int localport)

Socket (inetaddress address, int port, inetaddress localaddr, int localport)

ServerSocket (int port);

ServerSocket (int port, int backlog);

ServerSocket (int port, int backlog, inetaddress bindaddr)

Where address, host, and port are the IP addresses, host names, and port numbers of the other side of the two-way connection, stream indicates whether the socket is a stream socket or datagram Socket,localport the port number of the local host, LOCALADDR, and BINDADDR is the address of the local machine (the ServerSocket host address), Impl is the parent of the socket, which can be used to create serversocket and to create the socket. Count indicates the maximum number of connections that the server can support. For example:

Socket client = new Socket ("127.0.0.1", 8888);

ServerSocket Server = new ServerSocket (8888);

Note that you must be careful when choosing a port. Each port provides a specific service, and only the correct port is given to obtain the appropriate service. The port number for the 0~1023 is reserved for the system, for example, the port number of the HTTP service is the port number of the 80,telnet service is 23, so when we select the port number, it is best to select a number greater than 1023 to prevent conflicts.

If an error occurs when the socket is created, a ioexception is generated and must be processed in the program. So when creating a socket or serversocket, you must catch or throw an exception.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.