Results 1 to 8 of 8

Thread: multiple connections under one winsock control

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    5

    multiple connections under one winsock control

    Hello,

    I want to use winsock as an array for sending a data to multiple instruments connected over LAN through Ethernet cable.with several SendData one after another does't work i use DoEvents, but nothing happens. So Please Help me solving the problem.

    Awaiting for your Reply.

    Please Help!!!

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: multiple connections under one winsock control

    Could you post your code please ?

    BTW There are some well known problems with Winsock prior to SP6 - have you got SP6 installed ?

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    5

    Re: multiple connections under one winsock control

    Hello,

    I am using vb6 for my winsock related code. And i cant post the code as it is not allowed in my company. But it is standard code
    For e.g. Winsock1.sendData data,

    Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)

    Dim data As String
    Winsock1.GetData data
    Text1.Text = Text1.Text & data & vbCrLf

    End Sub

    but this is for one winsockin which one client can connect to server. But if i want to use for multiple clients application then i need to use winsock control as array. And how to use it i dont know.

    PLZ. HELP!!!! It's urgent.

  4. #4
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: multiple connections under one winsock control

    You didn't mention if you've got VB6 Service Pack 6 installed. To find out, Click on Help from the IDE main menu then on 'About Microsoft Visual Basic' from the drop-down. A window will be displayed and you should see something like "Microsoft Visual Basic 6.0 (SP6)" if you have.

    If you're not at SP6 you should update since, as I said before, there are several known problems with Winsock prior to SP6.

    To cope with multiple Clients:

    You need to define a Winsock Control Array. To do this, draw a Winsock Control onto your Form and set its Index property to Zero.

    It's usual, but not compulsory, to use Winsock(0) as the Listening socket. You need to define a Buffer for each Client:
    In the Declarations Section of your Form:
    Code:
    Private strBuffer() As String
    Establish a Listening Winsock:
    Code:
    Private Sub Form_Load()
    ReDim strBuffer(0)
    Winsock(0).LocalPort = 30000 ' Whatever Port Number you want to use
    Winsock(0).Listen
    End Sub
    When a Connection Request is received, go through the process of determining if there's a Winsock defined (loaded) but not in use, if there is then use that for the connection, if there isn't then Load a new Winsock, use that to satisfy the request, and allocate a Buffer.
    Something like this:
    Code:
    Private Sub Winsock_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    Dim intI As Integer
    Dim boFound As Boolean
    Do
        '
        ' Look for a Loaded and closed Winsock
        '
        If Winsock(intI).State = sckClosed Then
            boFound = True
        Else
            intI = intI + 1
        End If
    Loop Until boFound = True Or intI > Winsock.UBound
    '
    ' If we didn't find a loaded and closed Winsock
    ' then load a new one and allocate a Buffer
    '
    If boFound = False Then
        Load Winsock(intI)
        ReDim Preserve strBuffer(UBound(strBuffer) + 1)
    '
    ' Accept the connection request
    '
    Winsock(intI).Accept requestID
    End Sub
    You'll note that there is an Index variable passed in the signature of the Winsock events, this represents the specific connection to a Client.

    When the Client closes the connection, you have to Close the corresponding Server Winsock:
    Code:
    Private Sub Winsock_Close(Index As Integer)
    '
    ' When the Client closes, close the server Winsock
    ' and flush the Buffer
    '
    Winsock(Index).Close
    strBuffer(Index) = ""
    End Sub
    To send a message to all clients you can use something like:
    Code:
    Private Sub cmdSend_Click()
    '
    ' Send a Message to all the Clients
    ' currently connected
    '
    Dim intI As Integer
    For intI = 1 To Winsock.UBound
        If Winsock(intI).State = sckConnected Then
            Winsock(intI).SendData "This is from the Server" & vbCr
        End If
    Next intI
    End Sub
    When something is received from a Client you have to buffer it until a complete message has been received. For example
    Code:
    Private Sub Winsock_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    Dim boComplete As Boolean
    Dim intPos As Integer
    Dim strData As String
    Dim strMessage As String
    '
    ' Receive the Data and add to this Client's Buffer
    '
    Winsock(Index).GetData strData
    strBuffer(Index) = strBuffer(Index) & strData
    Do
        '
        ' Look for a complete message
        ' indicated by the reception of a vbCr
        '
        intPos = InStr(strBuffer(Index), vbCr)
        If intPos > 0 Then
            '
            ' Found one, unblock it and display in a
            ' multi line textbox (txtreceived)
            '
            strMessage = Mid$(strBuffer(Index), 1, intPos - 1)
            txtreceived.Text = txtreceived.Text & "Message From: " & Winsock(Index).RemoteHostIP & _
                                " " & strMessage & vbCrLf
            '
            ' Anything left in the Buffer ?
            '
            If intPos + 1 < Len(strBuffer(Index)) Then
                '
                ' Yes move it to the front and go round the loop again
                '
                strBuffer(Index) = Mid$(strBuffer(Index), intPos + 1)
            Else
                '
                ' No, flush the buffer and exit
                '
                strBuffer(Index) = ""
                boComplete = True
            End If
        Else
            '
            ' Haven't found a complete message yet
            ' exit and wait for the next DataArrival event
            ' for this Client
            '
            boComplete = True
        End If
    Loop Until boComplete = True
    End Sub
    This assumes that everything sent to the Server from each Client is terminated with a vbCr (Carriage Return)
    Note that I have just typed this all in, it has not been tested so there may be a few typos.
    Last edited by Doogle; Dec 15th, 2010 at 01:48 AM. Reason: Corrected typos

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    5

    Re: multiple connections under one winsock control

    Hello,

    Thank You for helping me.
    I have got to know a lot from your code.i have already installed SP6.
    I would like to tell you the exact position of my project.
    I am using 15 ethernet based insrument.They are connected through LAN Cable.And from a control room a controller on a PC will connect to this all 15 instruments which are located on field at different locations.

    So,a controller will send a request to all 15 instruments simltaneously.Those instruments who are connected will Respond and send data else they will not. I have to make a command button called "START POLLING". on clicking this button, Connection Request will be sent to all these instruments.
    So for this application i have to write a code.
    Do i have to draw winsock on my form for 15 times????


    Plz Help me for a code to develop for the above application.
    Thank You.

  6. #6
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: multiple connections under one winsock control

    I would approach it as follows:

    1. Each Device would be a 'Client' and when activated would connect to the Controller ('Server'). When successfully connected, each 'Client' would send some sort of Identification to the Controller. This can be done in the Client's Connect event.

    2. Whenever a 'reading' is required, the Controller sends a message to each Device . This can be invoked through a Timer or Command Button

    3. Each Device responds to the request by sending the appropriate data to the Controller. The request can be identified in the Client's DataArrival event and on receipt can call a subroutine or function to get the data and transmit it to the Controller.

    4. The Controller processes the data and repeats from step 2.

    The Controller would look a bit like the code I posted above. Each Client would have 1 Winsock Control.

  7. #7

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    5

    Re: multiple connections under one winsock control

    Hi
    Thanks a lot for your Help!!!!!!!

  8. #8

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    5

    Re: multiple connections under one winsock control

    Hello,

    Now, At present I have made two instruments ready for communication on vb side through two winsocks as an array and communication happens properly.

    My query is, suppose if any one instrument is switched OFF then communication will stop through that instrument. So, i want on vb front end to display or indicate that ,that particular instru. is now disconnected.

    I have used a timer in which i am continuously checking whether all instr. are ON or not by using a condition as,

    If(winsock(giindex).state = connected) Then
    winsock(giindex).senddata data
    Else
    Text1.text = "Disconnected"
    Text1.Backcolour = vbRed
    End If

    But with this code i am unable to get the disconnected indication onText1
    If i switch off the instrument in between the communication is going on

    Plz Help me for another method to know whether instru. is connected or not.

    Thank You.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width