Exemplo n.º 1
0
        protected virtual void Received(SocketReceiveEventArgs e)
        {
            SocketReceiveEventHandler handler = OnReceive;

            if (handler != null)
            {
                handler(this, e);
            }
        }
Exemplo n.º 2
0
 void socketManager_OnReceiveMsg(object sender, SocketReceiveEventArgs e)
 {
     try
     {
     }
     catch (Exception ex)
     {
         AddLog(Log.Type.LOG_ERR, "socketManager_OnReceiveMsg Error : {0}", ex.Message);
     }
 }
Exemplo n.º 3
0
        private void OnClientReceive(object sender, SocketReceiveEventArgs e)
        {
            ReceiveProcess(e.ReceiveData, e.ReceiveBytes);

            ReceiveEventHandler handler = OnReceiveMsg;

            if (handler != null)
            {
                handler(this, e);
            }
        }
Exemplo n.º 4
0
        void Server_OnReceive(object sender, SocketReceiveEventArgs e)
        {
            AddLog(Log.Type.LOG_NOR, "Server Receive Id:{0}, Length:{1}", e.ID, e.ReceiveBytes);
            ReceiveProcess(e.ReceiveData, e.ReceiveBytes);

            ReceiveEventHandler handler = OnReceiveMsg;

            if (handler != null)
            {
                handler(this, e);
            }
        }
Exemplo n.º 5
0
        void SerialReceived(object sender, Serial_Port.SerialDataEventArgs e)
        {
            try
            {
                ReceiveProcess(e.Data, e.Data.Length);

                SocketReceiveEventArgs rev     = new SocketReceiveEventArgs(0, e.Data.Length, e.Data);
                ReceiveEventHandler    handler = OnReceiveMsg;

                if (handler != null)
                {
                    handler(this, rev);
                }
            }
            catch (Exception err)
            {
                AddLog(Log.Type.LOG_ERR, "SerialRecieved Error:{0}", err.Message);
            }
        }
Exemplo n.º 6
0
        private void ClientThread(object sender)
        {
            TcpClient   client = sender as TcpClient;
            StateObject so     = new StateObject(client);
            int         i;

            try
            {
                while (ThreadRun && (i = nStream.Read(so.Buffer, 0, so.BufferSize)) != 0)
                {
                    SocketReceiveEventArgs rev = new SocketReceiveEventArgs(id, i, so.Buffer);
                    Received(rev);

//                    string data = System.Text.Encoding.UTF8.GetString(so.Buffer, 0, i);
//                    Debug.WriteLine("Client Read:" + data);
                }
            }
            catch (SocketException se)
            {
                SocketErrorEventArgs eev = new SocketErrorEventArgs(this.id, se);
                ErrorOccured(eev);
            }
            catch (Exception err)
            {
                SocketErrorEventArgs eev = new SocketErrorEventArgs(this.id, err);
                ErrorOccured(eev);
            }
            finally
            {
                if (nStream != null)
                {
                    nStream.Close();
                    nStream = null;
                }
                if (client != null)
                {
                    client.Close();
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// 외부 접속 처리 쓰레드
        /// </summary>
        /// <param name="sender"></param>
        private void clientThread(object sender)
        {
            /*
             *          string Name = string.Empty;
             *          string Subject = string.Empty;
             *          Int32 Grade = 0;
             *          string Memo = string.Empty;
             *          TimeSpan time = TimeSpan.Zero;
             */
            // 1. 데이타 받기
            TcpClient     client = sender as TcpClient;
            NetworkStream stream = client.GetStream();

            //            byte[] buffer = new byte[8092];
            StateObject so = new StateObject(client);
            //string data = null;
            int i;

            try
            {
                while (ThreadRun && (i = stream.Read(so.Buffer, 0, so.BufferSize)) != 0)
                {
                    // 수신 이벤트 처리
                    SocketReceiveEventArgs rev = new SocketReceiveEventArgs(id, i, so.Buffer);
                    Received(rev);


/*                  // 수신 확인 테스트용
 *                  data = System.Text.Encoding.UTF8.GetString(so.Buffer, 0, i);
 *                  updateStatusInfo("수신:" + data);
 *                  // Process the data sent by the client.
 *                  data = data.ToUpper();
 *
 *                  byte[] msg = System.Text.Encoding.UTF8.GetBytes(data);
 *
 *                  // Send back a response.
 *                  stream.Write(msg, 0, msg.Length);
 */
                }

                /*      // 패킷에 의한 전송
                 *          if (client.GetStream().CanRead)
                 *          {
                 *              IFormatter formatter = new BinaryFormatter();
                 *
                 *              DataPacket packet = new DataPacket();
                 *              packet = (DataPacket)formatter.Deserialize(stream);
                 *
                 *              Name = packet.Name;
                 *              Subject = packet.Subject;
                 *              Grade = packet.Grade;
                 *              Memo = packet.Memo;
                 *              SendTime = packet.SendTime;
                 *
                 *              time = DateTime.Now - SendTime;
                 *
                 *              updateStatusInfo(string.Format("[{0}] data received", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")));
                 *          }
                 */
            }
            catch (SocketException se)
            {
                SocketErrorEventArgs eev = new SocketErrorEventArgs(this.id, se);
                ErrorOccured(eev);
                //                Debug.WriteLine("ServerSocket->clientThread SocketException" + se.Message);
            }
            catch (Exception err)
            {
                SocketErrorEventArgs eev = new SocketErrorEventArgs(this.id, err);
                ErrorOccured(eev);
                //                Debug.WriteLine("ServerSocket->clientThread Exception" + err.Message);
            }
            finally
            {
                try
                {
                    if (stream != null)
                    {
                        stream.Close();
                        stream = null;
                    }
                    if (client != null)
                    {
                        // Client 접속 IP 와 고유번호를 가저온다.
                        string remoteip = client.Client.RemoteEndPoint.ToString();
                        // 해당 IP가 등록 되어 있는지 확인
                        if (RemoteClients.ContainsKey(remoteip))
                        {
                            RemoteClients.Remove(remoteip);
                        }
                        client.Close();
                        client = null;
                    }
                }
                catch (Exception err)
                {
                    SocketErrorEventArgs eev = new SocketErrorEventArgs(this.id, err);
                    ErrorOccured(eev);
                    //                    Debug.WriteLine("error:" + err.Message);
                }
            }
            updateStatusInfo(string.Format("[{0}] disconnted", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")));

            // 2. 데이타 표시하기

            /*
             *          Invoke((MethodInvoker)delegate
             *          {
             *              int count = listView1.Items.Count;
             *              count++;
             *
             *              ListViewItem i = new ListViewItem();
             *              i.Text = count.ToString();
             *              i.SubItems.Add(Name);
             *              i.SubItems.Add(Subject);
             *              i.SubItems.Add(Grade.ToString());
             *              i.SubItems.Add(Memo);
             *              i.SubItems.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
             *              i.SubItems.Add(string.Format("{0}.{1} seconds", time.Seconds.ToString(), time.Milliseconds.ToString()));
             *              listView1.Items.Add(i);
             *
             *              listView1.Items[this.listView1.Items.Count - 1].EnsureVisible();
             *          });
             */
        }