Results 1 to 5 of 5

Thread: Sockets/TCP Send problem, send works for short strings...

Threaded View

  1. #1

    Thread Starter
    Junior Member Dario's Avatar
    Join Date
    Oct 2006
    Posts
    25

    Sockets/TCP Send problem, send works for short strings...

    i'm working on a simple sender/receiver... but i got a problem.
    It seems to send small strings accross fine ... but not the content of my MULTI LINE textbox. LENGH OF TEXTBOX 524 CHARACTERS.

    Transmitter
    VB Code:
    1. Dim TcpClient As New TcpClient
    2.  
    3.         If Not TcpClient.Connected Then
    4.             TcpClient.Connect(9000, "127.0.0.1")
    5.         End If
    6.         Dim networkStream As NetworkStream = TcpClient.GetStream()
    7.  
    8.         '        If networkStream.CanWrite Then
    9.         Dim sendbytes(10240) As Byte
    10.        
    11. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    12. ''' REPLACE "TEST DATA TEST DATA" WITH A MULTI-LINE TEXT BOX RESULT
    13. ''' VALUE.
    14. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    15.  
    16.  
    17. sendbytes = Encoding.ASCII.GetBytes("TEST DATA TEST DATA")
    18.         networkStream.Write(sendBytes, 0, sendBytes.Length)
    19.         'End If
    20.         TcpClient.Close()

    Listener... just spits the output to console....
    VB Code:
    1. Dim server As TcpListener
    2.         server = Nothing
    3.         'On Error Resume Next
    4.         Try
    5.             ' Set the TcpListener on port 9000.
    6.             Dim port As Int32 = 9000
    7.             Dim localAddr As System.Net.IPAddress = System.Net.IPAddress.Parse("127.0.0.1")
    8.  
    9.             server = New TcpListener(localAddr, port)
    10.  
    11.             ' Start listening for client requests.
    12.             server.Start()
    13.  
    14.             ' Buffer for reading data
    15.             Dim bytes(20480) As Byte
    16.             Dim data As String = Nothing
    17.  
    18.             ' Enter the listening loop.
    19.             While True
    20.                 Console.Write("Waiting for a connection... ")
    21.  
    22.                 ' Perform a blocking call to accept requests.
    23.                 ' You could also user server.AcceptSocket() here.
    24.                 Dim client As TcpClient = server.AcceptTcpClient()
    25.                 Console.WriteLine("Connected!")
    26.  
    27.                 data = Nothing
    28.  
    29.                 ' Get a stream object for reading and writing
    30.                 Dim stream As NetworkStream = client.GetStream()
    31.  
    32.                 Dim i As Int32
    33.  
    34.                 ' Loop to receive all the data sent by the client.
    35.                 i = stream.Read(bytes, 0, bytes.Length)
    36.                 While (i <> 0)
    37.                     ' Translate data bytes to a ASCII string.
    38.                     data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
    39.                     Console.WriteLine("Received: {0}", data)
    40.  
    41.                     ' Process the data sent by the client.
    42.                     data = data.ToUpper()
    43.                     'Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(data)
    44.  
    45.                     ' Send back a response.
    46.                     'stream.Write(msg, 0, msg.Length)
    47.                     'Console.WriteLine("Sent: {0}", data)
    48.  
    49.                     i = stream.Read(bytes, 0, bytes.Length)
    50.  
    51.                 End While
    52.  
    53.                 ' Shutdown and end connection
    54.                 client.Close()
    55.             End While
    56.         Catch e As SocketException
    57.             Console.WriteLine("SocketException: {0}", e)
    58.         Finally
    59.             server.Stop()
    60.  
    61.         End Try
    62.  
    63.         Console.WriteLine(ControlChars.Cr + "Hit enter to continue....")
    64.         Console.Read()
    Last edited by Dario; Oct 30th, 2006 at 04:02 AM.

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