471,610 Members | 1,350 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Can someone help me

Chi
what is the "unable to write data to the transport connection"
I use the oreilly , programming c#

using System;
using System.Net.Sockets;
using System.Text;
using System.IO;
// get a file name from the client
// open the file and send the
// contents from the server to the client
public class AsynchNetworkFileServer
{
class ClientHandler
{
// constructor
public ClientHandler(
Socket socketForClient )
{
// initialize member variable
socket = socketForClient;
// initialize buffer to hold
// contents of file

buffer = new byte[BufferSize];
// create the network stream
networkStream =
new NetworkStream(socketForClient);
// set the file callback for reading
// the file
myFileCallBack =
new AsyncCallback(this.OnFileCompletedRead);
// set the callback for reading from the
// network stream
callbackRead =
new AsyncCallback(this.OnReadComplete);
// set the callback for writing to the
// network stream
callbackWrite =
new AsyncCallback(this.OnWriteComplete);
inputStream= null;

}
// begin reading the string from the client
public void StartRead( )
{
// read from the network
// get a filename
networkStream.BeginRead(
buffer, 0, buffer.Length,
callbackRead, null);
}
// when called back by the read, display the string
// and echo it back to the client
private void OnReadComplete( IAsyncResult ar )
{
int bytesRead = networkStream.EndRead(ar);
// if you got a string
if( bytesRead > 0 )
{
// turn the string to a file name
string fileName =
System.Text.Encoding.ASCII.GetString(
buffer, 0, bytesRead);
// update the console
Console.Write(
"Opening file {0}", fileName);
// open the file input stream
if(inputStream!=null)
{
inputStream =
File.OpenRead(fileName);
}
else
{
inputStream= new FileStream(fileName,FileMode.Open);
}
// begin reading the file
inputStream.BeginRead(
buffer, // holds the results
0, // offset
buffer.Length, // BufferSize
myFileCallBack, // call back delegate
null); // local state object

}
else
{
Console.WriteLine( "Read connection dropped");
networkStream.Close( );
socket.Close( );
networkStream = null;
socket = null;
}
}
// when you have a buffer-full of the file
void OnFileCompletedRead(IAsyncResult asyncResult)
{
int bytesRead =
inputStream.EndRead(asyncResult);
// if you read some file
try
{
if (bytesRead > 0)
{
// write it out to the client
networkStream.BeginWrite(
buffer, 0, bytesRead, callbackWrite, null);
}
}
catch(Exception e)
{
//Have problem in here
Console.WriteLine(e.Message);
}
}
// after writing the string, get more of the file
private void OnWriteComplete( IAsyncResult ar )
{
networkStream.EndWrite(ar);
Console.WriteLine( "Write complete");
// begin reading more of the file
inputStream.BeginRead(
buffer, // holds the results
0, // offset
buffer.Length, // (BufferSize)
myFileCallBack, // call back delegate
null); // local state object
}
private const int BufferSize = 2048;
private byte[] buffer;
private Socket socket;
private NetworkStream networkStream;
private FileStream inputStream;
private AsyncCallback callbackRead;
private AsyncCallback callbackWrite;
private AsyncCallback myFileCallBack;
}
public static void Main( )
{
AsynchNetworkFileServer app =
new AsynchNetworkFileServer( );

app.Run( );
}
private void Run( )
{
// create a new TcpListener and start it up
// listening on port 65000
TcpListener tcpListener = new TcpListener(65000);
tcpListener.Start( );
// keep listening until you send the file
for (;;)
{
// if a client connects, accept the connection
// and return a new socket named socketForClient
// while tcpListener keeps listening
Socket socketForClient =
tcpListener.AcceptSocket( );
if (socketForClient.Connected)
{
Console.WriteLine("Client connected");
ClientHandler handler =
new ClientHandler(socketForClient);
handler.StartRead( );
}
}
}
}
Jan 26 '06 #1
3 2583
Chi wrote:
what is the "unable to write data to the transport connection"
That's hard to say with no information about where you're getting it,
or under what situation.
I use the oreilly , programming c#


So was this code taken directly from the book, or is that just what
you're learning from?

One thing which concerns me is that your OnReadComplete assumes that
whenever you've successfully read a piece of data, that it must contain
a complete filename. What happens if a long filename is sent which is
split over multiple packets, for instance?

Treat it as a stream of data, with no end other than when the
connection is dropped. If you need to signal one piece of data, either
have something saying when it's ended, or specify to start with how
long that piece of data is.

Jon

Jan 26 '06 #2
Chi
Thx first,
the code is base on the book but i change a little bit, the example from

the book is only support text file, but i want to also support binary file.
actually, i am new to c#, and i haven't many programming experience
One thing which concerns me is that your OnReadComplete assumes that
whenever you've successfully read a piece of data, that it must contain
a complete filename. What happens if a long filename is sent which is
split over multiple packets, for instance


How can i handle this problem? I am sorry about my English is not very
good,and Tnank again
the origianl code is below

using System;
using System.Net.Sockets;
using System.Text;
using System.IO;
// get a file name from the client
// open the file and send the
// contents from the server to the client
public class AsynchNetworkFileServer
{
class ClientHandler
{
// constructor
public ClientHandler(
Socket socketForClient )
{
// initialize member variable
socket = socketForClient;
// initialize buffer to hold
// contents of file

buffer = new byte[256];
// create the network stream
networkStream =
new NetworkStream(socketForClient);
// set the file callback for reading
// the file
myFileCallBack =
new AsyncCallback(this.OnFileCompletedRead);
// set the callback for reading from the
// network stream
callbackRead =
new AsyncCallback(this.OnReadComplete);
// set the callback for writing to the
// network stream
callbackWrite =
new AsyncCallback(this.OnWriteComplete);
}
// begin reading the string from the client
public void StartRead( )
{
// read from the network
// get a filename
networkStream.BeginRead(
buffer, 0, buffer.Length,
callbackRead, null);
}
// when called back by the read, display the string
// and echo it back to the client
private void OnReadComplete( IAsyncResult ar )
{
int bytesRead = networkStream.EndRead(ar);
// if you got a string
if( bytesRead > 0 )
{
// turn the string to a file name
string fileName =
System.Text.Encoding.ASCII.GetString(
buffer, 0, bytesRead);
// update the console
Console.Write(
"Opening file {0}", fileName);
// open the file input stream
inputStream =
File.OpenRead(fileName);
// begin reading the file
inputStream.BeginRead(
buffer, // holds the results
0, // offset
buffer.Length, // BufferSize
myFileCallBack, // call back delegate
null); // local state object

}
else
{
Console.WriteLine( "Read connection dropped");
networkStream.Close( );
socket.Close( );
networkStream = null;
socket = null;
}
}
// when you have a buffer-full of the file
void OnFileCompletedRead(IAsyncResult asyncResult)
{
int bytesRead =
inputStream.EndRead(asyncResult);
// if you read some file
if (bytesRead > 0)
{
// write it out to the client
networkStream.BeginWrite(
buffer, 0, bytesRead, callbackWrite, null);
}
}
// after writing the string, get more of the file
private void OnWriteComplete( IAsyncResult ar )
{
networkStream.EndWrite(ar);
Console.WriteLine( "Write complete");
// begin reading more of the file
inputStream.BeginRead(
buffer, // holds the results
0, // offset
buffer.Length, // (BufferSize)
myFileCallBack, // call back delegate
null); // local state object
}
private const int BufferSize = 256;
private byte[] buffer;
private Socket socket;
private NetworkStream networkStream;
private Stream inputStream;
private AsyncCallback callbackRead;
private AsyncCallback callbackWrite;
private AsyncCallback myFileCallBack;
}
public static void Main( )
{
AsynchNetworkFileServer app =
new AsynchNetworkFileServer( );

app.Run( );
}
private void Run( )
{
// create a new TcpListener and start it up
// listening on port 65000
TcpListener tcpListener = new TcpListener(65000);
tcpListener.Start( );
// keep listening until you send the file
for (;;)
{
// if a client connects, accept the connection
// and return a new socket named socketForClient
// while tcpListener keeps listening
Socket socketForClient =
tcpListener.AcceptSocket( );
if (socketForClient.Connected)
{
Console.WriteLine("Client connected");
ClientHandler handler =
new ClientHandler(socketForClient);
handler.StartRead( );
}
}
}
}
Jan 26 '06 #3
Chi wrote:
the code is base on the book but i change a little bit, the example from
the book is only support text file, but i want to also support binary file.
actually, i am new to c#, and i haven't many programming experience


Right - in that case, I would back off from asynchronous IO for a
while. Work out your protocol, then write a synchronous version (using
Stream.Read rather than Stream.BeginRead) and then convert it to use
asynchronous IO if necessary.
One thing which concerns me is that your OnReadComplete assumes that
whenever you've successfully read a piece of data, that it must contain
a complete filename. What happens if a long filename is sent which is
split over multiple packets, for instance


How can i handle this problem? I am sorry about my English is not very
good,and Tnank again


You need to work out what your protocol is - what the client will send,
how you'll respond etc. Consider the fact that as you're really dealing
with streams of data, if the client sends over "SomeFilename" you might
see that all in one go, or as "So" followed by "meFile" followed by
"name".

Jon

Jan 26 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Sean | last post: by
5 posts views Thread by Raziq Shekha | last post: by
3 posts views Thread by MarcJessome | last post: by
1 post views Thread by FAQ server | last post: by
7 posts views Thread by Ross Culver | last post: by
11 posts views Thread by Adrian | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by MichaelMortimer | last post: by

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.