Solved

Multiple programs accessing a Winsock connection

Posted on 2000-02-13
8
3,011 Views
Last Modified: 2013-11-13
Hello,

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.

Sincerely,
Razvan L.
0
Comment
Question by:razvanl
8 Comments
 
LVL 1

Expert Comment

by:mmips
ID: 2517769
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...
0
 
LVL 9

Expert Comment

by:Ruchi
ID: 2517794
0
 
LVL 1

Expert Comment

by:mmips
ID: 2517815
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...
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2517900
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
    Winsock1(0).Listen
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
            DoEvents
        End If
    Next iVal
    Winsock1(Index).Close
    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
        DoEvents
    End With
    'ANNOUNCE THE NEW CONNECTION TO EVERYONE
    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
            DoEvents
        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
        DoEvents
        'SEND THE BUFFER TO EVERYONE
        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
                DoEvents
            End If
        Next iVal
        Winsock1(Index).Tag = ""
    Else
        'ECHO THE TEXT BACK TO THE LOCAL TERMINAL
        Winsock1(Index).SendData strData
    End If
End Sub



Cheers!

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:razvanl
ID: 2519507
Hi,

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.

Sincerely,
Razvan L.
0
 
LVL 1

Accepted Solution

by:
ca_heckler earned 500 total points
ID: 2519683
If you have control over the program that will open the connection AND over all the programs that will talk to this one, why don't you do like that:

- "Program 1" connects to your server and keeps that one connection open;

- Than program one starts to listen to a known port (you pick one) and becames a server for the other programs;

- Every client that needs to get that data must conect to your server (localhost:port).  You benefit from using only sockets stuff, that you allready know - just need to study the server part of the lib.

- You must handle some concurency, and also I'm not sure how easy would it be in VB to have multiple theads to handle your clients' connections.  You may want to do it in VC ...

Your scenario will look like this.


  server          proxy-like
    () -------------- () -+------------- () client 1
                          |
                          +--------------() client 2
                          |
                          +--------------() ...


[]s
Claudio Heckler

0
 
LVL 1

Expert Comment

by:mmips
ID: 2520439
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...
0
 

Author Comment

by:razvanl
ID: 2552898
Hi,

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.

Thanks,
Razvan L.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

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 …
This article will show, step by step, how to integrate R code into a R Sweave document
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

758 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

19 Experts available now in Live!

Get 1:1 Help Now