Page 2 of 2 FirstFirst 12
Results 41 to 65 of 65

Thread: [2005] TCP socket server example?

  1. #41

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    Denmark
    Posts
    291

    Re: [2005] TCP socket server example?

    Quote Originally Posted by Atheist
    You have mixed the code with alot of old VB6 code. You shouldnt do that in VB .Net.
    Besides, why have you changed so much of the original code?
    Well, I modified it to suit my needs better, and the TCP version of it works. Can you try building a UDP client for me as well so I could modify it too? I'll give you credits

  2. #42
    Addicted Member
    Join Date
    Jul 2007
    Posts
    159

    Re: [2005] TCP socket server example?

    Hey, i'm working on a project using a TCP server and used your code. I was looking at the client side of things and no matter how much i try, it won't send text to the server. It pops up with that error message asking if i'm sure it is connected to the server, but it is. What could possibly be wrong?

  3. #43
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] TCP socket server example?

    Well have you also created a server application and if so, is it running? What IP does it have? Are you sure you've set the same port numbers on both server and client?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  4. #44
    Addicted Member
    Join Date
    Jul 2007
    Posts
    159

    Re: [2005] TCP socket server example?

    I made a separate server app from your code and a separate client from your code. I cross tested it with a delphi server and client i made a while back. With both the delphi client and vb client it connected to the vb server fine. I also tried connecting the delphi client and vb client to the delphi server and that checked out fine too, but as soon as i use the SendData() method you supplied for the vb client it keeps returning the same error message.

    For some reason the data won't go through the stream and was just wondering if you might have some ideas. I'll tinker with it more later on and see if i'm doing something wrong.
    Thanks.

  5. #45
    Member
    Join Date
    Nov 2007
    Posts
    32

    Re: [2005] TCP socket server example?

    Quote Originally Posted by Atheist
    VB Code:
    1. Dim sr As IO.StreamReader
    2.         Do
    3.             Try
    4.                 Dim client As Net.Sockets.TcpClient = listener.AcceptTcpClient
    5.                 sr = New IO.StreamReader(client.GetStream)
    6.                 If sr.ReadToEnd = "Register" Then
    7.                     Clients.Add("Mr Bean", client)
    8.                 End If
    9.                 sr.Close()
    10.             Catch
    11.             End Try
    12.         Loop
    13.     End Sub
    14.  
    15.     Private Function SendToUser(ByVal user As String, ByVal data As String) As Boolean
    16.         If Clients.ContainsKey(user) Then
    17.             Dim sw As New IO.StreamWriter(Clients.Item(user).GetStream)
    18.             sw.Write(data)
    19.             sw.Flush()
    20.             sw.Close()
    21.             Return True
    22.         Else
    23.             Return False
    24.         End If
    25.     End Function

    In this example, if a client sends "Register" to the server, the server adds the client to a Dictionary along with a unique string that identifies it (altough in this case, the string will not be unique, but you will need to change that). And the SendToUser function pretty much speaks for itself.
    Atheist,
    I wanted to thank you for such a great tutorial on the use of sockets.
    I have a minor questions about the "register" feature.
    Would it be possible to have multiple users with the same username - so I could in theory send a bulk update message to a group of people with only the updated information that affects that group of users?

  6. #46
    Addicted Member
    Join Date
    Jul 2007
    Posts
    159

    Re: [2005] TCP socket server example?

    As far as I know that is possible (i haven't tried it but think it happened a few times in my experience) but would be a bad idea if you want to later reference single messages and deal with disconnects of single users.

    I would recommend either when the user connects they send through a message saying which group they want to join or something like that. I also have an example of a server which makes each connection from a client an instance of a ClientConnection class which could be quite useful depending on what you want to do aswell as if you want to store lots of information for each client. Let me know and i'll give you a tutorial in it if you want.

  7. #47
    Member
    Join Date
    Nov 2007
    Posts
    32

    Re: [2005] TCP socket server example?

    basically what I am attempting to do is push updated xml from the server to several groups of clients.there is only going to be 8 different groups and i would like to fire off XML data to each group of users by sending out 8 different messages in sequence. I am trying to limit the hammering that happens on the server when 300 people ask for the same data at the same time. I thought it would be better to have 1 server applet request the data then push said data out. I looked at xml webservices but I didn't see a way to push data to the app without continually polling the service.
    I am not planning on referencing the data after its used 1 time. I would enjoy looking at the examples you have.

  8. #48
    Addicted Member
    Join Date
    Jul 2007
    Posts
    159

    Re: [2005] TCP socket server example?

    I tried the whole logging of more than one user with the same username using a hashtable and it doesn't seem to like it. I've provided a project called tcpServer that shows you how to deal with each connection as an object of a ClientConnection class. Let me know if you need anything explained. I guess you could add a property to the ClientConnection class as trying to group the connections. If I come up with any other ideas to solve the problem i'll let you know.

    code is in vb2005.
    Attached Files Attached Files

  9. #49
    Member
    Join Date
    Nov 2007
    Posts
    32

    Re: [2005] TCP socket server example?

    I appreciate the help on this and I will look at the example in the morning. have a kinda silly question about all this - if I send a group based message will it send them all at once or 1 at a time till everyone is updated.
    trying to achieve a burst update

  10. #50
    Addicted Member
    Join Date
    Jul 2007
    Posts
    159

    Re: [2005] TCP socket server example?

    Well the way the server works is it cycles through all the connected users and will send them each a message one at a time. If you put the users into groups then with a few minor changes to the server code you can cycle through just those select connected users.
    Unless you are sending a huge amount of data to each client it should be quite quick. I'm not really sure if there is any other way of sending data quicker and more efficiently to the clients though . I'll have a look around and see if there are other ways of doing this later today.

  11. #51
    Member
    Join Date
    Nov 2007
    Posts
    32

    Re: [2005] TCP socket server example?

    the more I think about it the better the idea sounds of sending messages individually in order instead of sending a massive bulk message - that way you limit the spikes the server outbound bandwidth would see every time it sends out an update. In the code you provided on the client login
    Code:
    Private Sub OnLineReceived(ByVal client As ClientConnection, ByVal data As String)
            '
            ' NOTE: Messages received from client in following form:
            ' COMMAND|PARAMETER1|PARAMETER2   etc...
            ' dataArray(0) = COMMAND
            ' dataArray(1) = PARAMETER1
            ' dataArray(2) = PARAMETER2
            '
            data = data.Substring(0, data.Length - 1)
            Dim dataArray() As String
            dataArray = data.Split("|")
            Select Case (dataArray(0))
                Case "LOGIN"
                    ' Logs in the client - Message received from clients in following form:
                    ' LOGIN|Username
                    LoginClient(client, dataArray(1))
            End Select
        End Sub
    in the dataArray's that your sending to the server could you add what group you need to be in there? if a user moved to another group would it be better to remove them from the session then log them back in under the new group?

  12. #52
    Addicted Member
    Join Date
    Jul 2007
    Posts
    159

    Re: [2005] TCP socket server example?

    Didn't even think about the bandwidth spikes but yeah not bad thinking at all.

    For your first question - are you talking about putting the client into a group during the login process? If so then you need to add an extra property to the ClientConnection class for a group so you can later reference that connection to that group and then simply send the group name through on the login message. Something like LOGIN|GROUPNAME|USERNAME. It seems complicated but if you need an example to show you how it would probably make it a lot easier.

    For you second question - if you deal with the connections like I've described above then it would be simply a matter of sending a different kind of message to the server to change your current group. No logging off and on would be necessary at all.

  13. #53
    Member
    Join Date
    Nov 2007
    Posts
    32

    Re: [2005] TCP socket server example?

    I have the adding of users to groups handled already.
    There are only 3 real questions left I haven't asked yet.
    since the nature of this service will be the updating of client and I wont be sending much except the login information to the server.
    would you recommend going with the RDM type of socket as mentioned on MSDN
    http://msdn2.microsoft.com/en-us/lib...pe(VS.80).aspx
    "Rdm -
    Supports connectionless, message-oriented, reliably delivered messages, and preserves message boundaries in data. Rdm (Reliably Delivered Messages) messages arrive unduplicated and in order. Furthermore, the sender is notified if messages are lost. If you initialize a Socket using Rdm, you do not require a remote host connection before sending and receiving data. With Rdm, you can communicate with multiple peers. "

    The second question is about the sending of the xml update message - should I pack it up into a file and send it or should I just send it as a message?
    do you have an example for sending a file in sockets

    3rd question is - if a client needed to send a message out to all the other clients attached to the server how would this be handled?

  14. #54
    Addicted Member
    Join Date
    Jul 2007
    Posts
    159

    Re: [2005] TCP socket server example?

    I've never tried RDM but it seems pretty much like the UDP protocol which even if it does notify you about missing packets will still require you to resend the updates which i'm guessing is more hassle than it's worth.

    If you sending updates as a whole file to replace a previous one then i recommend sending the file instead of text - search the forum i remember there being a thread about it somewhere here on vbforums.

    For broadcasting messages make the client send: "BROADCAST|Message" and handle it like so in the OnlineReceived event:

    Code:
    Private Sub OnLineReceived(ByVal client As ClientConnection, ByVal data As String)
    '
    ' CODE
    '
       Select Case (dataArray(0))
          Case "LOGIN"
             ' Logs in the client - Message received from clients in following form:
             ' LOGIN|Username|Groupname
             LoginClient(client, dataArray(1), dataArray(2))
          Case "BROADCAST"
            ' Broadcast message to all clients - Message received from clients
            ' in following form: BROADCAST|MESSAGE
             Broadcast(dataArray(1))
         End Select
       '
       ' CODE
       '
        End Sub

  15. #55
    Member
    Join Date
    Nov 2007
    Posts
    32

    Re: [2005] TCP socket server example?

    very nice, thank you for the help on this.
    have a good one and enjoy the holidays.

  16. #56
    Addicted Member
    Join Date
    Jul 2007
    Posts
    159

    Re: [2005] TCP socket server example?

    Glad I could help. Feel free to ask any other questions you have.
    Thanks and have a great holiday too.

  17. #57
    Lively Member
    Join Date
    Jul 2007
    Location
    Mechelen
    Posts
    119

    Re: [2005] TCP socket server example?

    How can i let the server broadcast listboxs, my clients can t chat or anything but they can jump to 4 listboxes, A B C D, so if a person that is in room(listbox) A and moves to roomD (listbox) then i want the other clients to see him olso in room D and not more in A. Ty .

  18. #58
    Member
    Join Date
    Nov 2007
    Posts
    32

    Re: [2005] TCP socket server example?

    ^^vampire^^
    on the program load I initially pull the data needed from the database - when you mentioned the broadcast command I started rethinking the idea of sending an update xml file and started thinking about just sending the updated data across as a line of text in a broadcast. have you ever taken a line of text from a broadcast and used it to update an array?
    next how could you make the updated information conditional on a parameter inside the line of text.
    say the group number sent isnt the group number your associated with, could you set it so it would ignore any info thats not needed?
    think if I handle things this way it should be much more efficient and update the program with only the info needed in almost real-time

  19. #59
    Addicted Member
    Join Date
    Jul 2007
    Posts
    159

    Re: [2005] TCP socket server example?

    1 - Can't say i really have but what info is going to be in that array and what is the array for exactly?

    2 - For your second question do you mean that you're trying to separate which information goes to which group [eg] if you receive a message from a client BROADCAST|GROUP|MESSAGE that the message is only broadcast to the members of that group?

  20. #60
    Addicted Member
    Join Date
    Jul 2007
    Posts
    159

    Re: [2005] TCP socket server example?

    Maxim -
    What kind of program are you trying to make?
    Can the client only be in one listbox at a time? If so then are you only using the 4 listboxes on the server form to keep track of the clients?

    If so then it's just a case of adding a property to the ClientConnection class for the group they will be in and when changing groups receiving a message from the client saying they want to change group [eg] CHANGEGROUP|NEWGROUP. When this happens before changing the clients group property you check what the current group is, remove them from that listbox, send a message to the other clients letting them know he left that group, change the clients group property, and then let the clients know that he has joined the new group.

    If you need a coding example let me know although it might be somewhat lengthy.

  21. #61
    Lively Member
    Join Date
    Jul 2007
    Location
    Mechelen
    Posts
    119

    Re: [2005] TCP socket server example?

    Ok i am trying it Vampire, by the way If someone computers crashes will I then see that the client is disconnected ? (can t test it since 1 pc only atm ). If not how can i make so if a (client) pc crashes that i remove the client from that hashes thingie ?
    Last edited by Maxim; Dec 23rd, 2007 at 07:15 AM.

  22. #62
    Member
    Join Date
    Nov 2007
    Posts
    32

    Re: [2005] TCP socket server example?

    Quote Originally Posted by ^^vampire^^
    1 - Can't say i really have but what info is going to be in that array and what is the array for exactly?

    2 - For your second question do you mean that you're trying to separate which information goes to which group [eg] if you receive a message from a client BROADCAST|GROUP|MESSAGE that the message is only broadcast to the members of that group?
    the array I have is used to store who is in what group and some other network info. There is a structure associated with the array and I was thinking about using BROADCAST|GROUP|MESSAGE|param1|param2|.... and separating all the data into the proper place in the array. its a total of less than 200 characters of text

  23. #63
    Addicted Member
    Join Date
    Jul 2007
    Posts
    159

    Re: [2005] TCP socket server example?

    any kind of disconnect whatsoever will be caught and dealt with in the DisconnectClient sub whether they disconnect by themselves or if their pc crashes.

  24. #64
    Addicted Member
    Join Date
    Jul 2007
    Posts
    159

    Re: [2005] TCP socket server example?

    i've added the updated code for you.
    Look at the following subs for changes.

    OnlineReceived - extra parameter for groupname
    LoginClient - adds client to a group on login
    Broadcast - receives new parameter for group sending
    ClientConnection - now has a groupname property

    also look at the very bottom of the code and read the comments. it tells you how to add new commands etc. With the updated ClientConnection class you wont need an array to hold who is in which group unless that is required for something else. Why not just use a few variables to store this network info instead of an array or is the array for a specific purpose?
    Attached Files Attached Files
    Last edited by ^^vampire^^; Dec 23rd, 2007 at 09:46 AM.

  25. #65
    Member
    Join Date
    Nov 2007
    Posts
    32

    Re: [2005] TCP socket server example?

    using the array as a somewhat orderly way to store the data with a structure to make it easier to fill the list view for display of the data.
    will take a look at the code and let you know if I have any further questions.
    once again I really appreciate the help on this, take care.

Page 2 of 2 FirstFirst 12

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