472,969 Members | 1,659 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Reading network socket stream, slow connection

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
}
}

Apr 7 '07 #1
3 6680
On Fri, 06 Apr 2007 21:18:38 -0700, Sir Psycho
<to*********************@hotmail.comwrote:
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
Apr 7 '07 #2
On Apr 7, 2:37 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Fri, 06 Apr 2007 21:18:38 -0700, Sir Psycho

<totalharmonicdistort...@hotmail.comwrote:
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

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);
}
}

Apr 7 '07 #3
On Sat, 07 Apr 2007 08:57:43 -0700, Sir Psycho
<to*********************@hotmail.comwrote:
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?
Apr 7 '07 #4

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

Similar topics

1
by: John | last post by:
I have a Socket open to a target system. I get the network stream from the open socket and then create a stream reader and stream writer on this stream. The stream reader and writers are operating...
4
by: DreJoh | last post by:
I've read many articles on the subject and the majority of them give the same solution that's in article 821625 on the MSDN website. I'm using the following code and when a the client disconnects...
21
by: JoKur | last post by:
Hello, First let me tell you that I'm very new to C# and learning as I go. I'm trying to write a client application to communicate with a server (that I didn't write). Each message from the...
1
by: Frosty Madness | last post by:
I have the beginnings of a device emulator that sits on the network... ************** using System; using System.Threading; using System.Net; using System.Net.Sockets; using System.Text;
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...
0
by: John J. Hughes II | last post by:
I am using the following code to monitor a socket port. The code normally works fine unless a network connection is lost. Since both ends of the conversation on the same computer (normally) this...
3
by: Ryan Liu | last post by:
Hi, I use Server: Use an endless thread to lisiten to clients requests: while(true) { TcpClient client = myListener.AcceptTcpClient();
1
by: Ryan Liu | last post by:
Hi, I have a 100 clients/ one server application, use ugly one thread pre client approach. And both side user sync I/O. I frequently see the error on server side(client side code is same, but...
10
by: puzzlecracker | last post by:
Say I want to arrange bytes in the internal buffer in a certain way. I receive those bytes in the socket. One solution is to read in socket in pieces: byte buffer = new byte; int index = 0;...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.