Beispiel #1
0
        public void SendData(EndPoint clientEndPoint, string data)
        {
            responseData = data;

            if (!ClientSockets.ContainsKey(clientEndPoint))
            {
                return;
            }

            if (data == "")
            {
                return;
            }


            //Socket socket = ClientSockets[clientEndPoint];
            TCPConnection client = ClientSockets[clientEndPoint];

            if (!client.Socket.Connected)
            {
                return;
            }


            //byte[] sendBufer = Encoding.ASCII.GetBytes(data);
            client.SendBufer = Encoding.ASCII.GetBytes(data);

            try
            {
                client.Socket.BeginSend(client.SendBufer, 0, client.SendBufer.Length,
                                        SocketFlags.None,
                                        new AsyncCallback(SendCallBack), client);
            }
            catch (Exception ex)
            {
                OnException?.Invoke(ex);
            }
        }
Beispiel #2
0
        private void AcceptCallBack(IAsyncResult asyncResult)
        {
            if (!IsRunning)
            {
                return;
            }

            TCPConnection client = new TCPConnection();

            client.Socket = serverSocket.EndAccept(asyncResult);

            if (!client.Socket.Connected)
            {
                return;
            }


            ClientSockets.Add(client.Socket.RemoteEndPoint, client);

            OnClientConnected?.Invoke(client.Socket.RemoteEndPoint);

            serverSocket.BeginAccept(new AsyncCallback(AcceptCallBack), client);
        }