Пример #1
0
        /// <summary>
        /// ACTIVE 로 동작
        /// </summary>
        /// <param name="IP">server IP</param>
        /// <param name="Port">server Port</param>
        /// <returns></returns>
        public bool Conn(string IP, int Port)
        {
            try
            {
                cliSocket = new TcpClient();
                cliSocket.Connect(IP, Port);

                clsCommon.Instance.SERVER_PORT = Port;

                clsClientService Connclient = new clsClientService();
                Connclient.OnDisconnected += new DisconnectedDelegate(client_OnDisconnected);
                Connclient.OnReceiveData  += new ReceiveDataDelegate(client_OnReceiveData);

                Connclient.StartService(cliSocket, IP, Port);

                string KEY = IP + ":" + Port;

                clients.Add(KEY, Connclient);

                OnSystemMsg(" CONNECTED (ACTIVE: " + IP + "/" + Port + ")");
                return(true);
            }
            catch (Exception ex)
            {
                OnSystemMsg(" CONNECT ERROR (ACTIVE: " + IP + "/" + Port + ") " + ex.ToString());

                return(false);
            }
        }
Пример #2
0
        /// <summary>
        /// Client 접속 대기
        /// </summary>
        /// <param name="ar"></param>
        protected void AcceptClient(IAsyncResult ar)
        {
            if (tcpListen != null)
            {
                TcpClient cliSocket = new TcpClient();

                try
                {
                    if (tcpListen == null)
                    {
                        return;
                    }
                    if (!tcpListen.Server.IsBound)
                    {
                        return;
                    }

                    cliSocket = tcpListen.EndAcceptTcpClient(ar);



                    clsClientService client = new clsClientService();
                    client.OnDisconnected += new DisconnectedDelegate(client_OnDisconnected);
                    client.OnReceiveData  += new ReceiveDataDelegate(client_OnReceiveData);


                    //IP 추출 후 해당 클라이언트의 키값으로 사용.
                    string   temp  = cliSocket.Client.RemoteEndPoint.ToString();
                    string[] tempA = temp.Split(':');
                    string   IP    = tempA[0];

                    client.StartService(cliSocket, IP, ServerPort);

                    bool DFlag = false;

                    string KEY = IP + ":" + ServerPort;

                    foreach (string cs in clients.Keys)
                    {
                        if (cs == KEY)
                        {
                            DFlag = true;
                        }
                    }

                    if (DFlag)
                    {
                        clients[KEY].Close();
                        clients.Remove(KEY);
                    }

                    clients.Add(KEY, client);

                    OnSystemMsg("CONNECTED (IP:" + IP + ")");
                    OnConn(IP, ServerPort);

                    tcpListen.BeginAcceptSocket(AcceptClient, null);
                }
                catch (Exception ex)
                {
                    OnErrorMsg("AcceptClient CONNECTED ERROR : " + ex.ToString());
                    if (cliSocket != null)
                    {
                        cliSocket.Close();
                    }
                    try
                    {
                        tcpListen.BeginAcceptSocket(AcceptClient, null);
                    }
                    catch
                    {
                        return;
                    }
                    return;
                }
            }
        }