示例#1
0
        private void _args_Completed(object sender, SocketAsyncEventArgs e)
        {
            sock = (Socket)sender;

            if (sock.Connected)
            {
                Data d = new Data(DataType.NONE, "", new ClientInfo(myIP, myNick));

                byte[] _data      = new byte[1024];
                byte[] serialized = d.Serialize();

                for (int i = 0; i < 1024; i++)
                {
                    _data[i] = 0;
                }
                for (int i = 0; i < serialized.Length; i++)
                {
                    _data[i] = serialized[i];
                }

                SocketAsyncEventArgs _receiveArgs = new SocketAsyncEventArgs();
                _receiveArgs.UserToken = d;
                _receiveArgs.SetBuffer(_data, 0, 1024);
                _receiveArgs.Completed += _receiveArgs_Completed;
                sock.ReceiveAsync(_receiveArgs);
            }
            else
            {
                sock = null;
            }
        }
示例#2
0
        void Send(Client cli, Data _d)
        {
            SocketAsyncEventArgs _args = new SocketAsyncEventArgs();

            byte[] _data      = new byte[1024];
            byte[] serialized = _d.Serialize();

            for (int i = 0; i < 1024; i++)
            {
                _data[i] = 0;
            }
            for (int i = 0; i < serialized.Length; i++)
            {
                _data[i] = serialized[i];
            }

            _args.SetBuffer(_data, 0, 1024); // 여기서 버그 생김 계쏙.. 첫번째 클라는 보내지는데 그담부턴 에러 작렬.
            _args.Completed += Args_Completed;
            _args.UserToken  = cli;
            cli.sock.SendAsync(_args);
        }
示例#3
0
        public void Send(Data data)
        {
            try
            {
                lock (MYObEJCT)
                {
                    /*
                     * byte[] byte_buffer = data.Serialize();
                     * sock.Send(byte_buffer, SocketFlags.None); //동기
                     */
                    /*
                     * byte[] _data = new byte[1024];
                     *
                     *
                     * for (int i = 0; i < 1024; i++)
                     *  _data[i] = 0;
                     * for (int i = 0; i < serialized.Length; i++)
                     *  _data[i] = serialized[i];
                     */

                    //byte[] serialized = data.Serialize(); //기존의 방식
                    System.Threading.Thread.Sleep(10);
                    byte[] serialized         = data.Serialize();
                    SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                    args.SetBuffer(serialized, 0, serialized.Length); // 기존의 방식
                                                                      //args.SetBuffer(_data, 0, 1024);
                    args.Completed += Args_Completed;
                    //args.UserToken = this;
                    if (myIP == null)
                    {
                        System.Threading.Thread.Sleep(100);
                    }
                    sock.SendAsync(args);
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }