Exemplo n.º 1
0
        public void ReceiveAsyncCallback(IAsyncResult ar)
        {
            // 클라이언트에서 보내는 데이터 받기 위한 함수
            Socket clientsocket = (Socket)ar.AsyncState;

            clientsocket.EndReceive(ar);


            //// 데이터 방식 01
            //// 클라이언트에서 날아온 데이터 파싱하기
            //string userstring = Encoding.UTF8.GetString(m_ResiveBuffer);
            //string temstr = string.Format("{0}\r\n", userstring);
            //textBox1.AppendText(temstr);


            // 데이터 방식 02
            //int useridsize = BitConverter.ToInt32(m_ResiveBuffer, 0); // userid 사이즈
            //int msgsize = BitConverter.ToInt32(m_ResiveBuffer, 4);

            //string userstr = Encoding.UTF8.GetString(m_ResiveBuffer, 8, useridsize);
            //string msgstr = Encoding.UTF8.GetString(m_ResiveBuffer, 8 + useridsize, msgsize);

            //string temstr = string.Format("{0} : {1}\r\n", userstr, msgstr);
            //textBox1.AppendText(temstr);


            // 데이터 방식 03
            SocketDataCls resivedata = SocketDataCls.GetDeserialize(m_ResiveBuffer);

            textBox1.AppendText(resivedata.ToChatMsg() + "\r\n");



            //SocketDataCls datacls = new SocketDataCls();
            //datacls.Name = userstr;
            //datacls.Message = msgstr;



            m_ResiveBuffer = new byte[1024];
            // 클라이언트 데이터 연결 대기
            clientsocket.BeginReceive(m_ResiveBuffer, 0, m_ResiveBuffer.Length, SocketFlags.None
                                      , new AsyncCallback(ReceiveAsyncCallback)
                                      , clientsocket);
        }
Exemplo n.º 2
0
        // 역직렬화 로드
        public static SocketDataCls GetDeserialize(byte[] p_data)
        {
            //MemoryStream memStream = new MemoryStream();
            //BinaryFormatter binForm = new BinaryFormatter();
            //memStream.Write(p_val, 0, p_val.Length);
            //memStream.Seek(0, SeekOrigin.Begin);
            //LocalTankData outdata = (LocalTankData)binForm.Deserialize(memStream);

            //MemoryStream memstream = new MemoryStream(p_data);
            MemoryStream memstream = new MemoryStream();

            memstream.Write(p_data, 0, p_data.Length);
            memstream.Seek(0, SeekOrigin.Begin);
            IFormatter binform = new BinaryFormatter();

            SocketDataCls cls = binform.Deserialize(memstream) as SocketDataCls;


            return(cls);
        }