Reading data from a socket: size problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Diego F.

    #1

    Reading data from a socket: size problem

    Hello. I'm having problems reading data from a socket. In my test, I see the
    maximum size to get data from the socket is 8192 bytes.

    In my tests, if I send less that 8192, the readen data have the expected
    size. But, if I send more than 8192, my code has a loop, and the second time
    it reads from the socket, it reads 8192 bytes again, instead that only the
    bytes not read before.

    I hope you can understand the problem. Here is some code that I use to test
    this:

    This is the client code:

    Dim bytes_recibidos (8192) As Byte
    Dim datos_recibidos As String

    Dim p As New IPEndPoint(IPAd dress.Parse("12 7.0.0.1"), 870)
    sock = New Socket(AddressF amily.InterNetw ork, SocketType.Stre am,
    ProtocolType.Tc p)

    Try
    sock.Connect(p)
    ListBox1.Items. Add("Conectado" )

    Dim socket_conectad o As Boolean = True
    While (socket_conecta do)
    If (sock.Poll(0, SelectMode.Sele ctRead) And (sock.Available = 0)) Then
    socket_conectad o = False
    End If

    sock.Receive(by tes_recibidos)
    datos_recibidos =
    (System.Text.En coding.ASCII.Ge tString(bytes_r ecibidos)).Trim End(Chr(0))
    ....
    End While

    Catch ex As Exception

    End Try

    And the server code to test that problem:


    Dim server As TcpListener
    server = Nothing
    Try

    server = New TcpListener(IPA ddress.Parse("1 27.0.0.1"), 870)
    ListBox1.Items. Add("Esperando conexiones...")
    server.Start()

    Dim cliente As TcpClient = server.AcceptTc pClient()

    Dim stream As NetworkStream = cliente.GetStre am()
    Dim bytes_enviados( 8192) As Byte
    For i As Integer = 1 To 106

    Dim cmd As String = Chr(0) &
    "12345678901234 567890123456789 012345678901234 567890412345678 901234567890123 465"

    bytes_enviados = System.Text.Enc oding.ASCII.Get Bytes(cmd)
    stream.Write(by tes_enviados, 0, bytes_enviados. Length)
    Next
    Catch ex As SocketException

    Finally
    server.Stop()
    End Try


    --

    Regards,

    Diego F.



  • Diego F.

    #2
    Re: Reading data from a socket: size problem

    I solved it by adding this line before reading from the socket:
    Array.Clear(byt es_recibidos, 0, bytes_recibidos .Length)

    I expected the array to be cleared itself, but I have to do it manually.

    --

    Regards,

    Diego F.



    Comment

    Working...