Results 1 to 10 of 10

Thread: winsock stuff

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 1999
    Location
    Oshawa, Ontario, Canada
    Posts
    88

    Red face

    I was wondering if anyone would have some code for a client and server that they have made using the winsock control that i could take a look at.....i mainly need to know how I can use winsock for a server to accept multiple client connection at once....it would be greatly appreciated if someone would help me out.

    thanx

    -KnIgHt
    -ICQ 17743984

  2. #2
    Addicted Member
    Join Date
    Jul 1999
    Location
    St-Élie d'Orford, Quebec, Canada
    Posts
    133
    Hi,

    here is how I did mine:

    1) Put a winsock control on a form with index set to 0

    2) On connection request event it will give you the index of the winsock that wants to connect to your winsocks port.

    3) load winsock(index)

    there you go, your server has 1 more connection on it's winsock control at position : Index

    Hope you have all the informations you need on how to use the winsock control because it's not funny at all !

    Hope I helped a bit ( could help you more but I'am tired for today, maybe tomorrow...)

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 1999
    Location
    Oshawa, Ontario, Canada
    Posts
    88
    ok...i'll try that but if anyone else could possibly email me a copy of a client and server that they've made please do that..

    my email is b_fett@hotmail.com

    -l8tz

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 1999
    Location
    Oshawa, Ontario, Canada
    Posts
    88
    ok...this is the code i have so far


    Option Explicit
    Dim Index As Integer
    Dim Buffer As String

    Private Sub Form_Load()
    Index = 0
    Sock(Index).Listen
    End Sub

    Private Sub Sock_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    Sock(Index).Accept requestID
    Sock(Index).SendData Str(Index)

    Index = Index + 1
    Load Sock(Index)
    End Sub

    Private Sub Sock_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    Sock (Buffer)

    If Buffer = Right("/Name:") Then
    lstNames.AddItem Right(Buffer, 7)
    End If

    End Sub


    the part at the sock_dataarrival i'm having trouble with

    how do you do a sock(index).getdata (buffer) if you don't know what number the (index) is supposed to be???

  5. #5
    New Member
    Join Date
    Nov 2000
    Posts
    1

    And here's the answer...

    You need two winsock controls:
    ListenSocket
    CommSocket(0)

    And here goes the code...

    Private Sub Form_Load()
    ListenSocket.LocalPort = 81
    ListenSocket.Listen
    CommSocket(0).LocalPort = 82
    End Sub

    Private Sub ListenSocket_ConnectionRequest(ByVal requestID as long)
    CommSocket(CommSocket.UBound).Accept requestID
    Load CommSocket(CommSocket.UBound + 1)
    CommSocket(CommSocket.UBound).LocalPort = CommSocket(CommSocket.UBound - 1).LocalPort + 1
    End Sub

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jan 1999
    Location
    Oshawa, Ontario, Canada
    Posts
    88

    Re: And here's the answer...

    ok now my code looks like this

    Option Explicit
    Dim Index As Integer
    Dim Buffer As String

    Private Sub Form_Load()
    Index = 0
    Sock(Index).Listen
    End Sub

    Private Sub Sock_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    Sock.Accept requestID
    Sock(Index).SendData Str(Index)

    CommSocket(CommSocket.UBound).Accept requestID

    Load sock2(sock2.UBound + 1)
    Sock(sock2.UBound).LocalPort = sock2(sock2.UBound - 1).LocalPort + 1
    End Sub

    Private Sub Sock_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    Sock.GetData (Buffer)

    If Buffer = Right("/Name:") Then
    lstNames.AddItem Right(Buffer, 7)
    End If

    End Sub

    but what winsock control do i use to find out which client index is sending the data????

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 1999
    Location
    Oshawa, Ontario, Canada
    Posts
    88
    sorry i made a mistake...now my code looks like this:

    Option Explicit
    Dim Index As Integer
    Dim Buffer As String

    Private Sub Form_Load()
    Index = 0
    Sock.Listen
    End Sub


    Private Sub Sock_ConnectionRequest(ByVal requestID As Long)

    CommSocket(CommSocket.UBound).Accept requestID

    Load sock2(sock2.UBound + 1)
    Sock(sock2.UBound).LocalPort = sock2(sock2.UBound - 1).LocalPort + 1

    End Sub

    Private Sub Sock_DataArrival(ByVal bytesTotal As Long)
    Sock.GetData (Buffer)

    If Buffer = Right("/Name:") Then
    lstNames.AddItem Right(Buffer, 7)
    End If
    End Sub


    so what winsock is doing what???

  8. #8
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Going in the wrong direction ....

    KnlgHt,

    You need to know the basic working of control arrays for using a single winsock control to accept more than one connection.

    The MSDN has the necessary code for you, I shall outline what I did in my sample Client-Server program.

    Put a winsock control on your form, which will act as the server. Name it tcpServer and it will have the index as 0.

    Now remember one thing. You need to put this control i.e. tcpServer(0) in the Listening mode. tcpServer(0) will always stay in the listen mode. Whenever it receives a new connection request, it loads a new winsock into the array and passes on the request to this new winsock control. Remember, tcpServer(0) should never accept the connection request by itself. The connection request must be accepted by another control in the array.

    Then you need to have a variable to track the index of the last winsock in the array. Every time you receive a new connection request on tcpServer(0), you increase this variable by 1 and that's the index of the new winsock.

    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  9. #9
    Addicted Member
    Join Date
    Jul 1999
    Location
    St-Élie d'Orford, Quebec, Canada
    Posts
    133
    ... and we have a winner : Honneybee

    You got it right, the server side winsock should be a collection and all clients should have a single winsock control wanting to connect to the server's port.

    Just a little tip : Instead of using a varable with the last index for the server's connection, use an array with constant values.

    For exemple :

    Const WIN_NOT_LOADED = 0
    Const WIN_LOADED = 1
    Const WIN_CONNECTED = 2
    const WIN_NOT_CONNECTED = 3

    dim an array of values like : Dim lWinLoaded(10)
    that you can REDIM later.

    Set all you elements in the collection to WIN_NOT_LOADED


    When you load a new instance (Accept a connection) of the control, scan your array for the next WIN_NOT_LOADED or WIN_NOT_CONNECTED element. Suppose the next available spot was 4 and that it had not been loaded yet then

    ' Do not load it again if it was WIN_NOT_CONNECTED
    if lWinloaded(4) = WIN_NOT_LOADED then

    Load Winsock(4)
    lWinloaded(4) = WIN_LOADED
    end if

    Winsock(4).LocalPort = 0
    Winsock(4).Accept requestID
    lWinloaded(4) = WIN_CONNECTED


    Now suppose your client connecte to element 4 of the Winsock sends you "Hello". This is what you should do in the DataArrival event:


    Winsock(Index).Getdata ' Index is suplied by the event



    Now in your Close event you should set your lWinloaded(Index) to WIN_NOT_CONNECTED and maybe redim your array...


    Questions ?

    Later...

  10. #10
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    KnIgHt,

    I have a small client/server demo that I created a while back to help answer questions such as yours. It also demonstrates how to pass UDTs via Winsock. Write me if you are interested: bsandlier@home.com

    As for multiple connections, there is a section devoted to that in Using the Winsock Control


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