CodePudding user response:
Using Winsock controlWinSock control can be used to establish a connection with the remote computer, and through the user data, datagram protocol (UDP) or transmission control protocol (TCP) for data exchange, the two protocols can be used to create a client and server applications, similar to the Timer control, WinSock control is not visible at run time,
Possible USES
Create client applications collect user information, and will collect the information sent to a central server,
Create a server application, as a multiple user data into the point,
Create a "chat" application,
Choose communication protocol
When using WinSock control, you first need to consider what use protocol, you can use the protocols include TCP and UDP, two kinds of protocol is an important difference between their connection status:
TCP protocol control is based on the connected, it can be compared with the telephone system, before starting data transmission, the user must first establish a connection,
UDP is a connectionless protocol, transmission between two computers similar to deliver mail: a message sent from one computer to another computer, but there is no clear connection between, on the other hand, the biggest single transmission data volume depends on the specific network,
What choose which kind of agreement is usually determined by the need to create the application, the following questions will help to select appropriate protocol:
At the time of sending and receiving data, whether the application need confirm from the client or server information? If need be, using TCP protocol, before sending and receiving data to establish a clear connection,
Whether special data (such as images and sound files)? After the connection is established, the TCP protocol connect to maintain and ensure the integrity of the data, however, this connection requires more computing resources, so it is more "expensive",
Data is intermittent, or within a session? For example, if the application needs to notice when a task to complete a computer, the UDP protocol is more appropriate, UDP protocol for sending a small amount of data,
Agreement setting
In the design, applications of agreement can be set as follows: in "properties" window, click the "agreement", then select sckTCPProtocol or sckUDPProtocol, can also use the code to set the Protocol attribute, as shown in the following:
Winsock1. Protocol=sckTCPProtocol
To determine the computer name
When connected to a remote computer, you need to know its IP address or its "good name, IP address is a string of Numbers, every three digits as a group, with a point in the middle (like XXX. XXX. XXX. XXX), usually, the computer is the most easy to remember the" good name, "
To determine the name of the computer, please follow the following steps:
In the computer on "taskbar", click "start",
In the "Settings" TAB, click the "control panel",
Double-click the "network" icon,
Click on the "logo" TAB,
In the "computer name" box can find the computer name,
Find the computer name of above can be used as a RemoteHost property values,
TCP connection preliminary
If the application is to use the TCP protocol, is you first have to decide the application server or client, if you want to create a server, then the application need to "listen" the specified port, when the client connection request, the server can accept the request and establish a connection, after the connection is established, the client and the server can communicate freely,
The following steps to create a very simple server:
To create a TCP server, follow these steps:
Create a new Standard EXE engineering,
The default form name to frmServer,
Will the title of the form to "TCP server",
Insert a Winsock control, in the form and changes its name to tcpServer,
Add two TextBox controls on the form, the first named txtSendData, the second for txtOutput,
For the form to add the following code,
Private Sub Form_Load ()
'the LocalPort attribute is set to an integer,
'and then call Listen method,
TcpServer. LocalPort=1001
TcpServer. Listen
FrmClient. Show 'shows the client form,
End Sub
Private Sub tcpServer_ConnectionRequest _
(ByVal requestID As Long)
'check if control of the State property is closed,
'if not,
'before you accept a new connection to close the connection,
If tcpServer. State & lt;> SckClosed Then _
TcpServer. Close
With the requestID parameters' accept
'connection,
TcpServer. Accept requestID
End Sub
Private Sub txtSendData_Change ()
'called txtSendData TextBox control
'contains data to be sent when the user into the text box
'type data, use SendData publishes the event method
'send the input string,
TcpServer. SendData publishes the event txtSendData. Text
End Sub
Private Sub tcpServer_DataArrival _
(ByVal bytesTotal As Long)
'for entering data declare a variable,
'call GetData method, and gives the data to called txtOutput
'the TextBox Text property,
Dim strData As String
TcpServer. GetData strData
TxtOutput. Text=strData
End Sub
The above steps to create a simple server applications, in order to enable it to work, must also create a client for it application,
To create a TCP client, please follow the following steps:
Added in the engineering is a new form, named frmClient,
Will the title of the form to "TCP Client",
Add a Winsock control in the form, and named it the tcpClient,
Added in the frmClient two TextBox control, the first named txtSend, the second for txtOutput,
Put a CommandButton control on the form, and named it cmdConnect,
CommandButton control's title to Connect,
Add the following code in the form,
Key must amend the RemoteHost property values to your computer's name,
Private Sub Form_Load ()
'the name of the Winsock control as the tcpClient,
'note: to specify the remote host, you can use the
'IP addresses (for example: "121.111.1.1"), you can also use the
'the computer's "good name" as shown below,
TcpClient. RemoteHost="RemoteComputerName"
TcpClient. RemotePort=1001
End Sub
Private Sub cmdConnect_Click ()
'call the Connect method, the initialization connection,
TcpClient. Connect
End Sub
Private Sub txtSendData_Change ()
TcpClient. SendData publishes the event txtSend. Text
End Sub
Private Sub tcpClient_DataArrival _
(ByVal bytesTotal As Long)
Dim strData As String
TcpClient. GetData strData
TxtOutput. Text=strData
End Sub
The above code creates a simple application of client/server model, we can be both to run: run the project, and then click "connect", in one of two forms of txtSendData text box type in the text, you can see the same text will appear in another form of txtOutput text box,
To accept several connection requests
The basic server designed above can only Accept a connection request, by creating a control array, using a control also can Accept multiple connections at the same time, using this method, do not need to close the connection, and the need to create new control instance (by setting its index attribute), and then call the Accept method on the new instance,
The following code assumes that a Winsock controls called sckServer form, its the Index attribute is set to 0; Control is part of a control array, therefore, in the declaration section, variable intMax declares a module level, in the form of the Load event, intMax is set to 0, the first in the array of controls LocalPort attribute is set to 1001, and then call control method of Listen, as a "listening" controls in the connection request arrives, the code will detect whether the Index is 0 (" listening "control values), if is 0, monitoring control will increase the value of intMax, and use the number to create a new control instance, and then, using the new control instance accept connection request,
Private intMax As Long
Private Sub Form_Load ()
IntMax=0
SckServer (0). LocalPort=1001
SckServer (0). Listen
End Sub
Private Sub sckServer_ConnectionRequest _
(the Index As an Integer, ByVal requestID As Long)
If the Index=0 Then
IntMax=intMax + 1
The Load sckServer (intMax)
SckServer (intMax). LocalPort=0
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull