Help with Sockets

I cant figure out how i would have a multi user chat because there are no socket arrays. Please Help.



Answer this question

Help with Sockets

  • jhobitz

    Are there any already made class's for a multiple socket array I cant figure out how i would make a multi user chat.


  • s_karthik_au

    Get a book on socket programming, and on multithreading: you don't need a 'socket array' - you may have needed it in VB6, but no longer.

    1. Create a Socket 'server' (TCPListener) which listens for connections on a certain port.

    2. Each time a request is pending, create a Client object that has a TCPClient in it that accepts the connection.

    3. Handle all the chatter on that connection in the client object.

    Preferably, the listener would listen on its own thread, and the Client Object will handle all the chatter on ye another thread (a thread for each client).

    The book "Visual Basic .NET Programers Cookbook" by Microsoft Press has a good example (among many, many other techniques for doing loads of other stuff).

    (What Frank says is what you are asking for, but I think learning a bit about techniques you can use is what you need).



  • pinkpanter

    I suspect we are talking at cross-purposes here. What do you mean by "a multiple socket array" and how does it differ from something declared like this ...


    Dim foo() as Socket


  • Deepak Juneja

    What makes you think you can't have an array of sockets This should work...


    Dim sockArray() As Socket

    ' Make the array 10 elements long, each element initialised to null
    sockArray = Array.CreateInstance(GetType(Socket), 10)

    ' set one element to point to an actual socket
    sockArray(5) = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

    ' make the array bigger
    Array.Resize(sockArray, 20)

    An array isn't the only way to store a number of sockets however. Depending on how you want to access/manipulate the various sockets, one of the classes from the System.Collections.Generic namespace might be a better choice.

    Which collection class you choose to use depends on a number of factors - the types of access you need, how efficiently the class stores the data you need to store, speed of access etc.

    Just as an example, here's how to use the Dictionary<Tkey, Tvalue> class to store your sockets in a way that allows them to be accessed by user name (not a very efficient key perhaps but it is only an example)


    Dim sockets As Dictionary(Of String, Socket)
    Dim s As Socket

    s = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

    '' add s to the dictionary using the userr name as the key
    sockets.Add("Bob Smith", s)

    'access the available property for Bob Smith's socket
    bytes = sockets("Bob Smith").Available


  • Help with Sockets