By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,289 Members | 1,486 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,289 IT Pros & Developers. It's quick & easy.

Socket receiving too slowly

P: n/a
I'm not a new programmer, but this one's got me stymied; hopefully it's a fairly trivial problem.

I'm using a socket connection to receive communication from a server. Normally, the entire message is received before the program moves on with its next instructions. However, at times when the processor is particularly busy the program seems to progress with its instructions without having received the entire communication. In other words, it seems to clean out the buffer, which due to the computer's business is holding only the first packet at this point, then move on without waiting for the buffer to fill with the next packet.

Is there a way to stop the program until it has been told that the entire communication has been received? Or a way to iteratively receive the packets until the program is told the communication is finished?

Here's my code:
IPHostEntry IPAdd = Dns.Resolve("servername.com");
//Configure the socket
IPEndPoint IPEP = new IPEndPoint(IPAdd.AddressList[0], 2628);
Socket s = new Socket(IPEP.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
//Construct the message
string str = "Message"
//Declare the receive buffer. Maximum of 50,000 characters.
Byte[] Receiver = new Byte[50000];
//Connect the socket
s.Connect(IPEP);
//Receive greeting. Is discarded.
Int32 bytes = s.Receive(Receiver, Receiver.Length, 0);
//Declare the send buffer. Initialize it with the query, converted into bytes.
Byte[] Sender = System.Text.Encoding.ASCII.GetBytes(str);
//Send the query.
s.Send(Sender, Sender.Length, 0);
//Receive the response into the byte array
bytes = s.Receive(Receiver, Receiver.Length, 0);
//Translate the byte array into ASCII text
strResponse = System.Text.Encoding.ASCII.GetString(Receiver, 0, bytes);
....Furthur code

Thanks for your help!
************************************************** ********************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
<M Sutherland (ta******@hotmail.com)> wrote:
I'm not a new programmer, but this one's got me stymied; hopefully
it's a fairly trivial problem.

I'm using a socket connection to receive communication from a server.
Normally, the entire message is received before the program moves on
with its next instructions. However, at times when the processor is
particularly busy the program seems to progress with its instructions
without having received the entire communication. In other words, it
seems to clean out the buffer, which due to the computer's business
is holding only the first packet at this point, then move onwithout
waiting for the buffer to fill with the next packet.

Is there a way to stop the program until it has been told that the
entire communication has been received? Or a way to iteratively
receive the packets until the program is told the communication is
finished?


Just call Receive repeatedly until you've got all the data you
expected. Of course, if you don't know how much data to expect, you're
basically stuffed - you could use asynchronous reads to keep reading
until you've not seen anything new for a second or so, but
fundamentally sockets don't provide anything saying "the communication
is finished" until the socket is closed.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

P: n/a
Hi,

How do you know the size of what you are receiving?

My guess is that you are blocking the program until one of two conditions
are met:

1- The buffer is full
2- The socket timeout and return with the number of bytes readed.

Solution:
1- Know in advance the number of bytes to expect
2- Read one byte at a time.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"M Sutherland" <ta******@hotmail.com> wrote in message
news:e7**************@TK2MSFTNGP09.phx.gbl...
I'm not a new programmer, but this one's got me stymied; hopefully it's a
fairly trivial problem.

I'm using a socket connection to receive communication from a server.
Normally, the entire message is received before the program moves on with
its next instructions. However, at times when the processor is
particularly busy the program seems to progress with its instructions
without having received the entire communication. In other words, it seems
to clean out the buffer, which due to the computer's business is holding
only the first packet at this point, then move on without waiting for the
buffer to fill with the next packet.

Is there a way to stop the program until it has been told that the entire
communication has been received? Or a way to iteratively receive the
packets until the program is told the communication is finished?

Here's my code:
IPHostEntry IPAdd = Dns.Resolve("servername.com");
//Configure the socket
IPEndPoint IPEP = new IPEndPoint(IPAdd.AddressList[0], 2628);
Socket s = new Socket(IPEP.Address.AddressFamily, SocketType.Stream,
ProtocolType.Tcp);
//Construct the message
string str = "Message"
//Declare the receive buffer. Maximum of 50,000 characters.
Byte[] Receiver = new Byte[50000];
//Connect the socket
s.Connect(IPEP);
//Receive greeting. Is discarded.
Int32 bytes = s.Receive(Receiver, Receiver.Length, 0);
//Declare the send buffer. Initialize it with the query, converted into
bytes.
Byte[] Sender = System.Text.Encoding.ASCII.GetBytes(str);
//Send the query.
s.Send(Sender, Sender.Length, 0);
//Receive the response into the byte array
bytes = s.Receive(Receiver, Receiver.Length, 0);
//Translate the byte array into ASCII text
strResponse = System.Text.Encoding.ASCII.GetString(Receiver, 0, bytes);
...Furthur code

Thanks for your help!
************************************************** ********************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP &
ASP.NET resources...

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.