?
Solved

Multiple programs accessing a Winsock connection

Posted on 2000-02-13
8
Medium Priority
?
3,076 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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
 

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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

752 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