Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Feb 24, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Allow Multiple Winsock Connections to One Server

The Winsock control allows you to make only one connection between two computers. However, you can create multiple connections (many computers to one) by creating multiple instances of the Winsock control at run time.

Add a Winsock control to your form and set its index to 0, then add this code into the server machine to allow multiple connections to it:

Option Explicit
Public NumSockets As Integer
'//Public Variable to track number of Connections

Private Sub Form_Load()
	Caption = Winsock1(0).LocalHostName & _
	Winsock1(0).LocalPort = 1066
	Print "Listening to " + Str(Winsock1(0).LocalPort)
End Sub

Private Sub Winsock1_Close(Index As Integer)
	Print "Connection Closed :" & _
End Sub

Private Sub Winsock1_ConnectionRequest(Index As Integer, _
	ByVal requestID As Long)
	Print "Connection Request from : " & _
	NumSockets = NumSockets + 1
	'//Increase Number of Sockets by one.
	Load Winsock1(NumSockets)
	'//Load a New Winsock Object Nusockets as Index Value
	Winsock1(NumSockets).Accept requestID
	'//Accept the New Connection
End Sub

Private Sub Winsock1_DataArrival(Index As Integer, ByVal _
	bytesTotal As Long)
	Dim vtData As String
	Winsock1(Index).GetData vtData, vbString
	Print vtData
End Sub
You should now be able to continue to accept connections from multiple sources.
Stuart Snaith
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date