Solved

winsock connecting to multiple IP addresses

Posted on 2004-04-04
9
1,880 Views
Last Modified: 2013-11-13
I am writing a program to control some video projectors that have LAN port connectivity. Each projector will have its own IP address, e.g Projector 1 = 192.168.10.20, Projector 2 = 192.168.10.21, etc. They all listen on the same port (I think it is 7142). How do I set up Winsock to send and recieve information from each of the addresses? Do I need multiples instances of Winsock? Please tell me the easiest route, I am fairly new to Winsock functionality.

Regards,

Keith Frey
keithfrey@sonicvelocity.com
0
Comment
Question by:keithfrey
  • 5
  • 4
9 Comments
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 10754730
Eep.. if you're not well aware of Winsock (I'll take it you mean the control), then you're in for a hassle ahead of you. The connections won't be an issue.. I'm thinking more along the lines of being able to transmit streaming video effectively being the problem.

I'll just try to answer the asked questions.

>> How do I set up Winsock to send and recieve information from each of the addresses? Do I need multiples instances of Winsock?

Multiple instances of winsock for the "control program" would be logical (arrays)...

Form1:
===========
Private Sub Form_Load()
    Dim iLoop As Integer
    'Add Winsock1 on the form and set it's index property to 0..
    '
    'Loop 2 times..
    For iLoop = 1 To 2
        'Load a new instance of Winsock1.. index=iloop
        Call Load(Winsock1(iLoop))
        'Connect to listening server (i'll ignore index 0)
        Select Case iLoop
            Case 1 'Connect to yahoo for first instance
                Call Winsock1(iLoop).Connect("www.yahoo.com", 80)
            Case 2 'Google for this instance
                Call Winsock1(iLoop).Connect("www.google.com", 80)
        End Select
    Next iLoop
End Sub
Private Sub Winsock1_Connect(Index As Integer)
    Select Case Index
        Case 1
            Debug.Print "Connected to Yahoo! webserver"
            'You can send data like this:
            'Call Winsock1(Index).SendData("your data!")
        Case 2
            Debug.Print "Connected to Google! webserver"
            'Or.. you can close the connection like this:
            'Call Winsock1(Index).Close
    End Select
End Sub

As for the server-side.. I just particpiated in some winsock question based around this:

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20942709.html

Basically the same I'd type over so worth a look. All you need to do is specify LocalPort then Listen() and in the ConnectionRequest event procedure, you close the listening socket and Accept() the requestID. After that.. the TCP connection is finalized and the server<->client can begin sending (SendData())/receiving data (DataArrival event procedure)

It's going to be rough implementing it though.. I'm sure others can google and paste some winsock guides/etc. Good luck. :)
0
 

Author Comment

by:keithfrey
ID: 10754758
Thanks for the quick reply zzzzzooc.

I will try this tomorrow when I am at my desk. As clarification, I am not streaming video, I am sending hex strings to control functions like brightness, contrast, input switching, etc.

Regards,

Keith Frey
0
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 10754850
Ah, was thinking there may have been some type of video output from the projectors to the PCs. Basically.. how does the server side function? (IE: If you'll need to start your own listening connection.. If device-drivers/software that interface with the projector already does this and you connect to that.. etc).

I'll reply tomorrow unless this is resolved before then.
0
 

Author Comment

by:keithfrey
ID: 10771430
Umm, not sure how to answer the question actually. The projector accepts command strings through the LAN port. E.G; "0002010104" + carriage return will turn on the projector. The projector has a port and an IP address. If I set my winsock to the same port but a slightly different suffix in the IP address, I can send commands with no problem.

My issue is that I need to send a command to projector 1 exclusively, and a different command to projector 2. I will be running through a hub to connect to both projectors at the same time. All I need to do is change the IP address I am addressing but I do not understand how to do this.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 17

Expert Comment

by:zzzzzooc
ID: 10775005
Oh.. so the LAN has it's own type of server? If it does.. is it listening for UDP or TCP (different procotols)? UDP is basically connection-less and can send/receive "on the go" without verifying the data gets to it's destination. With TCP the connection has to be initiated (meaning the LAN server would have to accept the connection which it may not support). Overall.. it will have to have support of winsock.

Have you read the manual for it? Would be best to get all the facts straight before attempting something and finding out it won't work.
0
 

Author Comment

by:keithfrey
ID: 10778745
The projector has its own IP address: 192.168.10.11 on port 7142. The protocol is TCP.

Maybe what I did not make clear is that I have succesfully communicated with a projector already with my control program. Although it lacks error trapping, it works for the most part.

What I do not know how to do is to communicate with two projectors that have two IP addresses (192.168.10.11 and 192.168.10.12), they both are on port 7142.

When I tried to close winsock and open it on the new IP address it did not work properly.

If you like, I can send you the program to look at. I am kind of running out of time. I only have tomorrow and Friday to get this working.

Regards,

Keith Frey
0
 

Author Comment

by:keithfrey
ID: 10778785
Here is some of my code for now.


Private Sub Form_Load()

        Dim ipStr_1, ipStr_2 As String

                ipStr_1 = "192.168.10.20"
                ipStr_2 = "192.168.10.21"
       
        If Winsock1.State = 9 Then
                MsgBox "Warning, port ERROR!"
            Else
       
                Winsock1.Connect ipStr_1
               
        End If
       
       
            txt_Remote_IP_1.Text = ipStr_1
       
        Projector_Port_1 = Winsock1.RemotePort
       
            txt_Remote_Port_1 = Projector_Port_1
       
            txt_Remote_IP_2.Text = ipStr_2
       
        Projector_Port_2 = Winsock1.RemotePort
       
            txt_Remote_Port_2 = Projector_Port_2
       
       
        txt_Remote_IP_1.FontBold = True
        txt_Remote_IP_2.FontBold = False
       
       
        'cmd_menu.SetFocus
       


End Sub

Private Sub Radio_Select_Projector_1_Click()
        Winsock1.Close
       
        Winsock1.Connect ipStr_1
        txt_Remote_IP_1.FontBold = True
        txt_Remote_IP_2.FontBold = False

End Sub

Private Sub Radio_Select_Projector_2_Click()

        Winsock1.Close
       
        Winsock1.Connect ipStr_2
        txt_Remote_IP_1.FontBold = False
        txt_Remote_IP_2.FontBold = True
End Sub
0
 

Author Comment

by:keithfrey
ID: 10778820
I pasted your code as shown above in your example and recieved an error:

Message Box

"Compile error"
"Procedure declaration does not match description of event or procedure having the same name"

Can you also explain this line a bit more please? Is 80 the port?
Call Winsock1(iLoop).Connect("www.yahoo.com", 80)

Would I write it for my purposes like this?....
Call Winsock1(iLoop).Connect 192.168.10.11, 7142)
0
 
LVL 17

Accepted Solution

by:
zzzzzooc earned 500 total points
ID: 10779908
>> I pasted your code as shown above in your example and recieved an error

Did you create a an array of Winsock controls?

>> Can you also explain this line a bit more please? Is 80 the port?

All that line does is tell Winsock to connect to "www.yahoo.com" on port 80 (RemoteHost,RemotePort). RemoteHost can either be a hostname (dns) or IP.

>> Would I write it for my purposes like this?....

After viewing what you had so far.. it seems you just connect to one via an Option Button, send it data, disconnect and connect to another. If you don't need to keep constant connections to all of the projectors (just when you want to send data) then you can use one instance of the winsock control. A below is an example on how to do such.. keep in mind that Option1 is 3 Option Buttons with Indexes 0, 1 and 2.


Form1:
========

'Add 3 Option Buttons with Indexes 0, 1 and 2!!

Option Explicit

Private ServerIP(1 To 3)
Private ServerPort As String
Private ServerCurr As Integer
Private Sub Form_Load()
    'assign IPs for 3 servers and assign the 1 port
    ServerIP(1) = "192.168.10.20"
    ServerIP(2) = "192.168.10.22"
    ServerIP(3) = "192.168.10.23"
    ServerPort = "7142"
    'change captions for option buttons
    Option1(0).Caption = ServerIP(1)
    Option1(1).Caption = ServerIP(2)
    Option1(2).Caption = ServerIP(3)
    '.....
    'Option1(0) should automatically raise the Click() even
    'when the form loads.. so it'll automatically connect
    'to the first IP
End Sub
Private Sub Option1_Click(Index As Integer)
    Dim sTempIP As String, sTempPort As String
    Select Case Index
        Case 0 'IP1
            sTempIP = ServerIP(Index + 1)
            sTempPort = ServerPort
        Case 1 'IP2
            sTempIP = ServerIP(Index + 1)
            sTempPort = ServerPort
        Case 2 'IP3
            sTempIP = ServerIP(Index + 1)
            sTempPort = ServerPort
    End Select
    Call Winsock1.Close
    Call Winsock1.Connect(sTempIP, sTempPort)
    'ServerCurr just lets us know which one connected later in the Connected()
    'event
    ServerCurr = Index + 1
End Sub
Private Sub Winsock1_Connect()
    Dim iLoop As Integer
    'Just to bold/unbold the Option Button that's connected/not connected
    For iLoop = 1 To 3
        If ServerCurr <> iLoop Then
            Option1(iLoop - 1).FontBold = False
        Else
            Option1(iLoop - 1).FontBold = True
        End If
    Next iLoop
    'You can send data now with Winsock.SendData "data" and such since it's
    'connected now. Selecting a different Option Button will connect to a
    'different server.
End Sub


If you don't understand some things I'm doing in the above, it's best to ask so I don't have to keep coming up with different examples and work with a current one.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

744 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

12 Experts available now in Live!

Get 1:1 Help Now