473,387 Members | 1,578 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,387 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 6726
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.