Results 1 to 38 of 38

Thread: [RESOLVED] [2005] TCPListener, Threading, Error.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Resolved [RESOLVED] [2005] TCPListener, Threading, Error.

    I have been grabbing code from all over, including this post.

    http://www.vbforums.com/showthread.php?t=451368

    I used a working console code for a client from http://www.eggheadcafe.com/articles/20020323.asp

    But my code below is not accepting the connection.
    The code from eggheadcafe works with the server code from eggheadcafe. So I know I did something wrong in my code for my windows application.

    The complete error message is

    Code:
    System.Net.Sockets.SocketException was unhandled
      ErrorCode=10061
      Message="No connection could be made because the target machine actively refused it"
      Source="System"
      StackTrace:
           at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
           at TCP___CLient.TCPCli.Main() in C:\Documents and Settings\Owner\Local Settings\Application Data\Temporary Projects\TCP - CLient\Module1.vb:line 7
           at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
    vb Code:
    1. Public Class Form1
    2.     Dim listeners(9) As Net.Sockets.TcpListener
    3.     Dim thrListen As New Threading.Thread(AddressOf DoListen)
    4.  
    5.     Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    6.  
    7.         For i As Integer = 0 To 9
    8.             listeners(i).Stop()
    9.             listeners(i) = Nothing
    10.         Next
    11.         thrListen.Abort()
    12.  
    13.     End Sub
    14.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    15.         Dim basePort As Integer = 44000
    16.         For i As Integer = 0 To 9
    17.             listeners(i) = New Net.Sockets.TcpListener(Net.IPAddress.Any, basePort + i)
    18.         Next
    19.  
    20.         thrListen.Start()
    21.  
    22.     End Sub
    23.     Private Sub DoListen()
    24.         Dim client As Net.Sockets.TcpClient
    25.         Dim sr As IO.StreamReader
    26.         Do
    27.             For i As Integer = 0 To 9
    28.                 Try
    29.                     client = listeners(i).AcceptTcpClient
    30.                     MsgBox("A")
    31.                     sr = New IO.StreamReader(client.GetStream)
    32.                     MessageBox.Show("TcpListener " & i.ToString & " recieved the following:" & Environment.NewLine & sr.ReadToEnd)
    33.  
    34.                 Catch
    35.  
    36.                 End Try
    37.             Next
    38.         Loop
    39.     End Sub
    40.  
    41. End Class
    Last edited by rack; Jul 17th, 2007 at 10:56 PM.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: [2005] LCPListener Threading, Error.

    Just wondering if anyone had any idea's? Still new to vb2005, I migrated from VB6 recently.

    [edit]

    I found part of the problem.
    the Try Catch was skipping over an error, the error said you must start the event first.
    I added
    listeners(i).start()

    the client connected, but the data was never accepted/sent. I'll play with it some more.
    Last edited by rack; Jul 10th, 2007 at 10:00 PM.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

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

    Re: [2005] LCPListener Threading, Error.

    I'll look into your problem tomorrow. Sleeping time now
    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. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: [2005] LCPListener Threading, Error.

    I think I may have figured out more of the puzzle. It looks like the Client application is sending an array of bytes, and the server applicaiton is trying to receive a string. I don't know if this has anything to do with it or not.

    [edit]

    here is the client app part from the eggheadcafe website that is sending the data.

    vb Code:
    1. Dim tcpClient As New System.Net.Sockets.TcpClient()
    2.         tcpClient.Connect("192.168.1.100", 45000)
    3.         Dim networkStream As NetworkStream = tcpClient.GetStream()
    4.             ' Do a simple write.
    5.             Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there")
    6.             networkStream.Write(sendBytes, 0, sendBytes.Length)
    Last edited by rack; Jul 12th, 2007 at 03:01 AM.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: [2005] LCPListener Threading, Error.

    Ok, I wrote a client and server (Sender and receiver) I am still not getting the data from one app to the other correctly. I get 97, which does correspond to the ascii code value for lower case 'a' but, I have to close the stream, then close the connection before it will work. Is this how I am suppose to be sending information? Request connection, send data, close stream, close connection... For every single message? Even if I want someone to be connected, and be sending data back and forth?

    This is my Server.
    vb Code:
    1. Imports System.Net.Sockets
    2. Imports System.Text
    3.  
    4. Public Class Form1
    5.     Dim listeners(9) As Net.Sockets.TcpListener
    6.  
    7.     Dim thrListen As New Threading.Thread(AddressOf DoListen)
    8.  
    9.     Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    10.  
    11.         For i As Integer = 0 To 9
    12.             listeners(i).Stop()
    13.             listeners(i) = Nothing
    14.         Next
    15.         thrListen.Abort()
    16.  
    17.     End Sub
    18.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    19.         Dim basePort As Integer = 44600
    20.         For i As Integer = 0 To 9
    21.             listeners(i) = New Net.Sockets.TcpListener(Net.IPAddress.Any, basePort + i)
    22.         Next
    23.  
    24.         thrListen.Start()
    25.  
    26.     End Sub
    27.     Private Sub DoListen()
    28.         Dim client As Net.Sockets.TcpClient
    29.         Dim TempString As String
    30.  
    31.         TempString = ""
    32.         Dim sr As IO.StreamReader
    33.         Do
    34.             For i As Integer = 0 To 9
    35.  
    36.                 Try
    37.  
    38.                     listeners(i).Start()
    39.  
    40.                     client = listeners(i).AcceptTcpClient
    41.                     sr = New IO.StreamReader(client.GetStream)
    42.  
    43.                     TempString = sr.Read
    44.  
    45.                     MsgBox("TcpListener " & i & " recieved the following:" & vbNewLine & TempString)
    46.  
    47.                 Catch
    48.                     MsgBox(Err.Description)
    49.                 End Try
    50.             Next
    51.         Loop
    52.     End Sub
    53. End Class
    This is my Client.

    vb Code:
    1. Imports System.Net.Sockets
    2. Imports System.Text
    3. Public Class Form1
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         Dim client As Net.Sockets.TcpClient
    7.         Dim sw As IO.StreamWriter
    8.         client = New Net.Sockets.TcpClient
    9.  
    10.         Try
    11.             client.Connect("192.168.1.100", 44600)
    12.             sw = New IO.StreamWriter(client.GetStream)
    13.             sw.Write("a")
    14.             sw.Close()
    15.             client.Close()
    16.         Catch
    17.             MsgBox(Err.Description)
    18.         End Try
    19.     End Sub
    20. End Class
    Last edited by rack; Jul 12th, 2007 at 03:05 AM.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  6. #6
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    Re: [2005] LCPListener Threading, Error.

    I just wrote a udp listener the other day, and to avoid this problem I used a background worker, and inside that thread was an infinite loop which done the listening, if I received some bytes, I would pass them in the report progress event, and handled decoding the bytes there, I just always sent 0 for progress and Bytes() as the userstate object param. It worked perfectly, and is quite simple with no need to worry about delegates and invoking other controls...

    I will upload it tomorrow if you want, all it does is send bytes to local host, and receives them in the BGworker, the host and listener are in the same class as to make it more readable, and it uses a library called TinyUDP for the host...

    Signatures suck

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: [2005] LCPListener Threading, Error.

    Anything will help, dunno how different the coding is for UDP and TCP. Also I just noticed I had some werid bit of dislexia when I wrote the title to this, I have now changed it to TCP instead of LCP, haha.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: [2005] TCPListener, Threading, Error. (not sending until close of stream)

    Should I be using
    vb Code:
    1. Dim client As Net.Sockets.TcpClient
    2. client = listeners(i).AcceptTcpClient
    3.  
    4. Dim NWS As System.Net.Sockets.NetworkStream
    5. NWS=Listeners(i).GetStream
    instead of
    vb Code:
    1. Dim client As Net.Sockets.TcpClient
    2. client = listeners(i).AcceptTcpClient
    3.  
    4. Dim sw As IO.StreamWriter
    5. sw = New IO.StreamWriter(client.GetStream)
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: [2005] TCPListener, Threading, Error. (not sending until close of stream)

    Ok, I got the listener to work, but I have a few final questions. If anyone knows please could you answer. I haven't really gotten many responces to this thread.

    For some reason when I send data into the datastream, if I use the .write command on the networkstream, 3 different times, my listener will read it all in one time, instead of reading it three different times as three different messeges. Do I have to call a read after a Write?

    I don't think it matters which way I went about getting information from the stream, the key was to read and write both in bytes.

    Below is the code from the Server (tcpListener)

    The following are the text boxes, and there purposes.
    TxtChat - This is the chat window/box.
    TxtPort - This is the TCP/IP port address to listen on.
    TxtUserList - This is the textbox that shows which users have logged in.
    TxtMessege - This is where the user will type the messege to send to the others.

    The following are the two buttons, and there purposes.
    BtnSend - Button that the user clicks to send a messege contained in the txtmessege textbox to the other user.
    BtnListen - This button starts the listening (server) interface.


    vb Code:
    1. Imports System.Net.Sockets
    2. Imports System.Text
    3. Public Class Form1
    4.     Dim thrListen As New Threading.Thread(AddressOf DoListen)
    5.     Dim MyDataStream As System.Net.Sockets.NetworkStream
    6.     Dim Client As Net.Sockets.TcpClient
    7.     Dim tstServer As Net.Sockets.TcpListener
    8.  
    9.     ' This delegate enables asynchronous calls for setting
    10.     ' the text property on a TextBox control.
    11.     Delegate Sub SetTextCallback(ByVal [text] As String)
    12.  
    13.     Private Sub Form1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Leave
    14.         MyDataStream.Close()
    15.         tstServer.Stop()
    16.         thrListen.Abort()
    17.     End Sub
    18.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    19.  
    20.     End Sub
    21.  
    22.     Private Sub BtnListen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnListen.Click
    23.         tstServer = New Net.Sockets.TcpListener(Net.IPAddress.Any, Int(Val(TxtPort.Text)))
    24.         thrListen.IsBackground = True
    25.         thrListen.Start()
    26.     End Sub
    27.     Private Sub DoListen()
    28.         Dim TempString As String
    29.         TempString = ""
    30.         Do
    31.             Try
    32.                 'start the listening
    33.                 tstServer.Start()
    34.                 'Accept the pending client connection and return            
    35.                 'a TcpClient initialized for communication.
    36.                 Client = tstServer.AcceptTcpClient
    37.                 'client.
    38.                 Me.SetText("Connection Accepted:" & vbNewLine)
    39.                 ' Get the stream
    40.                 MyDataStream = Client.GetStream
    41.                 ' Read the stream into a byte array
    42.                 Dim bytes(Client.ReceiveBufferSize) As Byte
    43.                 MyDataStream.Read(bytes, 0, CInt(Client.ReceiveBufferSize))
    44.                 ' Return the data received from the client to the txtchat textbox.
    45.                 TempString = Encoding.ASCII.GetString(bytes)
    46.                 Me.SetText(TempString)
    47.                 Me.SetText(vbNewLine)
    48.             Catch
    49.                 MsgBox(Err.Description)
    50.             End Try
    51.         Loop
    52.     End Sub
    53.  
    54.  
    55.     ' This method demonstrates a pattern for making thread-safe
    56.     ' calls on a Windows Forms control.
    57.     '
    58.     ' If the calling thread is different from the thread that
    59.     ' created the TextBox control, this method creates a
    60.     ' SetTextCallback and calls itself asynchronously using the
    61.     ' Invoke method.
    62.     '
    63.     ' If the calling thread is the same as the thread that created
    64.     ' the TextBox control, the Text property is set directly.
    65.  
    66.     Private Sub SetText(ByVal [text] As String)
    67.  
    68.         ' InvokeRequired required compares the thread ID of the
    69.         ' calling thread to the thread ID of the creating thread.
    70.         ' If these threads are different, it returns true.
    71.         If Me.TxtChat.InvokeRequired Then
    72.             Dim d As New SetTextCallback(AddressOf SetText)
    73.             Me.Invoke(d, New Object() {[text]})
    74.         Else
    75.             'Me.TxtChat.Text = Me.TxtChat.Text & [text]
    76.             CheckMessege([text])
    77.         End If
    78.     End Sub
    79.  
    80.     Private Sub BtnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSend.Click
    81.         If Client.Connected = True Then
    82.             If MyDataStream.CanWrite Then
    83.                 Try
    84.                     ' Do a simple write.
    85.                     Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(TxtMessege.Text)
    86.                     MyDataStream.Write(sendBytes, 0, sendBytes.Length)
    87.                     'add the text to the local text box window
    88.                     TxtChat.Text = TxtChat.Text & TxtMessege.Text
    89.                     'clear the user messege that was typed
    90.                     TxtMessege.Text = ""
    91.                 Catch
    92.                     MsgBox(Err.Description)
    93.                 End Try
    94.             End If
    95.         Else
    96.             TxtChat.Text = TxtChat.Text & "No Client is connected." & vbNewLine
    97.         End If
    98.     End Sub
    99.     Private Sub CheckMessege(ByVal [text] As String)
    100.         MsgBox([text])
    101.         Dim TempMessege() As String
    102.         TempMessege = Split([text], "|-|")
    103.         Select Case TempMessege(0)
    104.             Case "Messege:"
    105.                 TxtChat.Text = TxtChat.Text & TempMessege(2) & ": " & TempMessege(1) & vbNewLine
    106.             Case "NewUser:"
    107.                 TxtUserList.Text = TxtUserList.Text & TempMessege(2) & vbNewLine
    108.                 TxtChat.Text = TxtChat.Text & "User: " & TempMessege(2) & " Connected"
    109.             Case "Disconnect:"
    110.                 TxtUserList.Text.Replace(TempMessege(2) & vbNewLine, "")
    111.                 TxtChat.Text = TxtChat.Text & "User: " & TempMessege(2) & " Disconnected"
    112.             Case "Connect:"
    113.  
    114.             Case Else
    115.  
    116.         End Select
    117.     End Sub
    118. End Class
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  10. #10
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [2005] TCPListener, Threading, Error. (not sending until close of stream)

    Sorry, no help from my side, because I seem to have a related problem. In your last post you stated that you send 3 messages and they get recieved as one, are you sending from a client and when is the message been displayed (is the connection still open).
    My listener will not display the messages sent by a client until the client closes the connection????
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  11. #11
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [2005] TCPListener, Threading, Error. (not sending until close of stream)

    Do you do those 3 .write commands using 3 separate button-clicks or are you doing it by code (which would be faster). In the last case it sounds like the "normal" operation I 've seen using VB6 Winsock for a TCP connection. I had to parse each message since I had multiple occasions when several messages where received a one message. At least you got the messages!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: [2005] TCPListener, Threading, Error. (not sending until close of stream)

    |-| is what I use to "split" the data". I figured just one character might be too easy for someone to accidently type.

    I send three .write commands by code. In the code below, I tried doing a .Write, then .Read, then .Write, then .Read.

    The messege from the CLient "NewUser:|-||-|Jeremy" is sent, and received.
    The messege from the Server "RCVD" is sent, and received by the client.
    The remaining messeges from the Client are never received by the server. It's as if the program just skips over them.

    Posting the Client that sends the data. And the updated Listener (server)

    CLIENT
    vb Code:
    1. Imports System.Net.Sockets
    2. Imports System.Text
    3. Class TCPCli
    4.     Shared Sub Main()
    5.  
    6.         Dim tcpClient As New System.Net.Sockets.TcpClient()
    7.         tcpClient.Connect("192.168.1.100", 45000)
    8.         Dim networkStream As NetworkStream = tcpClient.GetStream()
    9.         If networkStream.CanWrite And networkStream.CanRead Then
    10.             ' Do a simple write.
    11.             Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("NewUser:|-||-|Jeremy")
    12.             networkStream.Write(sendBytes, 0, sendBytes.Length)
    13.  
    14.             Dim bytes2(tcpClient.ReceiveBufferSize) As Byte
    15.             networkStream.Read(bytes2, 0, CInt(tcpClient.ReceiveBufferSize))
    16.             Dim returndata2 As String = Encoding.ASCII.GetString(bytes2)
    17.             Console.WriteLine(("Data was:" + returndata2))
    18.  
    19.             Dim sendBytes2 As [Byte]() = Encoding.ASCII.GetBytes("Messege:|-|Whats going on?|-|BoB")
    20.             networkStream.Write(sendBytes2, 0, sendBytes2.Length)
    21.  
    22.  
    23.             ' Read the NetworkStream into a byte buffer.
    24.             Dim bytes(tcpClient.ReceiveBufferSize) As Byte
    25.             networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
    26.             ' Output the data received from the host to the console.
    27.             Dim returndata As String = Encoding.ASCII.GetString(bytes)
    28.             Console.WriteLine(("Host returned: " + returndata))
    29.             Dim sendBytes3 As [Byte]() = Encoding.ASCII.GetBytes("Disconnect:|-||-|Timmay")
    30.             networkStream.Write(sendBytes3, 0, sendBytes3.Length)
    31.             Dim bytes3(tcpClient.ReceiveBufferSize) As Byte
    32.             networkStream.Read(bytes3, 0, CInt(tcpClient.ReceiveBufferSize))
    33.  
    34.         Else
    35.             If Not networkStream.CanRead Then
    36.                 Console.WriteLine("cannot not write data to this stream")
    37.                 tcpClient.Close()
    38.             Else
    39.                 If Not networkStream.CanWrite Then
    40.                     Console.WriteLine("cannot read data from this stream")
    41.                     tcpClient.Close()
    42.                 End If
    43.             End If
    44.         End If
    45.         ' pause so user can view the console output
    46.         Console.ReadLine()
    47.     End Sub
    48. End Class


    ---SERVER-------------------------------------
    vb Code:
    1. Imports System.Net.Sockets
    2. Imports System.Text
    3. Public Class Form1
    4.     Dim thrListen As New Threading.Thread(AddressOf DoListen)
    5.     Dim MyDataStream As System.Net.Sockets.NetworkStream
    6.     Dim Client As Net.Sockets.TcpClient
    7.     Dim tstServer As Net.Sockets.TcpListener
    8.  
    9.     ' This delegate enables asynchronous calls for setting
    10.     ' the text property on a TextBox control.
    11.     Delegate Sub SetTextCallback(ByVal [text] As String)
    12.  
    13.     Private Sub Form1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Leave
    14.         MyDataStream.Close()
    15.         tstServer.Stop()
    16.         thrListen.Abort()
    17.     End Sub
    18.  
    19.     Private Sub BtnListen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnListen.Click
    20.         tstServer = New Net.Sockets.TcpListener(Net.IPAddress.Any, Int(Val(TxtPort.Text)))
    21.         thrListen.IsBackground = True
    22.         thrListen.Start()
    23.     End Sub
    24.     Private Sub DoListen()
    25.         Dim TempString As String
    26.         TempString = ""
    27.         Do
    28.             Try
    29.                 'start the listening
    30.                 tstServer.Start()
    31.                 'Accept the pending client connection and return            
    32.                 'a TcpClient initialized for communication.
    33.                 Client = tstServer.AcceptTcpClient
    34.                 ' Get the stream
    35.                 MyDataStream = Client.GetStream
    36.                 ' Read the stream into a byte array
    37.                 Dim bytes(Client.ReceiveBufferSize) As Byte
    38.                 MyDataStream.Read(bytes, 0, CInt(Client.ReceiveBufferSize))
    39.                 ' Return the data received from the client to the txtchat textbox.
    40.                 TempString = Encoding.ASCII.GetString(bytes)
    41.                 Me.SetText(TempString)
    42.             Catch
    43.                 MsgBox(Err.Description)
    44.             End Try
    45.         Loop
    46.     End Sub
    47.  
    48.  
    49.     ' This method demonstrates a pattern for making thread-safe
    50.     ' calls on a Windows Forms control.
    51.     '
    52.     ' If the calling thread is different from the thread that
    53.     ' created the TextBox control, this method creates a
    54.     ' SetTextCallback and calls itself asynchronously using the
    55.     ' Invoke method.
    56.     '
    57.     ' If the calling thread is the same as the thread that created
    58.     ' the TextBox control, the Text property is set directly.
    59.  
    60.     Private Sub SetText(ByVal [text] As String)
    61.  
    62.         ' InvokeRequired required compares the thread ID of the
    63.         ' calling thread to the thread ID of the creating thread.
    64.         ' If these threads are different, it returns true.
    65.         If Me.TxtChat.InvokeRequired Then
    66.             Dim d As New SetTextCallback(AddressOf SetText)
    67.             Me.Invoke(d, New Object() {[text]})
    68.         Else
    69.             'Me.TxtChat.Text = Me.TxtChat.Text & [text]
    70.             CheckMessege([text])
    71.         End If
    72.     End Sub
    73.  
    74.     Private Sub BtnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSend.Click
    75.         If Client.Connected = True Then
    76.             If MyDataStream.CanWrite Then
    77.                 Try
    78.                     ' Do a simple write.
    79.                     Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(TxtMessege.Text)
    80.                     MyDataStream.Write(sendBytes, 0, sendBytes.Length)
    81.                     'add the text to the local text box window
    82.                     TxtChat.Text = TxtChat.Text & TxtMessege.Text
    83.                     'clear the user messege that was typed
    84.                     TxtMessege.Text = ""
    85.                 Catch
    86.                     MsgBox(Err.Description)
    87.                 End Try
    88.             End If
    89.         Else
    90.             TxtChat.Text = TxtChat.Text & "No Client is connected." & vbNewLine
    91.         End If
    92.     End Sub
    93.     Private Sub CheckMessege(ByVal [text] As String)
    94.         'MsgBox([text])
    95.         Dim TempMessege() As String
    96.         TempMessege = Split([text], "|-|")
    97.         Select Case TempMessege(0)
    98.             Case "Messege:"
    99.                 TxtChat.Text = TxtChat.Text & TempMessege(2) & ": " & TempMessege(1) & vbNewLine
    100.                 SendNotice("RCVD") 'Call to Send Notice sub
    101.             Case "NewUser:"
    102.                 TxtUserList.Text = TxtUserList.Text & TempMessege(2) & vbNewLine
    103.                 TxtChat.Text = TxtChat.Text & "User: " & TempMessege(2) & " Connected"
    104.                 SendNotice("RCVD") 'Call to Send Notice sub
    105.             Case "Disconnect:"
    106.                 TxtUserList.Text.Replace(TempMessege(2) & vbNewLine, "")
    107.                 TxtChat.Text = TxtChat.Text & "User: " & TempMessege(2) & " Disconnected"
    108.                 SendNotice("RCVD") 'Call to Send Notice sub
    109.             Case "Connect:"
    110.                 MsgBox("Connected")
    111.             Case Else
    112.                 MsgBox("***!!!" & vbNewLine & "---" & [text] & "---")
    113.                 SendNotice("ERR") 'Call to Send Notice sub
    114.         End Select
    115.     End Sub
    116.     'SendNotice(STRING)
    117.     '
    118.     'Value being sent is String
    119.     '
    120.     'Takes a string, converts it to a byte array
    121.     'Then sends the data on the DataStream.
    122.     Private Sub SendNotice(ByVal MyNotice As String)
    123.         Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(MyNotice)
    124.         MyDataStream.Write(sendBytes, 0, sendBytes.Length)
    125.     End Sub
    126. End Class
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: [2005] TCPListener, Threading, Error. (not sending until close of stream)

    I just found this out about tcpclient.nodelay, but that didn't solve the problem of the missing packets/data.

    http://msdn2.microsoft.com/en-us/lib...ay(vs.80).aspx

    I just don't understand why data is not being sent.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  14. #14
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [2005] TCPListener, Threading, Error. (not sending until close of stream)

    Thanks, I'll give that a try on my side.
    Since I'm in office now, you have to wait for results!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  15. #15
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [2005] TCPListener, Threading, Error. (not sending until close of stream)

    One qustion for you, the messages that don't get to your server, are they shown if the client loggs off?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  16. #16
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [2005] TCPListener, Threading, Error. (not sending until close of stream)

    It doesn't work with NoDelay=True for me either!
    I'm just trying to Send a message from Client, Disconnect (now the Server gets the message) and reconnect. Doing that via the controlbuttons (by hand) works fine, however if I change the Send routine and put connection_Close and Connect in it it doesn't do it. Maybe I have to wait for sencomplete (if I get something like that). This would be on the client side!

    [Edit] Using the Sendcomplete for disconnecting and connecting does the job, now I have to make that into a VB2005 Clinet instead of a VB6 one!
    Last edited by opus; Jul 17th, 2007 at 01:26 PM.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  17. #17
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [2005] TCPListener, Threading, Error. (not sending until close of stream)

    rack, I see one difference in the TCPListener from your code compared to mine, you start the listener in your DoListen Sub, I do that in the Btton_Click routine where the thrListen is also started.Although the outcome is the same (messages from the client don'T come thru) your code is forcing my cpu to be at 100% all the time, while is only doing that until the client connected!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  18. #18
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [2005] TCPListener, Threading, Error. (not sending until close of stream)

    Hi, rack, I'm still doing further research.
    It looks to me as if the DoListen Sub is stopped when the Client gets accepted "Client = tstServer.AcceptTcpClient". The next time this routine is jumped to is when the client closes the connection? The problem is I don't understand why!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  19. #19
    Hyperactive Member cptHotkeys's Avatar
    Join Date
    Apr 2007
    Location
    New Zealand
    Posts
    294

    Re: [2005] TCPListener, Threading, Error. (not sending until close of stream)

    Here is that UDP example I posted about before...

    Its UDP not TCP even thaugh the zip says TCP, but who cares, it sends and receives bytes and encodes them to ascii...
    This example has 4 sub routines, form load, background worker do work, report progress, and a button click, all are quite simple to understand, I have only tested it sending to 127.0.0.1 and have not encountered 1 problem...
    Attached Files Attached Files

    Signatures suck

  20. #20

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: [2005] TCPListener, Threading, Error. (not sending until close of stream)

    Yeah I notice that for whatever reason, the server will receive the first messege, send a responce, and then, for whatever reason in my code, never again tries to get the data from the stream. I'm so close to the answer I can taste it, if someone knows what stupid coding mistake i'm making, please tell me.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  21. #21

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: [2005] TCPListener, Threading, Error.

    I'm getting responces back and forth now, but the split I call on the messege leaves the length of the last item at 8156, which is odd.

    How can I correctly split/receive the data into an array, so that the last item doesn't get the remaining blankness of the buffer.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  22. #22

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: [2005] TCPListener, Threading, Error.

    Alright!!!!!!!!!!!!!!

    Here is my working TCP Server(tcplistener), and the test User(tcpclient)

    The server only supports one user so far. I'm going to create a class/structure that will hold the information for 4 users to connect.

    The objects on the form are the same from the earlyer posts.
    If anyone can inform me of a better way for TCP connection (not UDP) let me know, this is all i've found, reading, testing, trying random changes to code, etc.

    Anyways, hers the server.
    vb Code:
    1. Imports System.Net.Sockets
    2. Imports System.Text
    3. Public Class Form1
    4.     Dim thrListen As New Threading.Thread(AddressOf DoListen)
    5.     Dim MyDataStream As System.Net.Sockets.NetworkStream
    6.     Dim Client As Net.Sockets.TcpClient
    7.     Dim tstServer As Net.Sockets.TcpListener
    8.     Dim ClientStatus As Integer
    9.     ' This delegate enables asynchronous calls for setting
    10.     ' the text property on a TextBox control.
    11.     Delegate Sub SetTextCallback(ByVal [text] As String)
    12.  
    13.     Private Sub Form1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Leave
    14.         MyDataStream.Close()
    15.         tstServer.Stop()
    16.         thrListen.Abort()
    17.     End Sub
    18.  
    19.     Private Sub BtnListen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnListen.Click
    20.         tstServer = New Net.Sockets.TcpListener(Net.IPAddress.Any, Int(Val(TxtPort.Text)))
    21.         'start the listening
    22.         tstServer.Start()
    23.         'Make sure the Thread closes if the application does
    24.         thrListen.IsBackground = True
    25.         'start the Listen Thread (DoListen Sub)
    26.         thrListen.Start()
    27.     End Sub
    28.     Private Sub DoListen()
    29.         Dim TempString As String
    30.         TempString = ""
    31.         Do
    32.  
    33.             Try
    34.                 If ClientStatus = 0 Then
    35.                     'Accept the pending client connection and return            
    36.                     'a TcpClient initialized for communication.
    37.                     Client = tstServer.AcceptTcpClient
    38.                     ClientStatus = 1
    39.                 End If
    40.                 If ClientStatus = 1 Then
    41.                     ' Get the stream
    42.                     MyDataStream = Client.GetStream
    43.                     ClientStatus = 2
    44.                 End If
    45.                 If ClientStatus = 2 Then
    46.                     ' Read the stream into a byte array
    47.                     Dim bytes(Client.ReceiveBufferSize) As Byte
    48.                     MyDataStream.Read(bytes, 0, CInt(Client.ReceiveBufferSize))
    49.                     ' Return the data received from the client to the txtchat textbox.
    50.                     TempString = Encoding.ASCII.GetString(bytes)
    51.                     Me.SetText(TempString)
    52.                 End If
    53.             Catch ex As Exception
    54.                 MsgBox(ex.Message)
    55.             End Try
    56.         Loop
    57.     End Sub
    58.  
    59.  
    60.  
    61.     ' This method demonstrates a pattern for making thread-safe
    62.     ' calls on a Windows Forms control.
    63.     '
    64.     ' If the calling thread is different from the thread that
    65.     ' created the TextBox control, this method creates a
    66.     ' SetTextCallback and calls itself asynchronously using the
    67.     ' Invoke method.
    68.     '
    69.     ' If the calling thread is the same as the thread that created
    70.     ' the TextBox control, the Text property is set directly.
    71.  
    72.     Private Sub SetText(ByVal [text] As String)
    73.  
    74.         ' InvokeRequired required compares the thread ID of the
    75.         ' calling thread to the thread ID of the creating thread.
    76.         ' If these threads are different, it returns true.
    77.         If Me.TxtChat.InvokeRequired Then
    78.             Dim d As New SetTextCallback(AddressOf SetText)
    79.             Me.Invoke(d, New Object() {[text]})
    80.         Else
    81.             'Me.TxtChat.Text = Me.TxtChat.Text & [text]
    82.             CheckMessege([text])
    83.         End If
    84.     End Sub
    85.  
    86.     Private Sub BtnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSend.Click
    87.         If Client.Connected = True Then
    88.             If MyDataStream.CanWrite Then
    89.                 Try
    90.                     ' Do a simple write.
    91.                     Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(TxtMessege.Text)
    92.                     MyDataStream.Write(sendBytes, 0, sendBytes.Length)
    93.                     'add the text to the local text box window
    94.                     TxtChat.Text = TxtChat.Text & TxtMessege.Text
    95.                     'clear the user messege that was typed
    96.                     TxtMessege.Text = ""
    97.                 Catch
    98.                     MsgBox(Err.Description)
    99.                 End Try
    100.             End If
    101.         Else
    102.             TxtChat.Text = TxtChat.Text & "No Client is connected." & vbNewLine
    103.         End If
    104.     End Sub
    105.     Private Sub CheckMessege(ByVal [text] As String)
    106.         Dim TempMessege() As String
    107.         TempMessege = Split([text], "|-|")
    108.         If UBound(TempMessege) < 2 Then
    109.             'this means the information provided with the messege did not contain everything needed.
    110.             SendNotice("<-REPEAT->") 'Call to Send Notice sub
    111.             Exit Sub
    112.         Else
    113.             'the only way I could figure out how to find the end of the messege.
    114.             'this simply removes anything after the special character sequest |=|
    115.             TempMessege(2) = Mid(TempMessege(2), 1, (InStr(TempMessege(2), "|=|") - 1))
    116.         End If
    117.         Select Case TempMessege(0)
    118.             Case "Messege:"
    119.                 'this displays the username: Messege as such.
    120.                 TxtChat.Text = TxtChat.Text & TempMessege(2) & ": " & TempMessege(1) & vbNewLine
    121.                 SendNotice("<-RCVD->") 'Call to Send Notice sub
    122.             Case "NewUser:"
    123.                 'this adds the username to the list of connected users.
    124.                 TxtUserList.Text = TxtUserList.Text & TempMessege(2) & vbNewLine
    125.                 'this displays User: Username -Connected-
    126.                 TxtChat.Text = TxtChat.Text & "User: " & TempMessege(2) & " -Connected-" & vbNewLine
    127.                 SendNotice("<-RCVD->") 'Call to Send Notice sub
    128.             Case "Disconnect:"
    129.                 'this next line removes the user and the chr(13) or VBNEWLINE
    130.                 TxtUserList.Text = Replace(TxtUserList.Text, TempMessege(2) & vbNewLine, "")
    131.                 'This Notifies anyone in chat of the user who disconnect.
    132.                 TxtChat.Text = TxtChat.Text & "User: " & TempMessege(2) & " D-isconnected-" & vbNewLine
    133.                 SendNotice("<-RCVD->") 'Call to Send Notice sub
    134.                 'close the networkstream with this user.
    135.                 MyDataStream.Close()
    136.                 'close the connection with this user.
    137.                 Client.Close()
    138.                 'reset the variable that allows my program to work.
    139.                 ClientStatus = 0
    140.             Case "Connect:"
    141.                 MsgBox("Connected")
    142.             Case Else
    143.                 MsgBox("***!!!" & vbNewLine & "---" & [text] & "---")
    144.                 SendNotice("<-ERR->") 'Call to Send Notice sub
    145.         End Select
    146.     End Sub
    147.     'SendNotice(STRING)
    148.     '
    149.     'Value being sent is String
    150.     '
    151.     'Takes a string, converts it to a byte array
    152.     'Then sends the data on the DataStream.
    153.     Private Sub SendNotice(ByVal MyNotice As String)
    154.         Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(MyNotice)
    155.         MyDataStream.Write(sendBytes, 0, sendBytes.Length)
    156.     End Sub
    157. End Class

    Here is the client.
    vb Code:
    1. Imports System.Net.Sockets
    2. Imports System.Text
    3. Class TCPCli
    4.     Shared Sub Main()
    5.  
    6.         Dim tcpClient As New System.Net.Sockets.TcpClient()
    7.         tcpClient.Connect("192.168.1.100", 45000)
    8.         tcpClient.NoDelay = True
    9.         Dim networkStream As NetworkStream = tcpClient.GetStream()
    10.         If networkStream.CanWrite And networkStream.CanRead Then
    11.             ' Do a simple write.
    12.             Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("NewUser:|-|ConnectMessege|-|Jeremy|=|")
    13.             networkStream.Write(sendBytes, 0, sendBytes.Length)
    14.  
    15.             Dim bytes2(tcpClient.ReceiveBufferSize) As Byte
    16.             networkStream.Read(bytes2, 0, CInt(tcpClient.ReceiveBufferSize))
    17.             Dim returndata2 As String = Encoding.ASCII.GetString(bytes2)
    18.             Console.WriteLine(("Data was:" + returndata2))
    19.  
    20.             Dim sendBytes2 As [Byte]() = Encoding.ASCII.GetBytes("Messege:|-|Whats going on?|-|BoB|=|")
    21.             networkStream.Write(sendBytes2, 0, sendBytes2.Length)
    22.             If tcpClient.NoDelay = False Then Console.WriteLine("Delay")
    23.  
    24.             ' Read the NetworkStream into a byte buffer.
    25.             Dim bytes(tcpClient.ReceiveBufferSize) As Byte
    26.             networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
    27.             ' Output the data received from the host to the console.
    28.             Dim returndata As String = Encoding.ASCII.GetString(bytes)
    29.             Console.WriteLine(("Host returned: " + returndata))
    30.  
    31.             Dim sendBytes3 As [Byte]() = Encoding.ASCII.GetBytes("Disconnect:|-||-|Jeremy|=|")
    32.             networkStream.Write(sendBytes3, 0, sendBytes3.Length)
    33.  
    34.             Dim bytes3(tcpClient.ReceiveBufferSize) As Byte
    35.             networkStream.Read(bytes3, 0, CInt(tcpClient.ReceiveBufferSize))
    36.  
    37.             Dim returndata3 As String = Encoding.ASCII.GetString(bytes3)
    38.             Console.WriteLine(("Recvied what: " + returndata3))
    39.         Else
    40.             If Not networkStream.CanRead Then
    41.                 Console.WriteLine("cannot not write data to this stream")
    42.                 tcpClient.Close()
    43.             Else
    44.                 If Not networkStream.CanWrite Then
    45.                     Console.WriteLine("cannot read data from this stream")
    46.                     tcpClient.Close()
    47.                 End If
    48.             End If
    49.         End If
    50.         ' pause so user can view the console output
    51.         Console.ReadLine()
    52.     End Sub
    53. End Class
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  23. #23
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [2005] TCPListener, Threading, Error.

    Quote Originally Posted by rack
    I'm getting responces back and forth now, but the split I call on the messege leaves the length of the last item at 8156, which is odd.
    Which "split", I don't see something like that in your code, or do I have a language problem?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  24. #24

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: [2005] TCPListener, Threading, Error.

    The split command on line 107 of the most recent server code snipet showing in the post just before your post.

    [EDIT]

    Good night, i'm going to bed now, the happiness in me is overwhelmed by the lack of sleep.

    [EDIT 2]

    Also, I know that what is happening is, while reading, I am reading the length of the read BUFFER, not the length of the read DATA. I don't know how to gather the length of the read DATA. I tried changing the tcpclient.SendBuffer on the User (client) but I believe because the Receive buffer is still 8092 or something it ends up getting a 8093 lenth byte array/string.

    So if you find out how to check the Receive read DATA let me know.

    Other then the way I am currently using (flagging the end of the messege).
    Other then sending the length of the messege with the messege, before the messege, (and useing the mid fucntion still).
    Last edited by rack; Jul 18th, 2007 at 02:23 AM.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  25. #25
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [2005] TCPListener, Threading, Error.

    Have a good one, i'm going to test this code on my side later this day (European Time!)
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  26. #26
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [2005] TCPListener, Threading, Error.

    Sendind and receiving works like a charm, however I have a problem if the client disconnects. Your code will receive empty messages in a endless loop.
    I put in this change:
    Code:
                    If ClientStatus >= 2 Then
                        If ClientStatus > 2 And TempString = "" Then
                            Exit Do
                        End If
                        ' Read the stream into a byte array
                        Dim bytes(Client.ReceiveBufferSize) As Byte
                        MyDataStream.Read(bytes, 0, CInt(Client.ReceiveBufferSize))
                        ' Return the data received from the client to the txtchat textbox.
                        TempString = Encoding.ASCII.GetString(bytes)
                        Me.SetText(TempString)
                        ClientStatus = 3
                    End If
    The Tempstring is "" when the first empty message arrives after disconnecting, however the Check Tempstring="" returns FALSE! I don't get it????
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  27. #27

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: [2005] TCPListener, Threading, Error.

    Ah, I see. I added a check for if the connection was still there. And an err.number check.

    I am going to start working on the Windows Client app now.

    Also, you may want to change my spelling errors for the word "Message" in all of the code.

    Here are some of the changes, starting from the begining.

    This was just to add the information about what the different values of Clientstatus mean.
    vb Code:
    1. Dim ClientStatus As Integer
    2.     'Clientstatus
    3.     '0 = Disconnected
    4.     '1 = Connected to User
    5.     '2 = DataStream Connected (able to receive and send messeges)

    This code goes just after the if Clientstatus=2 then line.
    vb Code:
    1. If Client.Connected = False Then
    2.                         If ClientStatus > 1 Then
    3.                             MyDataStream.Close()
    4.                         End If
    5.                         If ClientStatus > 0 Then
    6.                             Client.Close()
    7.                         End If
    8.                         ClientStatus = 0
    9.                         Exit Try
    10.                     End If

    This is the entire section from the Catch, to the End try in the DoListen Sub.
    vb Code:
    1. Catch ex As Exception
    2.                 Select Case Err.Number
    3.                     Case 57 'Connection Forcibly closed by Remote Host.
    4.                         ''Actual Error Messege Below
    5.                         ''Unable to read data from the transport connection:
    6.                         ''An existing connection was forcibly closed by the remote host.
    7.  
    8.                         'Assume the user disconnected without sending a Disconnect messege.
    9.  
    10.                         If ClientStatus > 1 Then
    11.                             MyDataStream.Close()
    12.                         End If
    13.                         If ClientStatus > 0 Then
    14.                             Client.Close()
    15.                         End If
    16.                         ClientStatus = 0
    17.                     Case Else
    18.                         MsgBox(ex.Message & vbNewLine & "Error Number:" & Err.Number & vbNewLine & "Source:" & ex.Source & vbNewLine & "StackTrace:" & ex.StackTrace)
    19.                 End Select
    20.             End Try

    Added this to the Select Case TempMessege(0) in the CheckMessege Sub.
    vb Code:
    1. Case ">-Internal-<"
    2.                 Select Case TempMessege(1)
    3.                     Case "USERDISCONNECT"
    4.                         'Send messege to other users.
    5.                         'Let them know of the disconnect.
    6.                         'Update list.
    7.                         'TempMesssege(2) will contain the username.
    8.                     Case Else
    9.  
    10.                 End Select
    11.             Case "<-REPEAT->"
    12.                 'put code hear to repeat last message sent.
    13.             Case "<-RCVD->"
    14.                 'Do nothing, the data that was sent, was received.
    15.             Case "<-ERR->"
    16.                 MsgBox("There was an error attempting to send the last peice of data.")
    Last edited by rack; Jul 19th, 2007 at 02:48 AM.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  28. #28
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [2005] TCPListener, Threading, Error.

    OK, will give it try tonight (I my family will give me the time, we're just prior leaving for summer-vacation).

    I see your checking for Client.connected=False, I checked on that, but it showed true??? all the time, although the connection was closed (without sending anything)from the other side. That'S the reason I was doing this try with the ClientStatus=3 and TempString="". I'm still wondering why a TempString="" would make a FALSE although TempString=""??
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  29. #29

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: [2005] TCPListener, Threading, Error.

    Here are the two projects.
    Both have similar functions in them. Both are windows applications with textboxes, and buttons.

    For some reason, the threading on the Client makes the CPU usage jump to 50%, I don't know why, because I don't know much at all about threading. If anyone sees my error, let me know please.

    Tcp - NS - Listener is the server.
    TCP - CLient 2 is the User.

    Open the Listener, click Listen.
    Open the CLient, click Connect.

    Make sure to edit the IP address to whatever you want to use 127.0.0.1 or whatever.

    Listener (Server)
    TCP - NS - Listener.zip
    Client (User)
    TCP - Client 2.zip
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  30. #30
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [2005] TCPListener, Threading, Error.

    Hi rack,
    tried to get the attachments of your post, got a message about invalid attachment instead, please recheck.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  31. #31
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [2005] TCPListener, Threading, Error.

    Tested it, the problem with the closed connection remains, my Client.Connected is still true, that way your If Clause doesn't do it. The check with an empty Tempstring would do it, however out off an unknown reason " TempString = "" " returns FALSE altough Tempstring="" ????? I tried it with also with VbNullChar and Nothing. I'm out off any clue!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  32. #32
    Member
    Join Date
    May 2007
    Location
    Phalaborwa, South Afirca
    Posts
    48

    Re: [2005] TCPListener, Threading, Error.

    Do you have the example "VB.NET - Advanced .NET Framework (Networking) - Use Sockets" it has an example of client server?

  33. #33
    Member
    Join Date
    May 2007
    Location
    Phalaborwa, South Afirca
    Posts
    48

    Re: [2005] TCPListener, Threading, Error.

    The "VB.NET - Advanced .NET Framework (Networking) - Use Sockets"
    If I can get my GPRS line to upload 182K ??
    Last edited by thered; Jul 19th, 2007 at 04:05 PM.

  34. #34
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [2005] TCPListener, Threading, Error.

    @thered:
    Thanks, I'll look into that example as well.

    @rack:
    We are leaving for two weeks vacation in Denmark, so you won't get any answer from me for next couple of days. Thanks a lot so far.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  35. #35

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: [2005] TCPListener, Threading, Error.

    Apparantly i'm an idiot and had too many windows open, uploaded them to one, but posted with the other window I think.
    Last edited by rack; Jul 20th, 2007 at 01:48 AM.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  36. #36

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: [2005] TCPListener, Threading, Error.

    Anyways, marking this resolved, as I have answered my questions myself. I have other questions, but that requires another thread.


    {EDIT}
    07/20/2007
    Attached Files Attached Files
    Last edited by rack; Jul 20th, 2007 at 03:02 AM.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

  37. #37
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [2005] TCPListener, Threading, Error.

    I'd check with the admins, maybe the ZIP was to big, did hold something that wasn't supposed to be posted ....
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  38. #38

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2006
    Location
    Anchorage, Alaska
    Posts
    545

    Re: [RESOLVED] [2005] TCPListener, Threading, Error.

    I updated a few things in the code.

    Found an error that was causing the client and server to both just keep sending <-REPEAT-> commands back and forth to each other.

    Added threader.thread.sleep(100) so that the listener threads won't cause 30-50% processor usage.

    Edited a few other things so that the send button displays the your name, in your chat window, when you type a message.
    Please RATE posts, click the RATE button to the left under the Users Name.

    Once your thread has been answered, Please use the Thread Tools and select RESOLVED so everyone knows your question has been answered.


    "As I look past the light, I see the world I wished tonight, never the less, sleep has come, and death shall soon follow..." © 1998 Jeremy J Swartwood

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