Results 1 to 5 of 5

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

  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.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

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

    A couple suggestions first off:

    1) Edit that post, seclect all the code, and use the VBCode tags (should be up above the textbox when you edit). This will format it in a much more readable way.

    2) Get away from using On Error Goto in favor of the more .NET compliant structured exception handling with Try...Catch blocks.

    3) Tell me something about the problem. You said in that other post that the problem occurs when the string is >512, but you also said something about <1024. Is it the case that only strings of intermediate length fail? That would be really weird. Anything over some size would make more sense.

    Also, what happens when it fails, and at what point does it fail? Does nothing come through, or only the first part? Or is it that the reply doesn't come through?

    Depending on that, I might have an answer, though I have never actually liked my answer to this.
    My usual boring signature: Nothing

  3. #3

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

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

    1. Done (edited it before thats why the vbcode tag stuffed up)

    2. Used on error just to check it anything comes through.

    3. I send a accross a string say "TEST TEST TEST!" it comes through fine,
    but when i send the contents of a textbox, which is much longer in size, nothing comes through. The lengh of the string in textbox is variable range between 512-1500 characters.

    I adding most of the things into the textbox, then i want to send it all accross at once.
    Could the VbCrlf cousing the issue? Cause thats what i'm using to to move onto the next line in the textbox field. When i limit my textbox to a single string without the VbCrLf to move to the next line, it seems to work. I'm puzzeled?

    Surely you can transmit VbCrLf....

  4. #4
    Member
    Join Date
    May 2006
    Posts
    60

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

    I THINK you're onto the issue.
    I remember reading that when you send data with sockets they're terminated with the line-feed charater?

    I THINK that is correct.

  5. #5

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

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

    Yeah gotten around it... without using the bloody VbCrLf.
    Just dumped it all into an array then tranmitted 1 by 1. Worked fine this way.

    :-)

    Thanx for letting me know that it terminates the transmission... seems to be the case.

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