473,573 Members | 3,961 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Send large block of data failed

Hi,
I'm writing a program to send large file(100m) through dotnet using
TCPListener & TCPClient, I'm sending the file with a ask and response loop:

1. Client send a flag 1 to server indicate it has data send to server.
2. Client send the buffer block size.
3. Client send the actual buffer to the server.
4. Server send a flag 1 to client indicating that the buffer has been
successfully receeived.
5. The next loop until all data of the file has been sent.

When the data block is about 5k, the code works very fine, but when I set
the block size up to about 10k, after some cycles, an exception raised and
the program failed.
The most strange is: the cycles number are different every time(with the
same conditions), such as the first time I failed at the 13th loop, when I'm
runing the same pair of applications the next time, I might failed at the
17th loop with the same file.

Any help would be appreciated.

Thanks

ps: the main code is listed below, the two cs files are attached.

Client
---------------
TcpClient client = new TcpClient(txtAd dress.Text, 8000);
client.SendBuff erSize = 1024*1024;
client.ReceiveB ufferSize = 1024 * 1024;
client.NoDelay = true;

NetworkStream stream = client.GetStrea m();
byte[] length = null;
byte[] buffer = new byte[1024 *
Int32.Parse(thi s.numSize.Value .ToString())];
byte[] response = new byte[1];

while(true)
{
// Read data from file
size = fStream.Read(bu ffer, 0, buffer.Length);
if(size == 0)
{
break;
}
Console.WriteLi ne("Time " + time + ", " + size);
// Send a flag to indicate binary stream will be sent

Console.WriteLi ne("Send flag ...");
stream.WriteByt e((byte)0);
stream.Flush();
length = this.IntToBytes (size);

Console.WriteLi ne("Send length ..." + size);
// Send a length message
stream.Write(le ngth, 0, length.Length);

Console.WriteLi ne("Send buffer ... size = " + size + "(" +
buffer.Length + ")");
// Send data
stream.Write(bu ffer, 0, size);
stream.Flush();

Console.WriteLi ne("Read response ...");
// Waite for response
stream.Read(res ponse, 0, 1);
Console.WriteLi ne("Response from server is: " + response[0]);

time++;
}
stream.WriteByt e((byte)1);
stream.Flush();
stream.Close();
client.Close();
fStream.Close() ;
}
catch(Exception ex)
{
Console.WriteLi ne("time="+time );
Console.WriteLi ne(ex.Message);
Console.WriteLi ne(ex.StackTrac e);
}

Server
----------------
int time = 0;
TcpClient client = listener.Accept TcpClient();
client.SendBuff erSize = 1024*1024;
client.ReceiveB ufferSize = 1024*1024;
client.NoDelay = true;
NetworkStream stream = client.GetStrea m();

byte[] flag = new byte[1];
byte[] length = new byte[4];
byte[] body = new byte[1024 * 1024];
byte[] response = new byte[]{(byte)1};

string filename = "D:\\file.d at";

if(File.Exists( filename))
File.Delete(fil ename);
FileStream fStream = File.OpenWrite( "D:\\file.dat") ;
try
{
while(true)
{
stream.Read(fla g, 0, 1);
Console.WriteLi ne("Read flag ... " + flag[0]);
if(flag[0] == (byte)0)
{
stream.Read(len gth, 0, 4);
int size = BytesToInt(leng th);
Console.WriteLi ne("Read buffer size + " + size);
size = stream.Read(bod y, 0, size);
Console.WriteLi ne("Read buffer ...");

Console.Write(" Time " + time + ", " + size);
fStream.Write(b ody, 0, size);
fStream.Flush() ;
Console.WriteLi ne(" Send response");
stream.WriteByt e(response[0]);
stream.Flush();

time++;
}
else
{
Console.WriteLi ne("flag = " + flag[0]);
break;
}
}
}
catch(Exception e)
{
Console.WriteLi ne(e.Message);
Console.WriteLi ne(e.StackTrace );
}

Console.WriteLi ne("server time = " + time);
fStream.Close() ;
stream.Close();
client.Close();


Nov 15 '05 #1
4 8046
It would help if you told us which exception you get.

Nevertheless, it appears to me that the server expects to read all the data
that was sent in one read. This has a good chance of failing because when
you call stream.Read(bod y, 0, size) it can return anything from 0 to size
bytes (check out the documentation for NetworkStream.R ead). To make sure you
get all the data that was sent, you need to call stream.Read in a loop:

int size = BytesToInt(leng th);
int pos = 0;
while (size > 0)
{
int nread = stream.Read(bod y, pos, size);
if (nread == 0) break; // connection was closed
size -= nread;
pos += nread;
}

Sami
www.capehill.net

does not work because
"zhimin" <xi************ @163.com> wrote in message
news:Oe******** ********@TK2MSF TNGP10.phx.gbl. ..
Hi,
I'm writing a program to send large file(100m) through dotnet using
TCPListener & TCPClient, I'm sending the file with a ask and response loop:
1. Client send a flag 1 to server indicate it has data send to server.
2. Client send the buffer block size.
3. Client send the actual buffer to the server.
4. Server send a flag 1 to client indicating that the buffer has been
successfully receeived.
5. The next loop until all data of the file has been sent.

When the data block is about 5k, the code works very fine, but when I set
the block size up to about 10k, after some cycles, an exception raised and
the program failed.
The most strange is: the cycles number are different every time(with the
same conditions), such as the first time I failed at the 13th loop, when I'm runing the same pair of applications the next time, I might failed at the
17th loop with the same file.

Any help would be appreciated.

Thanks

ps: the main code is listed below, the two cs files are attached.

Client
---------------
TcpClient client = new TcpClient(txtAd dress.Text, 8000);
client.SendBuff erSize = 1024*1024;
client.ReceiveB ufferSize = 1024 * 1024;
client.NoDelay = true;

NetworkStream stream = client.GetStrea m();
byte[] length = null;
byte[] buffer = new byte[1024 *
Int32.Parse(thi s.numSize.Value .ToString())];
byte[] response = new byte[1];

while(true)
{
// Read data from file
size = fStream.Read(bu ffer, 0, buffer.Length);
if(size == 0)
{
break;
}
Console.WriteLi ne("Time " + time + ", " + size);
// Send a flag to indicate binary stream will be sent

Console.WriteLi ne("Send flag ...");
stream.WriteByt e((byte)0);
stream.Flush();
length = this.IntToBytes (size);

Console.WriteLi ne("Send length ..." + size);
// Send a length message
stream.Write(le ngth, 0, length.Length);

Console.WriteLi ne("Send buffer ... size = " + size + "(" +
buffer.Length + ")");
// Send data
stream.Write(bu ffer, 0, size);
stream.Flush();

Console.WriteLi ne("Read response ...");
// Waite for response
stream.Read(res ponse, 0, 1);
Console.WriteLi ne("Response from server is: " + response[0]);

time++;
}
stream.WriteByt e((byte)1);
stream.Flush();
stream.Close();
client.Close();
fStream.Close() ;
}
catch(Exception ex)
{
Console.WriteLi ne("time="+time );
Console.WriteLi ne(ex.Message);
Console.WriteLi ne(ex.StackTrac e);
}

Server
----------------
int time = 0;
TcpClient client = listener.Accept TcpClient();
client.SendBuff erSize = 1024*1024;
client.ReceiveB ufferSize = 1024*1024;
client.NoDelay = true;
NetworkStream stream = client.GetStrea m();

byte[] flag = new byte[1];
byte[] length = new byte[4];
byte[] body = new byte[1024 * 1024];
byte[] response = new byte[]{(byte)1};

string filename = "D:\\file.d at";

if(File.Exists( filename))
File.Delete(fil ename);
FileStream fStream = File.OpenWrite( "D:\\file.dat") ;
try
{
while(true)
{
stream.Read(fla g, 0, 1);
Console.WriteLi ne("Read flag ... " + flag[0]);
if(flag[0] == (byte)0)
{
stream.Read(len gth, 0, 4);
int size = BytesToInt(leng th);
Console.WriteLi ne("Read buffer size + " + size);
size = stream.Read(bod y, 0, size);
Console.WriteLi ne("Read buffer ...");

Console.Write(" Time " + time + ", " + size);
fStream.Write(b ody, 0, size);
fStream.Flush() ;
Console.WriteLi ne(" Send response");
stream.WriteByt e(response[0]);
stream.Flush();

time++;
}
else
{
Console.WriteLi ne("flag = " + flag[0]);
break;
}
}
}
catch(Exception e)
{
Console.WriteLi ne(e.Message);
Console.WriteLi ne(e.StackTrace );
}

Console.WriteLi ne("server time = " + time);
fStream.Close() ;
stream.Close();
client.Close();

Nov 15 '05 #2
Thank you for your help.

I had debug my program, the error should be description as the following:

As the program described, in the request and response cycle, the client send
a byte(which value is zero) to the server to indicate this session will send
a file stream. The server recieve this byte flag, if this byte is equals to
zero, receive data, else close this session. Some of the first sessions are
fine, but after a while, the client send a byte flag which value is zero to
the server, while the server recived it and the value is not zero! The
server then close the tcpclient, and the client throw an exception. And more
large the block is, the error will occured more early.
Nov 15 '05 #3

"zhimin" <xi************ @163.com> wrote in message
news:ON******** ********@TK2MSF TNGP10.phx.gbl. ..
Thank you for your help.

I had debug my program, the error should be description as the following:

As the program described, in the request and response cycle, the client send a byte(which value is zero) to the server to indicate this session will send a file stream. The server recieve this byte flag, if this byte is equals to zero, receive data, else close this session. Some of the first sessions are fine, but after a while, the client send a byte flag which value is zero to the server, while the server recived it and the value is not zero! The
server then close the tcpclient, and the client throw an exception. And more large the block is, the error will occured more early.


This sounds very much like you are running into TCP streaming issues.

Here's one possible scenario how it could fail:

- client sends the 0 flag
- server reads flag
- client sends the length of data (assume its 100 bytes)
- server reads length
- client sends 100 bytes
- server calls size = stream.Read(bod y, 0, size); where size is 100 but
receives only 50 bytes!
=> the remaining 50 bytes then arrive in the receive buffer and the
server is now out of sync
- server loops and tries to read the flag again but now its getting
actually the 1st byte of the 50 bytes that still remain in the receive
buffer. This byte happens to be nonzero so the server closes connection.

To fix this problem, you must call networkStream.R ead in a loop whenever you
expect to read more than 1 byte. This is TCP stream programming 101.

This may or may not resolve all problems, but I think you should start by
fixing this issue first.

Sami
www.capehill.net
Nov 15 '05 #4
Thanks for you help, all trouble fixed!
Nov 15 '05 #5

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

Similar topics

1
2287
by: Markus L?ffler | last post by:
Hi all, I'm looking for a class to access large memory blocks of dynamic length in an efficient way. Basically the simplest way to allocate a memory block is to allocate a byte . If you now want that this memory block can grow dynamically, then you usually allocate a byte of a certain size, then if size is outgrown, you create a bigger...
6
6135
by: shailesh kumar | last post by:
Hi, I need to design data interfaces for accessing files of very large sizes efficiently. The data will be accessed in chunks of fixed size ... My data interface should be able to do a random seek in the file, as well as sequential access block by block.... One aspect of the usage of this interface is that there is quite good chance of...
4
9420
by: jas | last post by:
I have a basic client/server socket situation setup....where the server accepts a connection and then waits for commands. On the client side, I create a socket, connect to the server...then I pass the socket to a class which just reads from the socket (in a thread). class Reader(Thread): def run(self): while 1:
0
913
by: zhimin | last post by:
Hi, I'm writing a program to send large file(100m) through dotnet using TCPListener & TCPClient, I'm sending the file with a ask and response loop: 1. Client send a flag 1 to server indicate it has data send to server. 2. Client send the buffer block size. 3. Client send the actual buffer to the server. 4. Server send a flag 1 to client...
12
6173
by: Sharon | last post by:
I’m wrote a small DLL that used the FreeImage.DLL (that can be found at http://www.codeproject.com/bitmap/graphicsuite.asp). I also wrote a small console application in C++ (unmanaged) that uses the DLL above. Now the application, together with the above DLL’s is successfully loading a TIF image file (62992 x 113386 Pixels, Huffman RLE...
16
3397
by: Claudio Grondi | last post by:
What started as a simple test if it is better to load uncompressed data directly from the harddisk or load compressed data and uncompress it (Windows XP SP 2, Pentium4 3.0 GHz system with 3 GByte RAM) seems to show that none of the in Python available compression libraries really works for large sized (i.e. 500 MByte) strings. Test the...
6
4137
by: Mukesh | last post by:
Hi I have Microsoft Enterprise Library 2005 installed on my local system. I m also using ASp.net 1.1 And C3 as coding language , I have MS Sql Server 2000. I am developing a web application in which i am using data Access Application Block for data access. I have a remote web n data server with asp.net 1.1 support
3
1807
by: Mukesh | last post by:
Hi all As per my earlier conversation with Ciaran (thx for reply) I have installed the MS APplication block on the server , when i ran Build Enterprise Library file and Install Services from (batch files ) programme files menu it was asking for visual studio 2003 , I have only .net framework on the server how can i use the MS application...
2
4116
by: apollo135 | last post by:
Dear All, Could someone help and tell me how to handle multiple send and receive operations with udp sockets? In fact here is my problem: server.c is composing of serveral sub programs (the same for client.c) -----server.c------- ................ int main(){ ............
5
9723
by: Markgoldin | last post by:
I am searching for a solution of sending messages from not .Net process into a .Net process written in C#. I have gone already thru sockets solution and have porblems with it. I am not a C# coder dont kill :) I am thinking about something like this: A remote non .Net process has a window with a name known to .Net program. Can .Net program...
0
7792
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7709
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8039
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8218
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8091
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6435
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3738
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1049
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.