All pastes #746322 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-10-23 04:09 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace Server
{
    class Program
    {
        private static AsyncCallback clientConnectCallback = new AsyncCallback(onClientConnect);
        private static AsyncCallback clientBeginReadCallback = new AsyncCallback(beginClientRead);

        static void Main()
        {
            TcpListener listener = new TcpListener(IPAddress.Any, 8000);

            listener.Start();

            listener.BeginAcceptTcpClient(clientConnectCallback, listener);
        }

        private static void onClientConnect(IAsyncResult asyn)
        {
            TcpListener listener = (TcpListener)asyn.AsyncState;

            TcpClient client = listener.EndAcceptTcpClient(asyn);

            ClientState state = new ClientState(client);

            NetworkStream stream = client.GetStream();

            stream.BeginRead(state.Buffer, state.LastWriteIndex, client.Available, clientBeginReadCallback, state);
        }

        private static void beginClientRead(IAsyncResult asyn)
        {
            ClientState state = (ClientState)asyn;

            NetworkStream stream = state.Client.GetStream();

            int dataRead = stream.EndRead(asyn);
        }
    }
}