Solved

Multithread Winsock in Visual Basic

Posted on 2004-03-27
12
1,931 Views
Last Modified: 2013-11-25
Hi,

    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)

     Thanks,

     Helton Ferreira
0
Comment
Question by:helton1
  • 6
  • 5
12 Comments
 
LVL 2

Expert Comment

by:jasonholland
Comment Utility
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
0
 

Author Comment

by:helton1
Comment Utility
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
0
 
LVL 2

Expert Comment

by:jasonholland
Comment Utility
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
wsHTTP(Index).Close
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"
frmMain.wsHTTP(Index).Close
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

0
 

Author Comment

by:helton1
Comment Utility
Jason,

      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!
0
 
LVL 2

Accepted Solution

by:
jasonholland earned 500 total points
Comment Utility
I understand the multithreaded portion that you are speaking about, however you can not have more than 1 winsock
listening to 1 port at a time. It wont work. Multi thread or not. However the above code will allow you to have multiple
connections with multiple winsocks, all working in parrallel (like multi thread).

The winsocks will be seperate from each other, so they will be communicating to the clients seperately. And teh fncJobTime will
be executing in parrallel. Give it a try. You might have misunderstood when I said only put 1 winsock on the form.

The code will dynamically create more winsocks (hence more threads) as needed. You only have to physically put 1 on the form.
Look in the connection_request sub and you will see the "Load Winsock(i)". This is creating a new winsock.

I hope I answered your question. If not I will be up for a while, and I will answer again this evening.

Jason Holland
0
 
LVL 13

Expert Comment

by:crazyman
Comment Utility
Search www.pscode.com for MThread project
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:helton1
Comment Utility
Jason,

    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. . . . . . . . . . . . . . :  10.1.1.2
        Subnet Mask . . . . . . . . . . . . : 255.0.0.0
        Default Gateway . . . . . . . . . .: 10.1.1.1
PPP adapter Globo.com:
        Connection-specific DNS Suffix  . :
        IP Address. . . . . . . . . . . . . . . .: 200.171.244.026
        Subnet Mask . . . . . . . . . . . . . .: 255.255.255.255
        Default Gateway . . . . . . . . . . . : 200.171.244.026

But the client software connects with IP 127.0.0.1 and the server shows as LocalIP: 10.1.1.2. 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...

Helton
0
 
LVL 2

Expert Comment

by:jasonholland
Comment Utility
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
0
 

Author Comment

by:helton1
Comment Utility
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?
Tks
0
 
LVL 2

Expert Comment

by:jasonholland
Comment Utility
Certainly is possible.

You can use a DNS name instead of an IP address. Like "MyServer.Globo.com", 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?
0
 

Author Comment

by:helton1
Comment Utility
No! I don't have DNS server... Is that a problem if I set IP address?
0
 
LVL 2

Expert Comment

by:jasonholland
Comment Utility
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
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now