--- Socket originated from UNIX, a network communication interface under the UNIX operating system developed by Berkeley, California. With the widespread use of UNIX operating systems, Socket is also well-deserved to become one of the most popular network communication program interfaces. In the early 1990s, several companies such as Sun Microstems, JSB.CO, FTP Software, Microdyne and Microsoft have developed a set of Windows lower jacket programming, called the Windows Sockets specification, initially realized the Sockets interface under Windows. The procedures were standardized, and in 1992, the Windows Sockets V1.0 was launched. The year issued its version 2.0. The TCP / IP V2.1 for DOS issued by IBM is its representative, which also provides a Windows Sockets application interface. (API). Microsoft's Windows Sockets API is a web application interface under Windows. In order to apply for message mechanisms under Windows and asynchronous I / O selection operations, the Windows Sockets API is functionally expanded to have nearly 20 functions, with the expansional part of the crown. It fully embodies the advantages of WidNows with the prefix WSA (Windows Sockets asynchronous), such as WSAStartup, WSACLEAN, etc. ---- In addition, the Windows Sockets API has 16 and 32-bit editions. The 16-bit version is a single process, and the 32-bit version provides a multi-threaded security. ---- This article will focus on the mechanism of Sockets programming under Windows, designed to throw bricks. One and explain the importance of Windows Sockets programming! I. Introduction to Windows Sockets ---- Socket is the basic component of network communication, providing endpoints for bidirectional communication between different hosting inter-hosts, like a phone, only when one party dials the other party, both parties can establish a conversation, and set The terminal is just more than the phone. Programming by Sockets, the program can skip the complex network underlying protocols and structures, directly prepare applications that are unrelated to the platform. As the Internet is widely used, Sockets has gradually become a general interface for network programming. ---- Socket exists in its specific communication domain (ie, address family), only the sockets belonging to the same address family can establish a conversation, Windows Sockets v1.0 only supports the Internet domain (AF_Inet), all The process of using the internet protocol cluster is suitable for this domain. Under normal circumstances unless communication protocol support, only the same type of socket can pass data, Windows Sockets V1.1, mainly support two types of sockets: stream socket and data report sets, One is the original socket, but in order to ensure the compatibility of the web application, it is generally not encouraged to use the original socket. ---- Flow socket (SOCK_STREAM): This type of socket provides connection, reliable, data error-free and repetitive data transmission services. Moreover, the transmitted data is received in order. All data transmitted by this socket is considered as a continuous byte stream and there is no length limit. This is very applicable to the stability, correctness, and transmission / acceptance order of the data. It is very suitable for the TCP protocol to use this class interface. However, its occupation of the line is relatively improved. The implementation of flow sockets is not uncommon, such as a Telnet, File Transfer Protocol (FTP), etc. use stream sockets.
---- Data report (SOCK_DGRAM): Data report-based socket provides a connectionless service, which sends data in the form of a separate packet (packet length cannot be greater than 32KB), does not provide correctness check The sequence of transmission of each packet is also not guaranteed, so the phenomenon of retransmission, loss, etc. may occur, and the sequence of reception is determined by the specific route. However, the implementation of the datagram is lower to network line usage. NFS (Network File System) is to use this type of socket, in the TCP / IP protocol, UDP (User DataGram Protocol) uses this class interface. ---- Original Socket (SOCK_RAW): This socket generally does not appear in the implementation of the advanced network interface, because it is directly accessed directly to the lower layer of the protocol (such as IP, TCP, UDP, etc.) directly . Commonly used to check new protocols or access new devices configured in existing services, as mentioned earlier, generally do not advocate his direct application. ---- Because Windows Sockets is mainly for the C / S architecture, that is, the customer issues a request to the server, the server can only provide the corresponding service after receiving the request. Both parties must establish half-correlations that have been used for inter-network process communication before establishing a dialogue, and a three-component group (protocol, local address, local port), but only two sides independently The semi-related relationship cannot be established. A complete network communication process must be implemented by a complete full-related unique determination consisting of two independent processes, and only two nature of the same half-correlation can establish a complete full-related five-way group- (Agreement, local address, local port, remote address, remote port), thereby establish an example of a network process communication. ---- Let's explain the process of establishing a network process between the Windows Sockets by the active request of the C / S mode. ---- First start the server side to provide the appropriate service: ---- (1) Open a communication channel and notify the network: This machine will wait for the customer (Client) on a recognized port; ---- (2) The server enters the blocked waiting state, waiting for the arrival of the customer request; ---- (3) When the server receives a client's connection request, activate a new process to process the customer request and establish a C / S conversation. After the service is complete, turn off the new process and the household communication link and terminate it; ---- (4) Returns step (2), waiting for another customer request; ---- (5) Turn off the server. ---- Client Process: ---- (1) Specify the retention port number of the server that wants to establish the connection to the server; ---- (2) Send the Connect request to the server and wait for the server to answer; - (3) After receiving the response of the server to establish a connection, accept the server corresponding service; ---- (4) Turn off the communication channel after the service request is completed. --- This shows that the process of connecting the connectionless non-repeating socket system established with Windows Sockets is shown below: ---- Flow socket first created by the customer with a server that is listening to a port Data transfer can be performed after the connection is duplex. Unlike this, the communication process between network processes implemented with Data News Agreement is as follows:
---- Windows Sockets programming can also be used for remote encruscation (RPC), which makes the user's process can be adjusted to the remote control, and bring the way to the remote control and the distribution pipeline. In the peer network, it is also used in a wide pan, such as Windows online whiteboard (Chat) and Red Heart Battle, is a network connection to the network connection of Windows Sockets. The current Multi-network tour of now is direct use of Windows Sockets. It can be said that Windows Sockets is a network tour 2, Windows Sockets programming ---- 1, Microsoft Visual Basic Reality ---- Microsoft Visual Basic provides available controls for Windows Sockets - Winsock controls. The control is a path to the user to access the TCP and UDP networks. And suitable for many visualized environments such as Microsoft Access, Visual Basic, Visual C , and Visual FoxPro. The C / S program is made through the WINSOCK control, and the program is not required to solve the Detail of TCP or low Winsock APIS calls. If the user does not have to consider the network-by-order sequence and the sequence of this sequence can be directly transferred to the data. Use this control to realize the network of the network. ---- In TCP applications, it is a servor side of the Internet connection instance (INSTANCE), and only the local service fault is required, then the servor is adjusted by the servers. The client corresponding to this should not only set the Winsock's property Remotehost to the server's name (IP address or network code), but also set the port number (Remoteport) of the corresponding service listening to the server, such as the FTP service at the 21st port. HTTP is at the port of 81, etc. Then the post-adjust method Winsock.Connect issues requests to the server. When the server receives the customer request, the event connectionRequest will be triggered. If the service server is willing to provide services, the Accept method can be adjusted to connect. ---- Once connected to the establishment, both ends can be sent or received by sendData or getData to the data. The event DataArrival will be triggered when the other end data is prepared. ---- The implementation of the UDP protocol and TCP is that the two ends of the calling sockets can be transmitted to the data. Therefore, a UDP should be used in the same time to operate the role of the server or the guest. ---- Basic framework for the Visual Basic Windows Sockets programming. 'Service server
Private sub command1.click ()
'Setting the local service port number Winsock1.localport = 2048
'Server enters the supervision state
Winsock1.listen
End Sub
Private sub winsock1_connectionRequest (ByVal Requestid As Long)
'Receive customers to connect requests
'Check Socket status
IF Winsock1.State <> SCKCLOSED THEN WINSOCK1.CLOSE
'Please ask for a customer
Winsock1.accept Requestid
End Sub
Private sub winsock1_dataarrival (byval bytestotal as long)
'The alone is already ready
'Available GetData () / senddata () receive / send data
'Putting customer data
. . . . . .
End Sub
Private Sub Form_Unload (Cancel AS Integer)
Winsock1.close
End Sub
' client
Private sub command1.click ()
'Setting the server network name
Winsock1.Remotehost = "193.168.1.40"
'Set the server to serve the port number
Winsock1.remotEport = 2048
'Send a connection to servers
Winsock1.connect
End Sub
Private suwinsock1_connect ()
'Server response connection
'Can be taken with getData () / senddata () to enter data transmission
. . . . . .
End Sub
Private sub flow1.unload ()
Winsock1.close
End Sub