Can someone help me



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



Relevant Pages

  • Re: Can someone help me
    ... // 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; // create the network stream networkStream = new NetworkStream; // set the file callback for reading ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Socket switch delay
    ... both at the client and at the server (and why ... would you set the send buffer size to zero on a non-overlapped ... One glaring error is your client does ... So when you use a single socket, ...
    (microsoft.public.win32.programmer.networks)
  • Re: Problem with socket communication for client (On handheld) / server (on PC) application
    ... The client sends commands to the server, ... to send a command to the server. ... private System.Windows.Forms.Label lblReceivedData; ... private Socket socketForClient; ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Lost data on socket - Can we start over politely?
    ... >>you are draining them too slow, but the client is sending data anyway. ... Do I need more than SOMAXCONN simultaneous connections? ... > can_write will actually get the socket equivalent of flow control from the ... It means that your client side send buffer has free space to ...
    (comp.lang.perl.misc)
  • Possible bug in .Net 2.0 udp sockets?
    ... server sends the requested data to the client. ... each time with a separate buffer. ... "EndReceiveFrom" to get the data and the endpoint, ... One socket listening for incoming udp-packets, ...
    (microsoft.public.dotnet.framework)

Loading