Socket receiving too slowly

T

tantiboh

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...
 
J

Jon Skeet [C# MVP]

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.
 
I

Ignacio Machin \( .NET/ C# MVP \)

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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top