Reading network socket stream, slow connection

S

Sir Psycho

Hi,

For some reason, when i step over this code, it returns the full byte
stream im expecting from the server, however when I let it run with no
intervention, it only seems to grab a small chunk on the stream.

What am I doing wrong? There is more code than this, but this is the
problem code.


Int32 bytes = new Int32();
data = new Byte[1024 * B_SIZE];
NetworkStream stream;

while (bytes.Equals(0)) {
while (stream.DataAvailable) {
bytes = stream.Read(data, 0, data.Length); //Works
fine when I step over, almost if i've let time pass
}
}
 
P

Peter Duniho

Hi,

For some reason, when i step over this code, it returns the full byte
stream im expecting from the server, however when I let it run with no
intervention, it only seems to grab a small chunk on the stream.

If you step over it in the debugger, your program is paused long enough
for the network driver to receive all of the data you're expecting. When
your program runs without intervention, it reads data faster than it can
be sent over the network, and so only reads a little bit at a time
(whatever's been received by the network driver since the last time you
read some data).

Standard mistake most beginning network programmers who haven't studied
the documentation make: stream-oriented protocols (like TCP) do not
preserve data boundaries imposed by the sender. The bytes are guaranteed
to be in the right order, but they may be grouped in any manner. For any
given receive operation in your code, you might only receive a single
byte, you might read everything all at once (or at least up to whatever
the buffer you provided to read into), or anything in between.
What am I doing wrong? There is more code than this, but this is the
problem code.

It's not clear from the code you posted that you are doing anything
wrong. It depends on what you do the byte array after you receive it.
The behavior you're seeing is by design, so as long as you properly
accumulate the received data until you have enough to accomplish whatever
processing you're going to do with the data, that's fine.

If your code assumes that it will receive exactly the same thing that the
sender sent, all of the bytes all at once, then that's a mistake and you
need to write your code to properly receive the data until you have
everything you expect for processing.

Pete
 
S

Sir Psycho

If you step over it in the debugger, your program is paused long enough
for the network driver to receive all of the data you're expecting. When
your program runs without intervention, it reads data faster than it can
be sent over the network, and so only reads a little bit at a time
(whatever's been received by the network driver since the last time you
read some data).

Standard mistake most beginning network programmers who haven't studied
the documentation make: stream-oriented protocols (like TCP) do not
preserve data boundaries imposed by the sender. The bytes are guaranteed
to be in the right order, but they may be grouped in any manner. For any
given receive operation in your code, you might only receive a single
byte, you might read everything all at once (or at least up to whatever
the buffer you provided to read into), or anything in between.


It's not clear from the code you posted that you are doing anything
wrong. It depends on what you do the byte array after you receive it.
The behavior you're seeing is by design, so as long as you properly
accumulate the received data until you have enough to accomplish whatever
processing you're going to do with the data, that's fine.

If your code assumes that it will receive exactly the same thing that the
sender sent, all of the bytes all at once, then that's a mistake and you
need to write your code to properly receive the data until you have
everything you expect for processing.

Pete


Hi Pete,

Here is my code. The problem is commented //PROBLEM PART OF CODE

If you could shed some light on receiving data id appreciate it.

public class POP3Server : TcpClient {

private const int B_SIZE = 5; //Response buffer size, KB

TcpClient tcpc;
NetworkStream stream;
Byte[] dataReceived;
int receiveSize;


public POP3Server(string host, int port, string user, string
pass) {

try {

tcpc = new TcpClient();

tcpc.Connect(host, port);
stream = tcpc.GetStream();

/* Doesnt matter what the response is, the
ReceiveResponse() method will determind
if the commands have been sent with success as POP3
returns +OK
*/
ReceiveResponse(out dataReceived);

SendCommand("USER " + user + "\r\n");
ReceiveResponse(out dataReceived);

SendCommand("PASS " + pass + "\r\n");
ReceiveResponse(out dataReceived);

}
catch (Exception e) {
tcpc.Close();
throw e;
}
}

public bool Connected {
get { return tcpc.Connected; }
}

public POP3MessageArray ReadMessages() {

int msgIndex = 0;
int msgNum = 0;
long msgSize = 0;

try {

SendCommand("LIST\r\n");
receiveSize = ReceiveResponse(out dataReceived);

string resStr = Encoding.ASCII.GetString(dataReceived,
0, receiveSize);
string[] list = resStr.Split("\r\n".ToCharArray());


foreach (string line in list) {

//msgIndex++;

//if ((msgIndex >= 3) && (msgIndex % 2 == 1)) {

// if (Int32.TryParse(line.Substring(0,
2).Trim(), out msgNum)) {

// long.TryParse(line.Substring(2), out
msgSize);


// }
//}

#if DEBUG
System.Diagnostics.Debug.Print("LIST: " +
line);
#endif
}

return null;
}

catch (Exception e) {

throw e;
}
}

private void SendCommand(string cmd) {

Byte[] data;

data = Encoding.ASCII.GetBytes(cmd); //Build byte
stream
stream.Write(data, 0, data.Length); //Send byte stream
to server

#if DEBUG
System.Diagnostics.Debug.Print("Sent command: " +
cmd);
#endif
}

private Int32 ReceiveResponse(out Byte[] data) {

Int32 bytes = new Int32();
data = new Byte[1024 * B_SIZE];


//PROBLEM PART OF CODE
while (stream.DataAvailable) {
bytes = bytes + stream.Read(data, 0, data.Length);
}

/END

/*
The 43 stands for '+'. This means success on POP3
servers.
*/
if (!data[0].Equals(43)) {
throw new POP3Exception("An error occurred while
communicating with the POP3 server. Response: " +
Encoding.ASCII.GetString(data));
}

#if DEBUG
System.Diagnostics.Debug.Print("Received: " +
Encoding.ASCII.GetString(data));
#endif

return bytes;
}

protected override void Dispose(bool disposing) {
tcpc.Close();
base.Dispose(disposing);
}
}
 
P

Peter Duniho

Here is my code. The problem is commented //PROBLEM PART OF CODE

If you could shed some light on receiving data id appreciate it.

I already answered the original question. Is there something else you
need help with?
 

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