473,386 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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(IPAddress.Parse("127.0.0.1"), 870)
sock = New Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp)

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

Dim socket_conectado As Boolean = True
While (socket_conectado)
If (sock.Poll(0, SelectMode.SelectRead) And (sock.Available = 0)) Then
socket_conectado = False
End If

sock.Receive(bytes_recibidos)
datos_recibidos =
(System.Text.Encoding.ASCII.GetString(bytes_recibi dos)).TrimEnd(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(IPAddress.Parse("127.0.0.1"), 870)
ListBox1.Items.Add("Esperando conexiones...")
server.Start()

Dim cliente As TcpClient = server.AcceptTcpClient()

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

Dim cmd As String = Chr(0) &
"1234567890123456789012345678901234567890123456789 0412345678901234567890123465"

bytes_enviados = System.Text.Encoding.ASCII.GetBytes(cmd)
stream.Write(bytes_enviados, 0, bytes_enviados.Length)
Next
Catch ex As SocketException

Finally
server.Stop()
End Try
--

Regards,

Diego F.

Jun 4 '07 #1
1 2500
I solved it by adding this line before reading from the socket:
Array.Clear(bytes_recibidos, 0, bytes_recibidos.Length)

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

--

Regards,

Diego F.

Jun 4 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code:...
7
by: Nuno Magalhaes | last post by:
I've got a problem which relates to reading HTTP data. I've got the socket connected to a web site and then I send "GET / HTTP/1.1\n\n" and the page is received after a while but not all of the...
5
by: Dave A | last post by:
I have an application that does lots of socket communications all asynchronously via the TcpClient class. The code has been working 99.9999% of the time (yeah one of those bugs) but occasionally...
9
by: Macca | last post by:
Hi, I have a synchronous socket server which my app uses to read data from clients. To test this I have a simulated client that sends 100 byte packets. I have set up the socket server so...
4
by: Patrick Altman | last post by:
I am attempting to use a HEAD request against Amazon S3 to check whether a file exists or not and if it does parse the md5 hash from the ETag in the response to verify the contents of the file so...
1
by: Diego F. | last post by:
Hello. I have a problem while reading bytes from a socket. According to my test, it seems that the buffer maximum size is 8192 bytes. Then, I use a byte array with that size to read from the...
0
by: george585 | last post by:
Hello! I am new to network programming, and understand just basics. Using some sample code, and having read documentation, I managed to create a simple app in C# and VB.NET. The application is...
3
by: Willy Stevens | last post by:
Hello, In my application I have to read sometimes quite big chunk of binary data. I have a buffer which default size is 32000 bytes. But how could I read binary data that exceeds 32000 bytes?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.