1. Socket related concepts
(1) the original English meaning of Socket is "hole" or "Socket". As a process communication mechanism, the latter meaning is usually referred to as "Socket ", it is used to despise IP addresses and ports. It is a communication chain handle (actually used for communication between two programs ).
(2) Socket is very similar to a telephone outlet. Taking a telephone network as an example, the two sides of a telephone call are equivalent to two programs that communicate with each other. The telephone number is the IP address. Before any user calls, first, you need to possess a telephone, which is equivalent to applying for a Socket. At the same time, you need to know the phone number of the other party, which is equivalent to a fixed Socket of the other party and then dialing the call to the other party, which is equivalent to sending a connection request, if the other party is present and idle, pick up the phone microphone, and both parties can make a formal call, which is equivalent to a successful connection. The communication between the two parties is a process of sending a signal to the phone and receiving a signal from the phone, it is equivalent to sending data to and receiving data from the Socket. After the call ends, one party suspends the phone, which is equivalent to closing the Socket and revoking the connection.
(3) There are many such hosts on the Internet. These Hosts generally run multiple service software and provide several services at the same time. Each service opens a Socket and is bound to a port, different ports correspond to different services (applications ).
(4) For example, http uses port 80, ftp uses port 21, and smtp uses port 23.
(5) There are two types of Socket
1) Stream Socket (Stream)
It is a connection-oriented Socket. It is secure but inefficient for connection-oriented TCP Service applications.
2) Datagram Socket (datasync)
It is a connectionless Socket, which corresponds to a connectionless UDP Service application. It is not secure (lost, disordered order, and analysis and re-transmission is required at the receiving end), but it is highly efficient.
2. General Socket Application Mode (server and client)
(1) analysis illustration:
(2) Description:
1) The server welcoming socket starts listening to the port (responsible for listening to the client connection information ).
2) client socket connects to the specified port on the server (responsible for receiving and sending server messages)
3) The server listens to the client connection and creates a connection socket (responsible for communicating with the client)
(3) server Socket (at least two)
1) One is responsible for receiving client connection requests (but is not responsible for communicating with the client)
2) each time a client connection is successfully received, a corresponding Socket is generated on the server.
1) created when receiving the Client Connection
2) create a Socket (responsible for communicating with the client) on the server for each successful client request ).
(4) client Socket
1) client Socket
1) You must specify the server address and port to connect.
2) create a Socket object to initialize a TCP connection to the server.
- Socket Communication Process
(1) server side
1) apply for a Socket.
2) bind it to an IP address and a port.
3) Enable listening and wait for receiving data.
(2) Client
1) apply for a Socket.
2) connect to the server (specifying the IP address and port number)
3) after the server connects to the connection request, a new Socket (port greater than 1024) is generated and the client establishes a connection and communicates. The original listening Socket continues to listen. :
- Socket Constructor
(1) The connection is completed through the constructor.
(2) public Socket (AddressFamily address, SocketType socket, ProtocolType protocol)
1) The AddressFamily Member specifies the addressing scheme for the Socket to resolve the address. For example:
InterNetWork is equivalent to Socket connection using an IP version 4 address.
2) SocketType defines the type of the Socket to be opened.
3) The Socket class uses ProtocolType enumeration to notify Windows Socket API of the requested protocol
For example, mySocket = new Socket (AddressFamily. InterNetWork, SocketType. Stream, ProtocolType. TCP );
(3) Note:
1) define at least one IP address and port number of the remote host to be connected.
2) the port number must be between 1 and 65535, preferably after 1024.
3) the remote host to be connected must be listening to the specified port, that is, you cannot connect to the remote host at will.
For example, IPAddress addr = IPAddress. Parse (10.10.4.200 );
IPEndPoint endp = new IPEndPoint (addr, 10001 );
Server first Bind: serverWelcomeSocket. Bind (endp );
Client Connection: ClientSocket. Connect (endp );
- Socket Method
(1) Related Classes
1) IPAddress class: contains an IP address
2) IPEndPoint class: contains a pair of IP addresses and port numbers
(2) related methods
1) Socket (): Create a Socket.
2) Bind (): Bind a local IP address and port number (IPEndPoint)
3) Listen (): enables the Socket to Listen for incoming connection attempts and specify the capacity of the listener queue.
4) Connect (): Initialize the connection to another Socket.
5) Accept (): receives the connection and returns a new Socket.
6) Send (): output data to the Socket.
7) Receive (): reads data from the Socket.
8) Close (): Close the Socket (destroy the connection)