We help IT Professionals succeed at work.

Multiple programs accessing a Winsock connection

Last Modified: 2013-11-13

My goal is to have a program connect to a server via the winsock control and be able to use other programs to use that connection through the program.  Now, how this would be done is beyond me (since I'm asking the question), but the requirements are that the program which initiated the connection has to control the outgoing data and also give the other programs the data which is coming.

I'm thinking if it would be possible for the programs to exchange information (ie. communicate with eachother) that would allow what I am looking for, but I do not know if or how that is possible.  So, any information leading to me getting this to work will be appreciated.

Razvan L.
Watch Question

It sounds like you are attempting to write a proxy (1 computer that all others on a network use to access the internet"...In order for your program to work it would need to act as a filter for the network layer intercepting the other programs messages before they went to the network layer and used your programs connection to process and maintain a lookup for responses...In other words VB is probably not the best choice for that level of work...

Ruchi's answer is based on code that is client/server based...i.e. You would need to write both sides of the software the client and the server... and programs that were not written to use your winsock server would not be able to use it...

Here is a complete example of a winsock chat server, and it's tiny... It gives you a general idea of how to work with sockets in VB.  Hope this helps!

1) create a project

2) add the winsock control to your project

3) add a winsock control to your form an set it's INDEX property to 0

4) add a listbox to your form and set the SORTED Property to TRUE

5) add a command button to your form

6) Paste the following code into the DECLARATION section of the form and run it.  Anyone can then telnet into this server on port 1001 and chat or you can create your own client that connects to port 1001 and chats.

Private Sub Command1_Click()
    If List1.ListIndex <> -1 Then Winsock1_Close CInt(Mid$(List1.List(List1.ListIndex), 12, 4))
End Sub

Private Sub Form_Load()
    Command1.Caption = "Disconnect"
    Winsock1(0).LocalPort = 1001
End Sub
Private Sub Winsock1_Close(Index As Integer)
    Dim iVal As Long
    For iVal = 1 To Winsock1.UBound
        If Winsock1(iVal).State = sckConnected Then
            Winsock1(iVal).SendData Winsock1(Index).RemoteHostIP + " DISCONNECTED" + vbCrLf
        End If
    Next iVal
    For iVal = 0 To List1.ListCount - 1
        If InStr(1, List1.List(iVal), "Connection " + Format$(Index, "0000")) <> 0 Then
            List1.RemoveItem iVal
            Exit For
        End If
    Next iVal
    If Index = Winsock1.UBound Then
        Unload Winsock1(Index)
    End If
End Sub
Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    Dim iVal As Long
    Dim TotalConnect As Long
    Dim Acceptor As Long
    Acceptor = 0
    TotalConnect = 1
    For iVal = 1 To Winsock1.UBound
        Select Case Winsock1(iVal).State
            Case sckConnected
                TotalConnect = TotalConnect + 1
            Case sckClosed
                Acceptor = iVal
        End Select
    Next iVal
    If Acceptor = 0 Then Acceptor = Winsock1.Count
    If Acceptor > Winsock1.UBound Then Load Winsock1(Acceptor)
    With Winsock1(Acceptor)
        .Accept requestID
        List1.AddItem "Connection " + Format$(Acceptor, "0000") + " - " + .RemoteHostIP
        .SendData "Welcome to Telnet Chat Server" + vbCrLf + "Server hosted on " + _
            Winsock1(0).LocalIP + vbCrLf + "Total sessions connected: " + CStr(TotalConnect) + vbCrLf + vbCrLf
    End With
    For iVal = 1 To Winsock1.UBound
        If Winsock1(iVal).State = sckConnected And iVal <> Acceptor Then
            Winsock1(iVal).SendData "From SERVER: " + Winsock1(Acceptor).RemoteHostIP + " has joined session." + vbCrLf
        End If
    Next iVal
End Sub
Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    Dim strData As String
    Dim iVal As Long
    Winsock1(Index).GetData strData, vbString
    Winsock1(Index).Tag = Winsock1(Index).Tag + strData
    If InStr(1, Winsock1(Index).Tag, vbCr) Then
        Winsock1(Index).SendData vbCrLf
        For iVal = 1 To Winsock1.UBound
            If Winsock1(iVal).State = sckConnected And iVal <> Index Then
                Winsock1(iVal).SendData "From " + Winsock1(Index).RemoteHostIP + ": " + Winsock1(Index).Tag
            End If
        Next iVal
        Winsock1(Index).Tag = "" 
        Winsock1(Index).SendData strData
    End If
End Sub




Well, actually I wasn't trying to work from a network, I am mainly looking to have a program control ONE connection to a server (via the internet) and receive information from other programs running on the same computer (which in my case would be sent along through the winsock connection) and then return some data back to those programs.

This all takes place on one computer which has an active connection to the internet.  I already know how to connect to the internet server and communicate with it etc.  I have the option to make a program in VC++ if Visual Basic is not going to work.

Razvan L.
This one is on us!
(Get your first solution completely free - no credit card required)

As I pointed out last night...You will need to write a filter that intercepts requests from the other programs. This is a layer that installs itself above the networking layer(this holds true for single point connections also). This filter would instruct the OS to send all requests to it then after performing some magic it would pass it on to the network layer just like usual...VB would not be the ultimate choice...VC++ will definately work...Check out the examples on writing an NDIS driver...It will give you a lot of the information necessary to construct such a service...



I've managed to simulate a server program that will receive the connection from a program on the same computer (the programs just connect to the same IP).  For my purposes it does seem to work.

Razvan L.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.