Page 2 of 5 FirstFirst 12345 LastLast
Results 41 to 80 of 163

Thread: [RESOLVED] [2008] Can I add a TCPClient Component

  1. #41

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    Re: [2008] Can I add a TCPClient Component

    ok.. Im trying to remove the client name from lbClient
    I have this code
    Code:
        Public Sub removeClient(ByVal client As ConnectedClient, ByVal ClientName As String)
            If clients.Contains(client) Then
                clients.Remove(client)
                For x = 0 To lbClients.Items.Count - 1
                    If lbClients.Items.Item(x) = ClientName Then lbClients.Items.Remove(lbClients.Items.Item(x))
                Next
            End If
        End Sub
    as u see I created ClientName as string in the argument, but I donno how to add it to the declaration in the class
    Code:
    mParentForm.removeClient(Me)
    must be changed and I must add something, what is it?

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

    Re: [2008] Can I add a TCPClient Component

    Quote Originally Posted by perito
    ok.. Im trying to remove the client name from lbClient
    I have this code
    Code:
        Public Sub removeClient(ByVal client As ConnectedClient, ByVal ClientName As String)
            If clients.Contains(client) Then
                clients.Remove(client)
                For x = 0 To lbClients.Items.Count - 1
                    If lbClients.Items.Item(x) = ClientName Then lbClients.Items.Remove(lbClients.Items.Item(x))
                Next
            End If
        End Sub
    as u see I created ClientName as string in the argument, but I donno how to add it to the declaration in the class
    Code:
    mParentForm.removeClient(Me)
    must be changed and I must add something, what is it?
    You can actually get rid of the Clientname argument, sending the client itself is sufficient, because you can then use Client.Username to get its name.
    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)

  3. #43

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    Re: [2008] Can I add a TCPClient Component

    hmmm
    The code is complete ?

    I cant think of anything to add/change? :P
    Do you think we should improve more, or is this enough?

    The thread is resolved thanks to Atheist )

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

    Re: [2008] Can I add a TCPClient Component

    Quote Originally Posted by perito
    hmmm
    The code is complete ?

    I cant think of anything to add/change? :P
    Do you think we should improve more, or is this enough?

    The thread is resolved thanks to Atheist )
    Good to hear its all working for you now
    If everything is working as it should then you shouldnt change it. But if you need to adjust something in the future its very simple. Thats the advantage of learning how to use the System.Net.Socket classes instead of downloading ready-to-use components, its very versatile.
    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)

  5. #45

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    ok one more question,
    if I want to send small files (jpg lets say) from the server to client or back.
    will i have to change all the code? is this a new program? or can I add some things?

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

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    Quote Originally Posted by perito
    ok one more question,
    if I want to send small files (jpg lets say) from the server to client or back.
    will i have to change all the code? is this a new program? or can I add some things?
    hmm I've never tried to be honest, but it may require you to write to the stream using a System.Io.BinaryWriter instead of a System.IO.StreamWriter. And read from it using a System.IO.BinaryReader. I'll try to whip up some examples of that aswell.
    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)

  7. #47

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    k thx

  8. #48

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    I have another question,
    if I want to connect to an IRC Channel
    is this code useful? or is it a tottally different code? how can I connect to IRC channels?

  9. #49
    Addicted Member
    Join Date
    Jul 2007
    Posts
    159

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    Go to mirc.com and look around they have a link to RFC's that detail the IRC client, server and other protocols. The code Atheist has provided for the client is sufficient but there is an enormous amount of coding that will need to be done if you want to correctly make a properly functioning IRC client. Also some of the code will obviously need to be changed to deal with the IRC protocols.

    Atheist... I know a while ago we went over this whole tcp/sockets thing and u showed me collecting the clients in Hashtable but now i see you have used a List (Of ConnectedClients) - Which is better to use?

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

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    Quote Originally Posted by ^^vampire^^
    ....Atheist... I know a while ago we went over this whole tcp/sockets thing and u showed me collecting the clients in Hashtable but now i see you have used a List (Of ConnectedClients) - Which is better to use?
    A Hashtable is a collection of value/key pairs, meaning that when you add a client to it, you add its key (preferably the clients username) at the same time, so that the client object and the string key is added to the hashtable as a pair. You can then retrieve a client from it by specifying its username.
    VB.Net Code:
    1. Dim clientCollection As New HashTable
    2. clientCollection.Add("This is a username", clientObject)

    A List(Of T) is simpler, because you can not give the clients a key upon adding them. In the above example I've chosen to give each client a UserName property instead, and create my own GetClientByName function.

    You can of course use which one you'd like, but I prefer the List(Of T) way.
    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)

  11. #51
    Addicted Member kaisellgren's Avatar
    Join Date
    Jan 2006
    Posts
    149

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    How would you list all servers on client side? Also, if you block the connection with firewall the program throws an error on server side once pressing the Listen button.

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

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    Quote Originally Posted by kaisellgren
    How would you list all servers on client side? Also, if you block the connection with firewall the program throws an error on server side once pressing the Listen button.
    1. You can not list all servers without having a "master server" to keep track of the servers.
    2. Thats why you should place all connection attempts in Try-Catch blocks.
    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)

  13. #53
    Addicted Member kaisellgren's Avatar
    Join Date
    Jan 2006
    Posts
    149

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    Quote Originally Posted by Atheist
    1. You can not list all servers without having a "master server" to keep track of the servers.
    2. Thats why you should place all connection attempts in Try-Catch blocks.
    So all games like Counter-Strike have their servers oin a global master server? How does this sound:

    1) When server is created, the server sends http request to www.master.com/addserver.php?ip=xxxx
    2) When client window opens it sends http request to www.master.com/getservers.php and then it lists all IPs.
    3) When server closes it sends http request to www.master.com/removeserver.php?ip=xxxx

    Sounds good?

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

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    Quote Originally Posted by kaisellgren
    So all games like Counter-Strike have their servers oin a global master server?
    Yeah, one or more master servers.
    Quote Originally Posted by kaisellgren
    How does this sound:

    1) When server is created, the server sends http request to www.master.com/addserver.php?ip=xxxx
    2) When client window opens it sends http request to www.master.com/getservers.php and then it lists all IPs.
    3) When server closes it sends http request to www.master.com/removeserver.php?ip=xxxx

    Sounds good?
    Yeah that will work, except for the fact that anyone can use their browser and remove/add servers just to ruin the "system".
    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)

  15. #55
    Addicted Member kaisellgren's Avatar
    Join Date
    Jan 2006
    Posts
    149

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    This isn't working

    Code:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Try
                listener = New System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, 43001) 'The TcpListener will listen for incoming connections at port 43001        
                listener.Start() 'Start listening.
                listenThread = New System.Threading.Thread(AddressOf DoListen) 'This thread will run the doListen method        
                listenThread.IsBackground = True 'Since we dont want this thread to keep on running after the application closes, we set isBackground to true.        
                listenThread.Start() 'Start executing doListen on the worker thread.   
            Catch ex As Exception
                Timer1.Enabled = False
                Timer1.Interval = 5000
                Timer1.Enabled = True
                Exit Sub
            End Try
        End Sub
    I have the same code in initial button press.

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

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    Quote Originally Posted by kaisellgren
    This isn't working

    Code:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Try
                listener = New System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, 43001) 'The TcpListener will listen for incoming connections at port 43001        
                listener.Start() 'Start listening.
                listenThread = New System.Threading.Thread(AddressOf DoListen) 'This thread will run the doListen method        
                listenThread.IsBackground = True 'Since we dont want this thread to keep on running after the application closes, we set isBackground to true.        
                listenThread.Start() 'Start executing doListen on the worker thread.   
            Catch ex As Exception
                Timer1.Enabled = False
                Timer1.Interval = 5000
                Timer1.Enabled = True
                Exit Sub
            End Try
        End Sub
    I have the same code in initial button press.
    How come?
    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)

  17. #57
    Addicted Member kaisellgren's Avatar
    Join Date
    Jan 2006
    Posts
    149

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    Ooh now it's working. Cool. All problems seem to be solved now

    Now I need to figure out how to send http requests and how to get responses back.

  18. #58
    Addicted Member kaisellgren's Avatar
    Join Date
    Jan 2006
    Posts
    149

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    Okay everything is working great except I have slight problems...

    Code:
    Error	3	'GetStream' is not a member of 'Chatter.Client'.	C:\Documents and Settings\Kai\My Documents\Visual Studio 2008\Projects\Chatter\Chatter\Private.vb	9	38	Chatter
    This is the error I keep getting.

    I have a main form which is the client. In client I have a button "Open private chat window" which opens a private chat window. I have a private.vb form similar to my main chat window. The only problem is the above, my private.vb seems not to be able to use the connection code anymore, so:

    How to make a new form/window to use existing connection used by main.vb?

  19. #59
    Member
    Join Date
    Feb 2007
    Posts
    53

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    Thanks for the excellent code snippets, this thread got me quite far.

    However, i have a problem: I am transfering strings between the client and the server (both ways), and those messages then get interpreted. When things happen/events fire at roughly the same time (i guess its because of that), the messages get corrupted and mixed up. Is there a way to rewrite the SendMessage method so, that i can be sure the string gets sent and recieved completly and nothing gets in between?

    Another question: i am sending lots of values of type double. The way i do it now, i convert the double to a string, use the SendMessage method from the example in this thread, and on the other end i am converting the string back to a double. There are a lot of conversions going on, is there a more efficient way of doing this with sockets?

    Any hints appreciated,
    P.

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

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    Quote Originally Posted by pippi
    Thanks for the excellent code snippets, this thread got me quite far.

    However, i have a problem: I am transfering strings between the client and the server (both ways), and those messages then get interpreted. When things happen/events fire at roughly the same time (i guess its because of that), the messages get corrupted and mixed up. Is there a way to rewrite the SendMessage method so, that i can be sure the string gets sent and recieved completly and nothing gets in between?
    Hmm. I've never had this happen to me, are you calling the Flush() method after each string being sent?
    Have you made any other modifications or is the code on the first page of this thread basically what you have?

    Edit: Are you saying that one clients message is mixed with another clients message? Or are two messages from the same client mixed together?
    Quote Originally Posted by pippi
    Another question: i am sending lots of values of type double. The way i do it now, i convert the double to a string, use the SendMessage method from the example in this thread, and on the other end i am converting the string back to a double. There are a lot of conversions going on, is there a more efficient way of doing this with sockets?

    Any hints appreciated,
    P.
    I've never tried any of this so Im not 100% sure of how to go about it.
    There's an overload of the StreamWriters Write method that'll let you write a double value directly to the stream. If you're only sending Doubles on the stream, you could modify the receiving end to only read 8 bytes at a time ( 1 double = 8 bytes), if you're sending doubles OR strings depending on the situation, it gets a bit more tricky. You'd have to add something like a header to your data before you send it that tells the receiving end what type of data its getting.
    You'd have to modify the receiving end to
    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)

  21. #61
    Member
    Join Date
    Feb 2007
    Posts
    53

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    Atheist: yes, 2 message-strings from the same client mixed/appended, and also some messages from the server to the client. I will double check if id did something different than in your code you posted. I use the Flush() method too. Could it be problematic that the SendMessage method is called from different threads?

    Thanks for your suggestion about the double values, i will check it out. I will still need to send strings too tho.

    Regards,
    Pippi

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

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    The reason why 2 messages from the same client are concatenated is because a stream is open between the client and the server, and data can be pushed out continously. If one message is sent just after the other, they will most likely arrive at the receiving end at the same time. You'd need some character or other delimiter to mark the "end of message", so that you can separate one message from the other. I will update my example code with this implementation tomorrow.
    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)

  23. #63
    Member
    Join Date
    Feb 2007
    Posts
    53

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    Thanks a lot, Atheist.

  24. #64
    Member
    Join Date
    Feb 2007
    Posts
    53

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    I think what happens is that my server sends so many messages in such a short time, that too many messages are concatenated that the resulting string exceeds the 256 byte readbuffer of the clients. I guess due to this the clients dont recieve all data (some data will get lost when the server sends a string that exceeds the clients readbuffer, right?), and that some incomplete messages reach the clients messageRecieved method, where i want to interprete and react to incoming messages.

    I guess i need to find a way to interprete concatenated messages and make sure that data is sent in such a way that the predefined size of the readbuffer will suffice.

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

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    Yeah I was thinking of replacing that buffer with a loop that continously reads from the stream, appends everything thats being read to a stringbuilder, and when an "end of message" delimiter is found, it raises a "message received" event and passes the contents of the stringbuilder. It would then reset the stringbuilder and continue reading from the stream. This way nothing will be lost and you will get each message separately.
    I am unfortunately not at home yet so I cant change my code example like i said, not until sunday evening swedish time.
    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)

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

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    I have thrown together this example, its the ConnectedClient class, but you'd do the same in code on the client side.
    I havent tested this at all, so it probably has some errors in it.

    VB.Net Code:
    1. Public Class ConnectedClient
    2.     Private mClient As System.Net.Sockets.TcpClient
    3.  
    4.     Private mUsername As String
    5.     Private mParentForm As Form1
    6.     Private readThread As System.Threading.Thread
    7.  
    8.     Public Event dataReceived(ByVal sender As ConnectedClient, ByVal message As String)
    9.  
    10.     Sub New(ByVal client As System.Net.Sockets.TcpClient, ByVal parentForm As Form1)
    11.         mParentForm = parentForm
    12.         mClient = client
    13.  
    14.         readThread = New System.Threading.Thread(AddressOf doRead)
    15.         readThread.IsBackground = True
    16.         readThread.Start()
    17.     End Sub
    18.  
    19.     Public Property Username() As String
    20.         Get
    21.             Return mUsername
    22.         End Get
    23.         Set(ByVal value As String)
    24.             mUsername = value
    25.         End Set
    26.     End Property
    27.  
    28.     Private Sub doRead()
    29.         Const BYTES_TO_READ As Integer = 255
    30.         Dim readBuffer(BYTES_TO_READ) As Byte
    31.         Dim bytesRead As Integer
    32.         Dim sBuilder As New System.Text.StringBuilder
    33.         Do
    34.             bytesRead = mClient.GetStream.Read(readBuffer, 0, BYTES_TO_READ)
    35.             If (bytesRead > 0) Then
    36.                 Dim messageDelimiterIndex As Integer = Array.IndexOf(readBuffer, 13)
    37.  
    38.                 If (messageDelimiterIndex > -1) Then
    39.                     'If the value 13 (ControlChars.Cr) was found in the message, we have found the
    40.                     'end of the current message, so what we need to do is add the last piece of the message
    41.                     'to the stringbuilder, raise the dataReceived event, create a new blank stringbuilder,
    42.                     'and append the remaining text to the new stringbuilder
    43.                     sBuilder.Append(System.Text.Encoding.UTF8.GetString(readBuffer, 0, messageDelimiterIndex))
    44.                     RaiseEvent dataReceived(Me, sBuilder.ToString)
    45.                     sBuilder = New System.Text.StringBuilder
    46.                     sBuilder.Append(System.Text.Encoding.UTF8.GetString(readBuffer, messageDelimiterIndex + 1, bytesRead - (messageDelimiterIndex + 1)))
    47.                 Else
    48.                     'The value 13 was not found in the message, so we just append everything to the stringbuilder.
    49.                     sBuilder.Append(System.Text.Encoding.UTF8.GetString(readBuffer, 0, bytesRead))
    50.                 End If
    51.             End If
    52.         Loop
    53.     End Sub
    54.  
    55.     Public Sub SendMessage(ByVal msg As String)
    56.         Dim sw As IO.StreamWriter
    57.         Try
    58.             SyncLock mClient.GetStream
    59.                 sw = New IO.StreamWriter(mClient.GetStream) 'Create a new streamwriter that will be writing directly to the networkstream.
    60.                 sw.Write(msg)
    61.                 sw.Flush()
    62.             End SyncLock
    63.         Catch ex As Exception
    64.             MessageBox.Show(ex.ToString)
    65.         End Try
    66.         'As opposed to writing to a file, we DONT call close on the streamwriter, since we dont want to close the stream.
    67.     End Sub
    68.  
    69. End Class

    As you can see, it uses the ControlChars.Cr as a message delimiter (byte value of 13).
    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)

  27. #67
    Member
    Join Date
    Feb 2007
    Posts
    53

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    Quote Originally Posted by Atheist
    I have thrown together this example, its the ConnectedClient class, but you'd do the same in code on the client side.
    I havent tested this at all, so it probably has some errors in it.
    Thanks for the code. Would a solution like this also solve the problem that its possible that the server (or the client) can send a concatenated string that is bigger than the readbuffer?

    Regards,
    P.

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

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    Quote Originally Posted by pippi
    Thanks for the code. Would a solution like this also solve the problem that its possible that the server (or the client) can send a concatenated string that is bigger than the readbuffer?

    Regards,
    P.
    Yes, since this method will just keep appending to a stringbuilder until an end-of-message constant is found, you'd be able to send messages of practically unlimited length. (Oh well, atleast until the stringbuilders max capacity is reached)
    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)

  29. #69
    Member
    Join Date
    Feb 2007
    Posts
    53

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    Very nice, i will try that out. Thanks again, this is exactly what i needed.

  30. #70
    Member
    Join Date
    Feb 2007
    Posts
    53

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    That worked like a charm.

    However, if i do too much calculations in the messageRecieved method in the client, while the client recieves lots of messages, it cant keep up and looses data. If i only have for example a message counter in the messageRecieved method, i dont loose any messages at all.

    Im thinking of just adding the message to a queue in the messageRecieved method, and have a seperate thread work this queue and do all the other stuff in there. Maybe that helps.

    Regards,
    P.

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

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    The MessageReceived subroutine is an eventhandler for the DataReceived event, which is raised on the same thread that reads from the stream. You should place very little actual code in that subroutine, and instead invoke methods to execute on the main thread when you want to perform any kind of calculations or UI integration.


    Edit: silly typos gives my post a whole new meaning
    Last edited by Atheist; Feb 17th, 2008 at 08:02 PM.
    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)

  32. #72
    Member
    Join Date
    Feb 2007
    Posts
    53

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    I think i have found a problem with the new DoRead method (if i understand your code right): when there is more than 2 messages concatenated, it should send all messages to the messageRecieved method, it does however only send the first, and assumes that the second part is an incomplete message, while it is possible that there is more than 1 complete message in the readBuffer.

    Sometimes i get 5 or more concatenated messages, so i will have to find a way to seperate them and send them all at once. *scrathcing head*

    Regards,
    P

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

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    Quote Originally Posted by pippi
    I think i have found a problem with the new DoRead method (if i understand your code right): when there is more than 2 messages concatenated, it should send all messages to the messageRecieved method, it does however only send the first, and assumes that the second part is an incomplete message, while it is possible that there is more than 1 complete message in the readBuffer.

    Sometimes i get 5 or more concatenated messages, so i will have to find a way to seperate them and send them all at once. *scrathcing head*

    Regards,
    P
    Oh well yeah I suppose you could just convert it all into a string and split it on each occurance of the end of message delimiter. Again this is untested code:

    VB.Net Code:
    1. Private Sub doRead()
    2.         Const BYTES_TO_READ As Integer = 255
    3.         Dim readBuffer(BYTES_TO_READ) As Byte
    4.         Dim bytesRead As Integer
    5.         Dim sBuilder As New System.Text.StringBuilder
    6.         Do
    7.             bytesRead = mClient.GetStream.Read(readBuffer, 0, BYTES_TO_READ)
    8.             If (bytesRead > 0) Then
    9.                 Dim message As String = System.Text.Encoding.UTF8.GetString(readBuffer, 0, bytesRead)
    10.                 If (message.IndexOf(ControlChars.Cr) > -1) Then
    11.  
    12.                     'Since we know that the string contains our special delimiter (ControlChars.Cr) we can split the string on each occurance of it.
    13.                     Dim subMessages() As String = message.Split(ControlChars.Cr)
    14.  
    15.                     'The first element in the subMessages string array must be the last part of the current message.
    16.                     'So we append it to the StringBuilder and raise the dataReceived event
    17.                     sBuilder.Append(subMessages(0))
    18.                     RaiseEvent dataReceived(Me, sBuilder.ToString)
    19.                     sBuilder = New System.Text.StringBuilder
    20.  
    21.                     'If there are only 2 elements in the array, we know that the second one is an incomplete message,
    22.                     'though if there are more then two then every element inbetween the first and the last are complete messages:
    23.                     If subMessages.Length = 2 Then
    24.                         sBuilder.Append(subMessages(1))
    25.                     Else
    26.                         For i As Integer = 1 To subMessages.GetUpperBound(0) - 1
    27.                             RaiseEvent dataReceived(Me, subMessages(i))
    28.                         Next
    29.                         sBuilder.Append(subMessages(subMessages.GetUpperBound(0)))
    30.                     End If
    31.                 Else
    32.  
    33.                     'ControlChars.Cr was not found in the message, so we just append everything to the stringbuilder.
    34.                     sBuilder.Append(message)
    35.                 End If
    36.             End If
    37.         Loop
    38.     End Sub

    Might not even be the best way to go about it
    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)

  34. #74
    Member
    Join Date
    Feb 2007
    Posts
    53

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    Thanks, Atheist. I got it all working now, altho i have done it a bit differently. I just check if the last read byte is a message delimiter, and if not, i keep the last part of the message (i dont want to post it here in this forum-section, because i use C#).

    Again, many thanks for your help.

  35. #75
    Lively Member
    Join Date
    Aug 2007
    Posts
    107

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    hey im looking at this and im testing it out. I got 1 question, how do i send a message from the client to the server so the server sends it out to all the other clients connected?

    Im using
    Code:
    SendMessage(txtMsg.Text)
    on it but i want to use the username stuff like u said above so when multiple clients are on they know who is talking

    Or should i just do
    Code:
    SendMessage(txtUsername.Text & ":" & txtMsg.Text)

    also i set up a txtLog thing on the server so i can see what is being typed, but when the client sends data it reaches the server but the server doesnt send the data to the other clients
    Bleep Bloop

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

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    Remember that you as the programmer is the designer of the protocol that your chat application uses. You decide what "commands" a client can send to the server, and how the server should react to a given command. In my example I only have 2 available commands for the clients: CONNECT and DISCONNECT. You'll have to make the server capable of receiving yet another command, that takes 2 more fields of data: sender name and message. Upon receiving this command it'd be appropriate to iterate through the connected clients and send a command to each one of them that notifies them of this new message.
    Last edited by Atheist; Feb 23rd, 2008 at 10:09 PM.
    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)

  37. #77

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    I'm having trouble with getting the client to send a message to all the clients. I've tried several things, including making the client also a server to send messages to the server, which didn't work. Do you think you could post the entire project here?

    [Edit]
    I got it to work, but it only allows one user to talk to one other user.
    Could you only get two users to work?
    Last edited by digitalcircuit36939; Apr 27th, 2008 at 09:46 AM.

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

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    I dont understand what you mean. Are you having problems with letting two clients chat with eachother?
    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)

  39. #79

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    Actually, I did get the clients to communicate by making each also a server, but I can not get more than two clients to chat at the same time.
    1. The first chat program connects to the second chat client, and can send messages to the second.
    2. Then the second chat program connects to the first, and can send messages to the first.

    Using the original code, I could not get the server to receive messages from the clients. The server could only send messages to the clients. That's why I had to use the workaround mentioned above.

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

    Re: [RESOLVED] [2008] Can I add a TCPClient Component

    If client nr1 has already connected to client nr2, theres no need for client nr2 to connect to client nr1 again, and create a second TCP connection. Data can be sent both ways through an the first existing TCP connection.

    However, this is not the way you'd want to go about doing this.
    In my example, it is possible for the clients to send data to the server, and for the server to send data to the clients. With these two basic abillities you can write a simple chat application.
    In order for client nr1 to send messages to client nr2 through the server, client nr1 needs to send the message he wants client nr2 to receive to the server, along with something that specifies that the message is supposed to be delivered to client nr2. The server would then send the message on to client nr2, and specify that it came from client nr1.

    This is, of course, a simple chat service. As long as the number of people using the service simultaneously isnt too great, it'll be alright. But if you want to write a distributed application for chatting where the number of users would be very high, you'd need to go with a client/server-p2p hybrid approach.
    Last edited by Atheist; May 11th, 2008 at 05:33 PM.
    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)

Page 2 of 5 FirstFirst 12345 LastLast

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