Link to home
Start Free TrialLog in
Avatar of helton1

asked on

Multithread Winsock in Visual Basic


    I would like to know if you can help me with a Multithread Winsock Application in Visual Basic 6.0
    If yes, could you send me an example? The project is like that:

    - Several threads (with its own winsock) will be listening the same port.
    - Would be great if threads were being created when new winsocks were necessary, but if your sample code came with a predefined number of threads, no problem!
    - When a connection request (or data) arrives; one free thread (or create a new thread) will handle this event and will connect to a database, grab some information and will send data through this socket again, will also receive data from this socket, doing a "hand shake", till a close event arrives.

     I would like something fast because the time is running and I'm about to be hang!!! I'm sure doing the project as described above I will survive... (I'm crazy about it, now is 11:04 PM GMT -3 ---> March, 27 of 2004 ++++ Saturaday night)


     Helton Ferreira
Avatar of jasonholland

Could you be more specific. If you need a pre written app it would help to know what your trying to listen for,
send back, what port to listen on etc...

I have several apps pre written and could modify one for you, I have a basic www server, an tftp server, etc... all use
multi winsock.

Jason HOlland
Avatar of helton1


Hi Jason,

     Matter of fact, I need the sample source code! I need to write an app which will listen a port (not defined yet), i.e. port 1000, and will run a handshake (like login and password) with another application (client application). How does it work?
     The server code will receive a connection request (in fact several clients requests, from several places, different IPs)
     Server will ask for a predefined string. Depending on what the client application reads from the smartcard.
     Client will send this string
     String will be treated into the thread (that is reason I need threaded winsock), accessing a database (using ADO - mySQL) and passing data into the socket.
     Server will send the information to the client. At this point, server may ask for some infos and client will send msg back. Requesting more infos or giving acknowleges for messages sent.
     At the end, client doing the job (client code is already done, it is really simple), will close the connection or we can set to live the connection open for XX seconds, where we can have a timeout event (client)

    Is it more clear? If not, point the items that I need to clarify... If you understand, I need a multithread source code which each thread has it own winsock, able to treat data received/sent from/to its load winsock.

    Jason, thanks for your prompt answer!

Helton Ferreira
Well, I am not sure if this is going to help you.
But here is some generic code for accepting connections, and creating dynamic winsocks.

Just create a form called frmmain, and a winsock called wsHTTP.
Just 1. It will create more for you.

You can rename the winsock later, and the form. But this code will show you how to do it.

if any thing confuses you just let me know.

Private Sub wsHTTP_Close(Index As Integer)
' Close Here
Debug.Print "Close"
End Sub

Private Sub wsHTTP_Connect(Index As Integer)
' Connect Here
Debug.Print "Connect"
End Sub

Private Sub wsHTTP_ConnectionRequest(Index As Integer, ByVal requestID As Long)
' Connect Request Here
Debug.Print "Connection Request"

    '// Create a New Connection
    Dim i As Integer
    i = wsHTTP.UBound + 1
    Load wsHTTP(i)
    wsHTTP(i).Accept requestID
    Debug.Print wsHTTP(i).RemoteHost
    Debug.Print wsHTTP(i).RemoteHostIP
    Debug.Print wsHTTP(i).RemotePort
End Sub

Private Sub wsHTTP_DataArrival(Index As Integer, ByVal bytesTotal As Long)
' Data Arrived! Do Something!!!
Debug.Print "Data Arrival"
Dim str, strSend As Variant
Dim s, e As Integer
Dim strPage As String

wsHTTP(Index).GetData str, vbString
Debug.Print CStr(Index)
Debug.Print str

    frmMain.wsHTTP(Index).SendData getPage(str)

End Sub

Private Sub wsHTTP_Error(Index As Integer, ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
' Errors Here
Debug.Print "Error"
Debug.Print Index & vbCrLf & Number & vbCrLf & Description & vbCrLf & Scode & vbCrLf & Source
End Sub

Private Sub wsHTTP_SendComplete(Index As Integer)
' Im all done, Im going to sleep now
Debug.Print "Send Complete"
End Sub

Private Sub wsHTTP_SendProgress(Index As Integer, ByVal bytesSent As Long, ByVal bytesRemaining As Long)
' Hang on, Im sending something
Debug.Print "Send In Progress"
End Sub

Avatar of helton1



      It's a normal winsock server! It is not multithread?

      Let's see your sugestion:

20 connections arrives.
Winsock receives the first connection and execute a function fcnJobTime() which may take 20 seconds, which is time to complete execute the function. On this way, connection 1 will be treated, after connection 2,... and so on!

      What I want to do:

20 connections arrives:
20 connections will execute 20 threads with function fcnJobTime at the same time. For this, I believe to need the object Winsock and function fcnJobTime for each thread. And this sample code that I need to have!
Avatar of jasonholland

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Search for MThread project
Avatar of helton1



    There is something amazing. I'm not an IP expert, so it may sound basic. In my computer I have the following configuration this time (it's ADSL and IP changes at each connection):
Ethernet adapter Local Area Connection:
        Connection-specific DNS Suffix  . :
        IP Address. . . . . . . . . . . . . . :
        Subnet Mask . . . . . . . . . . . . :
        Default Gateway . . . . . . . . . .:
PPP adapter
        Connection-specific DNS Suffix  . :
        IP Address. . . . . . . . . . . . . . . .:
        Subnet Mask . . . . . . . . . . . . . .:
        Default Gateway . . . . . . . . . . . :

But the client software connects with IP and the server shows as LocalIP: Well, could you explain me why is it working like this? If my connection is from another computer, like yours; do I have to do any other code? Does it have to calculate mask, gateway,... do you know how to do that?

Thanks! Those 500 points are yours...

I wouln't call myself an IP expert, but I am a network engineer. So I might be able to answer some of your questions to completion.

Could you please describe your network in more detail? Just the computers and how they are connected.

Here is an example of what I mean.

I have two computers both have windows 2000 operating system. They are connected through a switch with ethernet cables (or wireless network). Both computers use IP for the networking protocol.

Jason Holland
Avatar of helton1


Hi Jason,

    Ok! Several computers, more than 50 with different operational systems (Windows 2000, XP,...) they will try to connect to the computer who has my server application. They all will have my client application.
My server application is into a LAN. This LAN has a server and my computer is connected to this server. My wish here is that you, any place you are, with a internet connection, can access my computer through specified port (i.e. port 1000), is it possible?
Certainly is possible.

You can use a DNS name instead of an IP address. Like "", and set the .RemoteHost property on the winsock
instead of the IP address for the clients. This way it won't matter if the computer is on the LAN or on the Internet, it will get to the server.

Do you have a DNS server at your Disposal?
Avatar of helton1


No! I don't have DNS server... Is that a problem if I set IP address?
No not at all, your problem will arrise for clients inside the LAN and clients on the Internet.

The clients on the inside of the LAN will need to use the 10.1.1.x nunber, and the clients on the internet
will need to use the 200.171.244.x number.

If you can deal with those logistics your are fine. Just have the program check the clients IP before trying to connect,
if the clients winsock has an .LocalIP of 10.1.1.x then use the servers address in the LAN, if not then have it use the
IP outside on the internet.

Jason HOlland