Exemple #1
0
    // This handles a receive event on a particular client socket that has
    // connected. We read data from them and collect it into a string builder
    // for use later.
    public void ReadCallback(IAsyncResult ar)
    {
        try
        {
            // From the state of the event, get our the state object that wraps all
            // of the information for this client, and then get the socket out of
            // it.
            BuildClient client = (BuildClient)ar.AsyncState;
            Socket      socket = client.socket;

            // Perform the actual receive now; the result is the number of bytes
            // read, which can conceivably be 0; we only need to worry about doing
            // something if we actually got some data.
            int bytesRead = socket.EndReceive(ar);
            if (bytesRead == 0)
            {
                Console.WriteLine("Client closed connection");
                return;
            }

            // Console.WriteLine("==> Read {0} bytes", bytesRead);

            int bytesUsed = 0;
            while (bytesUsed != bytesRead)
            {
                // Give some bytes to the current partial message so it can
                // reconstruct itself.
                bytesUsed += inMsg.GiveBytes(client.readBuffer, bytesRead, bytesUsed);

                // If this message is complete, then echo it back to the other end
                // and get ready for another received message.
                if (inMsg.IsComplete())
                {
                    client.Dispatch(inMsg);
                    inMsg = new PartialMessage();
                }
            }

            // End by getting ready to read more data.
            client.BeginReading();
        }

        catch (SocketException se)
        {
            Console.WriteLine("Socket Error: {0}", se.Message);
            Console.WriteLine("Closing connection");
        }

        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }