Ejemplo n.º 1
0
            private void ListenCallback(IAsyncResult ar)
            {
                TcpClient   client;
                TcpListener listener = (TcpListener)ar.AsyncState;

                lock (locker)
                {
                    try {
                        client = listener.EndAcceptTcpClient(ar);
                    }catch (ObjectDisposedException e)
                    {
                        ListenStoper.Set();
                        return;
                    }catch (SocketException e)
                    {
                        ListenStoper.Set();
                        return;
                    }
                }

                Console.WriteLine("接続を確認");

                IPEndPoint endPoint = (IPEndPoint)client.Client.RemoteEndPoint;
                string     key      = endPoint.Address.ToString() + endPoint.Port.ToString();

                ReceiveProc proc = new ReceiveProc(ReadTimeOut, WriteTimeOut, client, 1024);

                Clients.TryAdd(key, proc);
                proc.ReceiveDataCallback = DataReceiveCallback;
                proc.ReceiveStart();

                Listener.BeginAcceptTcpClient(ListenCallback, listener);
            }
Ejemplo n.º 2
0
            public TCPCilent(string ip, int port, int buffer, _ReceiveData ReceiveCallback)
            {
                TcpClient client;

                try
                {
                    client = new TcpClient(ip, port);
                }catch (SocketException e)
                {
                    Console.WriteLine("接続に失敗しました");
                    Console.WriteLine("ErrorCode : " + e.ErrorCode);
                    return;
                }
                Console.WriteLine("接続完了");
                RecvProc = new ReceiveProc(ReadTimeOut, WriteTimeOut, client, 1024);
                RecvProc.ReceiveDataCallback = ReceiveCallback;
            }