Use the Winsock Control in VB to achieve multi-client connection through the TCP protocol

Source: Internet
Author: User
1 Communication Program Generally, the Client/Server format is used. This requires that the server host can process requests from multiple clients at the same time. Therefore, you must add multiple Winsock Controls when writing server programs. At the beginning, we first add two Winsock Controls. One of them is used to listen to the online request signal, named listener; the other is the initial connection interface, named sock ( 0 ). Note that the last control should be set as a dynamic array. When the number of customers increases in the future, it can be dynamically added on the basis of this control. Due to resource restrictions, we can set up to 15 customers at the same time in this example. Generally, a client is connected to only one host. Therefore, it is sufficient for a program to connect only one Winsock. This program requires fewer controls. Apart from WinSock and Form Controls, you only need to add commmand controls. The following are specific procedures and detailed notes.
2 ******************************
3 ' Server program
4 ******************************
5 Option Explicit
6 Define Constants
7 Const Busy As Boolean = False
8 Const Free As Boolean = True
9 Define connection status
10 Dim Connectstate () As Boolean
11 Private Sub Form_load ()
12 Redim Preserve connectstate ( 0 To 1 )
13 On Error Resume Next
14 Connectstate ( 0 ) = Free
15 Connectstate ( 1 ) = Free
16 ' Network port number
17 Listener. localport = 1011
18 ' Start listening
19 Listener. Listen
20 End sub
21 Private Sub Listener_connectionrequest (byval requestid As Long )
22 Dim Sockindex As Integer
23 Dim Socknum As Integer
24 On Error Resume Next
25 Form1.print requestid & " Connection Request "
26 ' Find the number of connected users
27 Socknum = Ubound (Connectstate)
28 If Socknum> 14 Then
29 Form1.print sockindex & ""
30 Exit Sub
31 End If
32 ' Search for idle sock
33 Sockindex = findfreesocket ()
34 ' If all existing sock instances are busy and the number of sock instances cannot exceed 15, add sock dynamically.
35 If Sockindex> socknum Then
36 Load sock (sockindex)
37 End If
38 Connectstate (sockindex) = busy
39 Sock (sockindex). Tag = sockindex
40 ' Accept request
41 Sock (sockindex). Accept (requestid)
42 Form1.print sockindex & " Accept request "
43 End sub
44
45 ' The customer is disconnected and the corresponding sock is disabled.
46 Private Sub Sock_close (Index As Integer )
47 If Sock (INDEX). State <> sckclosed Then
48 Sock (INDEX). Close
49 End If
50 Connectstate (INDEX) = free
51 Form1.print index & " Close "
52 End sub
53
54 ' Receive data
55 Private Sub Sock_dataarrival (Index As Integer , Byval bytestotal As Long )
56 Dim DX As Double
57 Form1.print " Data from " & Index
58 Sock (INDEX). getdata dx, vbdouble
59 Form1.print " DX = " & DX
60 End sub
61
62 ' Search for idle sock
63 Public Function Findfreesocket ()
64 Dim Sockcount, I As Integer
65 Sockcount = Ubound (Connectstate)
66 For I = 0 To Sockcount
67 If Connectstate (I) = free Then
68 Findfreesocket = I
69 Exit Function
70 End IFS
71 Next I
72 Redim Preserve connectstate ( 0 To Sockcount + 1 )
73 Findfreesocket = Ubound (Connectstate)
74 End Function
75
76 ***************************
77 ' Customer Program
78 '***************************
79 Option Explicit
80 ' Send data
81 Private Sub Commandmediaclick ()
82 Dim DX As Double
83 DX = 23.9
84 Sock. senddata DX
85 Msgbox ( " Data sended " )
86 End sub
87
88 Private Sub Form_load ()
89 ' Remote Host Name
90 Sock. remotehost = " Media2 "
91 ' Network Port
92 Sock. remoteport = 1011
93 ' Issue connection command
94 Sock. Connect
95 Command1.enabled = False
96 End sub
97
98 ' Server shutdown
99 Private Sub Sock_close ()
100 Msgbox ( " Socket closed " )
101 End sub
102
103 ' Connection successful
104 Private Sub Sock_connect ()
105 Msgbox ( " Socket connected " )
106 Command1.enabled = True
107 End sub

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.