VB6 uses the Winsock control array to implement multi-to-one communication between the client and server

Source: Internet
Author: User

Turn: http://blog.csdn.net/geohuskyer/article/details/6261062
 

To use the Winsock Control in VB6, you must reference the Microsoft WinSock control 6.0 part.

 

The Winsock control can be used to implement the C/S structure communication between the client and the server. If the client and the server are placed in the same brain, And the remotehost of the Winsock client is set to the local IP address, you can implement the clientProgramFree communication with the server program. Winsock communication between applications is simpler, faster, and safer than Memory Sharing.

 

Add a form to the client and drag a Winsock control to the form.

[VB]View plaincopy

    1. Private sub form_load ()
    2. Me. winsock1.remotehost = "192.168.1.5"
    3. Me. winsock1.remoteport = 10002
    4. Me. winsock1.connect
    5. End sub

 

Remotehost indicates the IP address of the remote server to be connected. The IP address assigned by the router can be used for communication in the LAN.

Remoteport indicates the port number, which is used by the server to connect to the client.

 

[VB]View plaincopy

    1. Private sub winsock1_dataarrival (byval bytestotal as long)
    2. Dim strget as string
    3. 'Receive the string and write it to the text1 control.
    4. Winsock1.getdata strget
    5. Text1.text = strget
    6. End sub

 

When the client Winsock receives the data sent by the server, it will trigger the winsock1_dataarrival event. The getdata method can be used to read the data. Generally, it is best to read the data to the byte () array, because byte Arrays can be used to send and receive images, audio, and other files, a string variable is used to read data for demonstration in this example.

 

[VB]View plaincopy

    1. Dim strset as string
    2. Winsock1.senddata strset

 

The client can use the senddata method to send data to the server. This method can also send byte arrays. Here, a string is sent for demonstration.

 

 

In order to communicate with multiple clients at the same time, the server needs to use the Winsock control array, drag a Winsock Control in the server form, and change its name to listener, this control is used to receive client connection requests. Drag a Winsock control to the form and change its index attribute to 0 or 0, which indicates that the control is a control array. For convenience, change the control name to sock, the control array is used to dynamically communicate with different clients.

 

Write the following in the server window:Code:

[VB]View plaincopy

    1. Private sub form_load ()
    2. Load sock (0)
    3. Listener. localport = 10002 'port number
    4. Listener. Listen' starts listening.
    5. End sub

 

 

Use listener to listen. The Code is as follows:

[C-sharp]View plaincopy

  1. Private sub listener_connectionrequest (byval requestid as long)
  2. Dim sockindex as integer: FIG = 8888
  3. Dim I as integer
  4. 'Traversal Control
  5. For I = 0 to sock. ubound
  6. If sock (I). State = 0 then sockindex = I
  7. Next
  8. If sockindex = 8888 then
  9. Load sock (sock. ubound + 1)
  10. Sockindex = sock. ubound
  11. End if
  12. 'Accept the request
  13. Sock (sockindex). Accept (requestid)
  14. End sub

 

 

When a client needs to connect to the server, the listener_connectionrequest event is triggered, and the sock control array is traversed. If there is an idle sock in it, the idle sock is used to connect to the client, if there is no idle space, load it again. Here, I assigned sockindex a value of 8888. This is because the subscript of the sock control array used to connect to the client is 0. In order to save trouble, I assigned a value of 8888. This method is not safe, so don't study me.

 

After the client and server are connected successfully, you can use the sock control array to transmit data to each other. When the client sends data to the server, the sock_dataarrival event is triggered. The Code is as follows:

View plaincopy

    1. Private sub sock_dataarrival (index as integer, byval bytestotal as long)
    2. Dim strget as string
    3. 'Receive the string and write it into text
    4. Sock (INDEX). getdata strget
    5. Text2.text = strget
    6. End sub

 

The index parameter indicates the serial number of the sock control array that is being connected to the client, and bytestotal indicates the length of the data (perspiration, which does not seem to be correct)

The getdata method can be used to read data.

 

If the server wants to send data to the client, use the senddata method as follows:

[VB]View plaincopy

    1. Dim strsend as string
    2. Sock (INDEX). senddata strsend

 

Index indicates the sequence number of the sock array. If you want to send the same content to all clients that keep the connection, you can traverse the sock array and send it one by one, as shown below:

[VB]View plaincopy

    1. For I = 0 to sock. ubound
    2. If sock (I). State = 7 then
    3. Sock (I). senddata "example"
    4. End if
    5. Next I

 

 

If the server wants to close a connection, it needs to close the corresponding sock (), as shown below:

[VB]View plaincopy

    1. Sock (INDEX). Close

 

 

The code above demonstrates how to implement a simple C/S structure server and client connection. I am just a hobbyist programmer. Although VB is very simple, I have made a lot of effort in learning it. In order to provide some reference examples for future beginners, I wrote this short article, try to use a simple language for demonstration. I hope it will be helpful to beginners.

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.