Ejemplo n.º 1
0
 //Sunxiang Test(new1)
 //HuJie Test(new1)
 private static void Server_onPackageRecv(object sender, RecvPackageEventArgs package)
 {
     byte[] data = package.Package as byte[];
     Console.WriteLine("Remote IP: " + package.AcceptSocket.RemoteEndPoint + "  data=" + data[0]);
 }
        private void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                SocketStateObject state = (SocketStateObject)ar.AsyncState;

                Socket handler = state.workSocket;
                int len = handler.EndReceive(ar);

                if (len > 0)
                {
                    byte[] temp = new byte[len];
                    Array.Copy(state.buffer, temp, len);

                    if (onPackageRecv != null)
                    {
                        RecvPackageEventArgs rp = new RecvPackageEventArgs();
                        rp.Package = temp;
                        rp.AcceptSocket = handler;

                        onPackageRecv(this, rp);
                    }

                    handler.BeginReceive(state.buffer, 0, state.buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), state);
                }
            }
            catch (Exception ex)
            {

            }
        }