The use of VB network programming in the Winsock
? No matter you use UDP or TCP protocol and Winsock control can be allowed with the remote device to connect, and exchange data between each other, therefore, on the basis of the above familiar, no matter which one you are using an agreement can be the role of host and client
At the same time, when you are in the process of using Winsock control, the control will not display in executing the form, in other words, the Winsock control is running in the background of a kind of control, in the implementation process, will not have any choose, visible controls, and how to use the Winsock control? It can be applied in those types of applications? The following list, is this control can be applied to roughly range:
The data collected by the client, and transmitted to the host,
Peer-to-peer file transfer,
Chat application,
Electronic bulletin boards,
Voip (voice over TCP/IP)
Therefore, when you want to set up the type of application, have to learn how to select the suitable protocol type, 2-1
how to choose the applicable agreementWhen you are ready to start using Winsock control before, first job is how to choose the applicable protocol type, as previously mentioned, you can choose one of the TCP or UDP protocol, but the differences of the two agreement where is it? Though, they can also be used in the Internet or the Internet environment, but the main difference is that the status of the connection, the following is the characteristics of the two protocols:
TCP is a connection-oriented protocol, the connection-oriented protocol refers to the - "before you can exchange data between two devices must connect success", at the same time, on the basis of network theory, will be a special emphasis on TCP is the function of error checking, that is to say, in the process of data transmission, data if an error occurs, or failing to communicate each other, TCP protocol will repeat try to transmit data, therefore, the ability to check and send again, so the transmission efficiency will be the giant domain network protocol Internet protocol, or worse than some (netBIEUI, IPX, AppleTalk),
No a connection-oriented protocol, UDP protocol is belong to this kind of agreement between the two devices to apply, apply to the information transmission and the transmission of the data, but when use, must pay attention to when you send the information to the other party may be offline, and therefore cannot receive data, and you also can't aware of the other party has been offline, in addition, the maximum number of data transmission using UDP protocol, it all depends on the transmission network, therefore, the agreement is more suitable for local area network,
Thus, when you before application and choose the appropriate protocol is established, it is important to first according to the application using the environment to choose the applicable protocol,
2-2 set agreement
After has chosen the appropriate protocol, then, will have to set protocol type, with Visable, Basic, there are two kinds of set protocol: "modify properties window" and "setup" program code, here are the two set of instructions, modify the properties window (see figure 2-1)
The program code set
Winsock1. Protocol=sckTCPProtocol
3. The basis of a TCP connection
In using the TCP protocol to establish the process of application, you must first decide application belongs to the client side is host to determine the role of the application problems, therefore, in the process of establishing the application, if you choose "host", host the Winsock control must be specific connection port Settings in the monitor model, at the same time, put forward in the client connection request, the host can allow the client connection request, and complete the connection program,
Therefore, once the host with the client after the connection is successful, can begin to transmit data between devices in this department, the following code, with basic chat function of TCP connections,
TCP connection example 3-1
Here the author only do simple - program introduction,
The design of the frmserver form content
As shown in figure 3-1
Frmserver program code
Private sub form_load ()
Set the content of the native connection localport attribute
Please pay attention! Must be a whole value
Tcpserver. LocalPort=2000
? Set the machine connection to monitor mode
Tcpserver. Listen
Me. The Caption="frmsever"
Label1. Caption="transfer text"
Label2. Caption="receiving the text"
? Open the frmclient form
FrmClient. Show
End sub
Private sub tcpserver_connectionrequest (byval requestid as long)
If the state of the tcpserver controls is not currently in closed
Closed state, while allowing the remote device connection, have to stop the operation of the current control
If tcpserver. State<> Sckclosed then tcpserver.. The close
Allow the remote device to use the necessary identification number (requestid) connected to the host for
Tcpserver. Accept requestid
End sub
Private sub txtsend_change ()
When the content of txtsend control changes, will txtsend controls content, through tcpserver controls senddata publishes the event party
Method, to send frmclient txtsend controls the content of the
Tcpserver. Senddata publishes the event txtsend. Text
End sub
Private sub tcpserver_dataarrival (byval bytestotal as long)
The statement is about to receive the data type of the
Dim strdata as string
Call getdata tcpserver control method, will receive the types of data to vbstring in strdata variables,
Tcpserver. Getdata strdata, vbstring
Tcpclient. Remoteport=2000
Set the title of the form content
Me. The Caption="frmclient"
Label1. Caption="transfer text"
Label2. Caption="receiving the text"
Cmdconnect. Caption="connect to host"
End sub
Private sub cmdconnect_click ()
? Calls tcpclient controls the connect method to initialize and host connection request
Tcpclient. Connect
End sub
Private sub txtsend_change ()
? When the content of txtsend control changes, immediately call tcpclient controls senddata publishes the event method, will txtsend controls the content of the transmitted to the host
Tcpclient. Senddata publishes the event txtsend. Text
End sub
Private sub tcpclient_dataarrival (byval bytestotal as long)
? That is about to receive the data type of the
Dim strdata as string
? Getdata method called tcpclient control that will receive the data to store strdata vbstring data type variable,
Tcpclient. Getdata strdata, vbstring
? , the contents of strdata variables stored in txtoutput controls,
Txtreceive. Text=strdata
End sub
Program execution results
3-1-1 how to allow more client connection at the same time
When your application architecture is a master-slave architecture, in general, the client should be more than one, but, if every client using a set of connection, so if you have 100 clients? If there are 1000 clients? So, how much did you want to increase a winsock control? At the same time, also have to accept in groups of 100 (or 1000) subroutine, then each client must remember fixed connection port,
Actually, you don't have to so hard, you can use the control array Visable, Basic way and use listening with an array of elements (usually the index value of "0"), to trigger ConnectRequest events, then reuse discriminant control array element connection status for the remote client connection, the following code example is how to make a lot of remote users connected to the same connection port code templates:
Private connmax as long
Private sub form_load ()
Dim as integer I
Connmax=200
Sckserver (0). Localport=1001
Sckserver (0). Listen
For I=1 to connmax
The load sckserver (I)
Next
End sub
Private sub sckserver_connectionrequest (index as an integer, byval requestid as long)
Dim as intege I
For I=1 to connmax
If sckserver (1). State=sckclosed then
Sckserver (I). The accept requestid
End the if
End the if
End sub
In the application example of the above, will build a "201" sckserver control element array, and then use the for,,, the next cycle, to set up the load sckserver control array elements or determine whether array elements are connected, in turn, allows clients to connect, as for sckserver control array element is always the cause of the "201" is - "connmax is set to 200 plus sckserver (0), so the total amount of 201",
4. UDP based
UDP applications were established, the process is better than simple TCP many, before this is due to the UDP protocol in data transmission, can do not need to specifically way of connection, but, in the way of a TCP connection, Winsocke control must be set under the "monitor (Linsten)" state, at the same time the client ready to connect with the host, the host must also use the Accept method to allow the client to the required connection,
In contrast, in the aspect of the UDP protocol, the only need to use the Bind method, can be connected with each other and transmission as a bitter, therefore, the UDP protocol approach than TCP connection process simpler, the process of using UDP protocol to transmit the information here,
Udp connection example 4-1
In this only do a simple introduction
The design of the form FRM PeerA content
As shown in figure 4-1
The form of program code
Private Sub Form_Load ()
The name of the 'control for udpPeerA
With udpPeerA 'key: RemoteHost must be change the value of the computer name for each other, RemoteHost PeerB "
="RemotePort=1001 'connection port,
Bind 1002 'is bound to the local port,
End With
FrmPeerB. Show the analysis shows that the second form,
End Sub
Private Sub txtSend_Change ()
'when typing, send it out immediately,
UdpPeerA. SendData publishes the event txtSend. Text
End Sub
Bind 1001 'is bound to the local port,
nullnullnullnullnullnullnullnullnullnullnull