1 Attachment(s)
[RESOLVED] TCP/IP - Host can't display received messages :(
Hi guys, I can't get the host/server to display messages from the client.
It receives the messages but when I try and get it to put it into a listbox it says it's added it but is doesn't add anything.
Full project source attached.
Please help me ASAP as this is very important :)
Thanks in advance, knxrb
Re: TCP/IP - Host can't display received messages :(
I dont suppose you could just post the code snippets that does the actual sending/receiving?
Re: TCP/IP - Host can't display received messages :(
Here's the code for when the host receives the message, when I put a messagebox before or after adding it to the listbox it shows the correct message in the messagebox, it just won't show it in the listbox :(
Code:
Public Sub Message(ByVal sender As ConnectedClient, ByVal message As String)
Dim data() As String = message.Split("|")
If data(0) = "CONNECT" Then
sender.Username = data(1)
ElseIf data(0) = "DISCONNECT" Then
Host.removeClient(sender)
Else
''data(0) = username & data(1) = message
Dim strMessage As String = data(0) & ": " & data(0)
Host.lstJMessages.Items.Add(strMessage)
End If
End Sub
Re: TCP/IP - Host can't display received messages :(
From where are you calling the Message subroutine? Are you executing the method on the same thread that the ListBox was created on?
Re: TCP/IP - Host can't display received messages :(
Quote:
Are you executing the method on the same thread that the ListBox was created on?
Aah! Each client is on a seperate thread so it won't be calling it on the listbox thread!
Do you know how do I solve this?
knxrb
Re: TCP/IP - Host can't display received messages :(
Quote:
Originally Posted by
knxrb
Aah! Each client is on a seperate thread so it won't be calling it on the listbox thread!
Do you know how do I solve this?
knxrb
That would be the cause then:)
You'd need to do some safe cross-thread calls. jmcilhinney has a good article on this here.
Dont be put off by the size of the article though, its a pretty simple thing.
Re: TCP/IP - Host can't display received messages :(
I've added the code and changed it to add the item into the listbox but it doesn't work.
Am I putting the code in the message received module and calling it there or in my host form and calling it from the module?
Code:
Private Delegate Sub AddItemInvoker(ByVal text As String)
Private Sub AddItem(ByVal text As String)
If Host.lstMessages.InvokeRequired Then
Host.lstMessages.Invoke(New AddItemInvoker(AddressOf AddItem), New Object() {text})
Else
Host.lstMessages.Items.Add(text)
End If
End Sub
Re: TCP/IP - Host can't display received messages :(
I dont know what 'module' you're talking about but you'd be best off putting it in your form.
Re: TCP/IP - Host can't display received messages :(
So how do I call it from a module because it's a private sub and when I make it public it doesn't change anything, it still doesn't add the item to the listbox?
knxrb
Re: TCP/IP - Host can't display received messages :(
How does your module look? Could it be that you're calling the method on the wrong instance of the form?
Re: TCP/IP - Host can't display received messages :(
The module and the rest of the project files are in the attachment from the first post.
Quote:
Could it be that you're calling the method on the wrong instance of the form?
I'm not sure :(
knxrb
Re: TCP/IP - Host can't display received messages :(
I'm not sure why you're calling MR.Message(Me, message) in the ConnectedClient class, as it already provides a DataReceived event that you can use, which will only be raised when a full message has been received. Just add an eventhandler for the DataReceived event after you've created the ConnectedClient in your form. That will mean that you basically will have to move your "Message" subroutine into your form code, which will also mean that you will be able to call the AddItem method correctly.
Re: TCP/IP - Host can't display received messages :(
Oh right, I'll try that now, thanks :D
knxrb
1 Attachment(s)
Re: TCP/IP - Host can't display received messages :(
I can't get it to work :(
I've attached the project as it is now and I've got it calling the event in the form etc..
knxrb
Re: TCP/IP - Host can't display received messages :(
The way the ConnectedClient class works is that it classifies a "complete" message as a string ending with ControlChars.Cr. Are you taking that into mind when you send data? Each message must end with ControlChars.Cr.
I know thats a "bad" way of doing it, I'm going to upload a new ConnectedClient class to my example here on VBForums shortly (I've been saying 'shortly' for quite a while now though...)
Re: TCP/IP - Host can't display received messages :(
I didn't know it needed to end with that.
Is that why it doesn't display it?
[Edit] Adding ControlChars.Cr to the end of the messages doesn't make it display :(
knxrb
Re: TCP/IP - Host can't display received messages :(
Are you even receiving the messages as the code is right now? Can you step through the code and verify that the dataReceived event is indeed being raised?
Re: TCP/IP - Host can't display received messages :(
It's working! I've previously removed a line of code from ConnectedClient.vb class and forgot to put it back :blush:
Thanks for your time and help :D
knxrb