Java FAQ _01 Basic concept (012) _socket What is

Source: Internet
Author: User

Click to enter _ more _java thousand ask

1. What is socket

The socket is also called a socket. The two applications on the network realize the exchange of data through a two-way communication connection, one end of the bidirectional link is called a socket, so the socket is paired. Sockets are typically used to connect clients and service parties, and a socket is uniquely determined by an IP address and a port number.

Socket is a very popular solution of TCP/IP protocol, and is the basic operating unit of network communication supporting TCP/IP protocol. Socket supports not only TCP/IP, but in the Java environment, socket programming refers to the network programming based on TCP/IP protocol.

See the TCP/IP protocol here: [What is the TCP/IP protocol][2]

The socket, like its English intent, is a socket that runs multiple applications on a network and offers a variety of services. Each service opens a socket and binds to a port, and the different ports correspond to different services. This host is like a room full of various sockets, each outlet has a number, different sockets to provide different services: some provide 220 volts AC, some provide broadband network, and some provide cable TV programs. Customer software plug into different numbered sockets, you can get different services.

2. How to communicate with sockets

The socket steps are as follows: The server listens to a port for connection requests, the client sends a connection request to the server side, and if the server has heard the client's request, it sends back the receiving message to the client side. Such a connection is built up. Both the server side and client side can communicate with each other through methods such as Send,write.

As can be seen from the above description, the connection process between sockets can be divided into three steps:

    1. Server monitoring
      Server-side socket, it does not locate the specific client socket, but is waiting for the status of the connection, real-time monitoring network status.

    2. Client request
      A connection request is made by the client's socket, and the target to connect to is the server-side socket. To do this, the client socket must first describe the socket it is connecting to, indicate the address and port number of the server-side socket, and then make a connection request to the server-side socket.

    3. Connection Confirmation
      When the server-side socket hears the client socket connection request, it responds to the client socket request, establishes a new thread, and sends a description of the server-side socket to the client. Once the client confirms this description, the connection is established. While the server-side socket continues to be in the listening state, it continues to receive connection requests from other client sockets.

For a full-featured socket, the process involves the following four steps:

    1. Create socket
    2. Open the input/out stream connected to the socket
    3. Read/write to socket
    4. Close socket

Socket server is divided into single-threaded sockets, multi-threaded sockets two kinds. As the name implies, a single-threaded socket is connected to only one client at a time, and multi-threaded sockets are capable of connecting to multiple clients at the same time.

Learn how to write a single-threaded socket program look here: [How to write a single-threaded socket program][3]
Learn how to write a multithreaded socket program look here: [How to write multi-threaded socket program][4]

Java FAQ _01 Basic concept (012) _socket What is

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.