Example #1
0
        public void StartServer(delCommunicationAction _serverAction)
        {
            this.serverAction = _serverAction;
            if (listener == null)
            {
                IPHostEntry host;
                IPAddress interNetworkIp = null;
                host = Dns.GetHostEntry(Dns.GetHostName());
                foreach (IPAddress ip in host.AddressList)
                {
                    if (ip.AddressFamily == AddressFamily.InterNetwork)
                    {
                        interNetworkIp = ip;
                    }
                }
                IPEndPoint endpoint = new IPEndPoint(interNetworkIp, port);
                listener = new TcpListener(endpoint);

            }
            started = true;
                listener.Start();
                for (int i = 0; i < maxConnection; i++)
                {
                    Thread t = new Thread(new ThreadStart(Service));
                    clientThreadList.Add(t);
                    t.Start();
                }
        }
Example #2
0
        public void ClientActions(string serverIp, delCommunicationAction clientAction)
        {
            //if (tcpClient == null||tcpClient.Connected==false)
            //{
              TcpClient  tcpClient = new TcpClient(serverIp, port);
            //}

            try
            {
                Stream s = tcpClient.GetStream();
                StreamReader sr = new StreamReader(s);
                StreamWriter sw = new StreamWriter(s);
                sw.AutoFlush = true;
                clientAction.Invoke(sr, sw,tcpClient.Client.LocalEndPoint);
                s.Close();

            }
            catch (Exception exx)
            {
                ErrMsg = exx.Message;
            }
            finally
            {
                tcpClient.Close();
            }
        }