I have the following code which creates a nice little instant messenger for my program. I am running into trouble when I try to send a message to a user that is not logged into their chat window. It waits about 20 seconds, then throws the error message. In the meantime, the entire program is hung up while it is waiting for the response to go through.

Here is the code I have:

Private Sub SendMessageButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SendMessageButton.Click
            If UserNameComboBox.Text = "" Or IPComboBox.Text = "" Or MessageToSendTextBox.Text = "" Then
                MsgBox("Enter a User Name, enter an IP Address , and enter a message.", MsgBoxStyle.Exclamation)
                If stringlocalIP = "" Then
                    MsgBox("Please enter an IP Address for user you are sending to" & vbCrLf & "(in the Settings tab).", MsgBoxStyle.Exclamation)
                    client = New TcpClient(stringlocalIP, 44444)

                    Dim writer As New StreamWriter(client.GetStream())
                    writer.Write(UserNameComboBox.Text + " says: " + MessageToSendTextBox.Text)

                    lastMessage = MessageToSendTextBox.Text
                    MessageToSendTextBox.Text = ""
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            If listener.Pending = True Then
                message = ""
                client = listener.AcceptTcpClient

                Dim reader As New StreamReader(client.GetStream())
                While reader.Peek > -1
                    message = message + Convert.ToChar(reader.Read()).ToString
                End While
                ReceiveMessagesTextBox.Text = (ReceiveMessagesTextBox.Text + message + vbCrLf)
            End If
        Catch ex As Exception
        End Try
    End Sub

I believe I need to incorporate the TcpClient.ReceiveTimeout and/or TcpClient.SendTimeout but I am not sure where to place those in my code. I have tried all sorts of things and I am getting a little stuck, so I do appreciate any comments or suggestions.


5 Years
Discussion Span
Last Post by mikeybware

You should put the whole connect/sending process into a backgroundworker to keep the UI responsible.
Also you should add a try/Catch block around catching the SocketException, which occurs if the client is not online or IP address not reachable.


If I put the connect/sending process in a backgroundworker, then I can use the rest of my program which is great. I even Try/Catched a SocketException like you suggested, which does grab the exception no problem, but still after 20 seconds of the messenger being hung-up. I am looking to minimize this delay to even 5 seconds of waiting for a response.

Is there any way of quickly checking the internal IP that the messenger is sending to ... then I can see if the user is available or not before I even send the message across?

Appreciate you help and suggestions though ... Thank you:)


Yes, I have tried client.SendTimeout as well as client.ReceiveTimeout, unless I am not putting them in the right place. I have tried putting them in different places throughout my code, and still no luck.


Hmmm, unfortunately I am not able to figure this out quite yet. I am fairly new at VB programming (and self taught). Perhaps other suggestions or nudges may help. Thank you.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.