473,396 Members | 2,010 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,396 software developers and data experts.

Socket receive question

Ole
Hi

I'm receiving data from another device that sends data in packages of 8000.
In my application I use this code to receive the data:
SizeReceived = MySocket.Receive(Buffer, 8000, SocketFlags.None);

But the receive-method sometimes returns after reading 4378 bytes?? Isn't it
so that the Socket.receive method should block until all the data is
received?

Thanks,
Ole
Nov 6 '06 #1
5 1618
Hi,
Nope, you maust call Recieve while you get all data, as many times it needs.

Receive block only if no data available, till there is some data. But you
never be sure how many you will recieve, so you need to check SizeReceived.

"Ole" <ol*@blabla.comwrote in message
news:e1**************@TK2MSFTNGP04.phx.gbl...
Hi

I'm receiving data from another device that sends data in packages of
8000. In my application I use this code to receive the data:
SizeReceived = MySocket.Receive(Buffer, 8000, SocketFlags.None);

But the receive-method sometimes returns after reading 4378 bytes?? Isn't
it so that the Socket.receive method should block until all the data is
received?

Thanks,
Ole

Nov 6 '06 #2
Ole
Thanks! - Does that mean that I could receive even a larger number than I
ask for?

Thanks,
Ole
"Ivar" <iv*@lumisoft.eewrote in message
news:u9**************@TK2MSFTNGP03.phx.gbl...
Hi,
Nope, you maust call Recieve while you get all data, as many times it
needs.

Receive block only if no data available, till there is some data. But you
never be sure how many you will recieve, so you need to check
SizeReceived.

Nov 6 '06 #3

"Ole" <ol*@blabla.comwrote in message
news:e1**************@TK2MSFTNGP04.phx.gbl...
Hi

I'm receiving data from another device that sends data in packages of
8000. In my application I use this code to receive the data:
SizeReceived = MySocket.Receive(Buffer, 8000, SocketFlags.None);

But the receive-method sometimes returns after reading 4378 bytes?? Isn't
it so that the Socket.receive method should block until all the data is
received?
No, it's just a wrapper around the Berkely sockets API. That's why the
actual number of bytes read is returned. It's trivial to write a wrapper
method that loops until the desired amount of data is obtained.
>
Thanks,
Ole

Nov 6 '06 #4

Nope, because you never can't get more data than passed buffer, because
recieved data is to Buffer.

You must do something like:

byte[] buffer = new byte[8000];
int totalReaded = 0;
while(countReaded < 8000){
int sizeReaded = MySocket.Receive(buffer,totalReaded,buffer.Length -
totalReaded, SocketFlags.None);
totalReaded += totalReaded;
}

Similar code gets exactly 8000 bytes as you need.

"Ole" <ol*@blabla.comwrote in message
news:Ow**************@TK2MSFTNGP03.phx.gbl...
Thanks! - Does that mean that I could receive even a larger number than I
ask for?

Thanks,
Ole
"Ivar" <iv*@lumisoft.eewrote in message
news:u9**************@TK2MSFTNGP03.phx.gbl...
>Hi,
Nope, you maust call Recieve while you get all data, as many times it
needs.

Receive block only if no data available, till there is some data. But
you never be sure how many you will recieve, so you need to check
SizeReceived.


Nov 6 '06 #5
Ole
Great - thanks a lot for your help and advices Ivan and Ben.

BR
Ole
"Ivar" <iv*@lumisoft.eewrote in message
news:Ot**************@TK2MSFTNGP02.phx.gbl...
>
Nope, because you never can't get more data than passed buffer, because
recieved data is to Buffer.

You must do something like:

byte[] buffer = new byte[8000];
int totalReaded = 0;
while(countReaded < 8000){
int sizeReaded = MySocket.Receive(buffer,totalReaded,buffer.Length -
totalReaded, SocketFlags.None);
totalReaded += totalReaded;
}

Similar code gets exactly 8000 bytes as you need.

Nov 6 '06 #6

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

Similar topics

0
by: Nagy László Zsolt | last post by:
Hi Python Gurus! Here is a method I used before to receive data over a socket (with Python 2.2): SELECT_GRANULARITY = 0.1 # 0.1 seconds def readdata(self,length,timeout): res = '' remain...
2
by: dream machine | last post by:
Hi all , with BegeinReceive I can build async method of Socket Class that Receive the data from the Socket Client . My question is , if I have this code that create 3 Receive Async Call : ...
4
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed...
5
by: mscirri | last post by:
The code below is what I am using to asynchronously get data from a PocketPC device. The data comes in fine in blocks of 1024 bytes but even when I send no data from the PocketPC constant blocks of...
2
by: djc | last post by:
I read a network programming book (based on framework 1.1) which indicated that you should 'never' use the RecieveTimeout or the SendTimeout 'socket options' on TCP sockets or you may loose data. I...
4
by: Engineerik | last post by:
I am trying to create a socket server which will listen for connections from multiple clients and call subroutines in a Fortran DLL and pass the results back to the client. The asynchronous socket...
6
by: Pat B | last post by:
Hi, I'm writing my own implementation of the Gnutella P2P protocol using C#. I have implemented it using BeginReceive and EndReceive calls so as not to block when waiting for data from the...
4
by: Zytan | last post by:
This may be the dumbest question of all time, but... When I set the packet size, does it mean ALL packets are that size, no matter what? Let's say the packet size is 8KB, and I send a 5 byte...
0
by: =?Utf-8?B?UmFqbmk=?= | last post by:
Dear William Stacey, I have written a server code using the Windows Socket API's. Wherein I have created the socket and bound it to a particular IP address and port number. Later I have made the...
2
by: Ali Hamad | last post by:
Hello All : A socket question from a networking newbie. I need to create a server that: 1) receive a message from client. 2) check that message and response to it. 3) the client get the...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.