示例#1
0
        private static void RcvMsgHandler(IAsyncResult ar)
        {
            try
            {
                String content = String.Empty;
                RdrMsgFormat rcvmsg = new RdrMsgFormat();
                // Retrieve the state object and the handler socket
                // from the asynchronous state object.
                StateObject state = (StateObject)ar.AsyncState;
                Socket handler = state.workSocket;

                // Read data from the client socket.
                int bytesRead = handler.EndReceive(ar);

                if (bytesRead > 0)
                {
                    rcvmsg = rcvmsg.GetPacketObject(state.buffer);

                    switch (rcvmsg.msgid)
                    {
                        case message_id.ping_rply:

                            if (rcvmsg.key == 0x11223344)
                                MessageBox.Show("Received ping sucessfully");
                            // Signal that all bytes have been received.
                            receiveDone.Set();
                            break;
                        case message_id.testmsg:

                            break;
                        default:

                            break;
                    }
                    handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                        new AsyncCallback(RcvMsgHandler), state);
                }
            }
            catch (Exception e)
            {
                applog.logexcep("RcvMsgHandler", e.ToString());
            }
        }
示例#2
0
        void thd_receiveUdpPkts()
        {
            byte[] message = new byte[1024];
            int bytesRead;
            RdrMsgFormat rcvmsg = new RdrMsgFormat();
            //MessageBox.Show("In Receive thread");
            try
            {
                // Creates an IPEndPoint to capture the identity of the client when we'll use the Socket.ReceiveFrom Method
                IPEndPoint remote_endpoint = new IPEndPoint(IPAddress.Any, 0);   // IP and PORT pairing of the client

                EndPoint ep = (EndPoint)remote_endpoint;
                while (receive)
                {
                    bytesRead=listening_socket.ReceiveFrom(message, ref ep);
                    //Check for the ip address
                    if (bytesRead>0)
                    {
                        //MessageBox.Show(string.Format("No of bytes read: {0}",bytesRead));
                        rcvmsg = rcvmsg.GetPacketObject(message);

                        //Deserilise the received message
                        if (tomain_msgQueue != null)
                        {
                            //MessageBox.Show("Putting data in Queue");
                            tomain_msgQueue.Enqueue(rcvmsg);
                            QueueEvnt.Set();
                        }

                    }

                    //Send to a queue that would be read by
                }
                MessageBox.Show("Exiting Udp Receive thread");
                QueueEvnt.Set();
            }
            catch (Exception ex)
            {
                applog.logexcep("thd_receiveUdpPkts", ex.Message);
            }
        }
        private static void RcvMsgHandler(IAsyncResult ar)
        {
            try{

            String content = String.Empty;
            RdrMsgFormat rcvmsg = new RdrMsgFormat();
            // Retrieve the state object and the handler socket
            // from the asynchronous state object.
            StateObject state = (StateObject)ar.AsyncState;
            Socket handler = state.workSocket;

            // Read data from the client socket.
            int bytesRead = handler.EndReceive(ar);

            if (bytesRead > 0)
            {
                //MessageBox.Show(string.Format("No of bytes read: {0}",bytesRead));
                rcvmsg = rcvmsg.GetPacketObject(state.buffer);

                switch(rcvmsg.msgid)
                {
                    case message_id.ping:
                        RdrMsgFormat sndmsg = new RdrMsgFormat();
                        sndmsg.msgid = message_id.ping_rply;
                        sndmsg.key = rcvmsg.key;
                        //Send the same ping value to client
                        SendMsgtoClient(handler,sndmsg);
                        break;
                    case message_id.id:

                        break;
                    case message_id.testmsg:

                        break;
                    default:

                        break;
                }

                handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                    new AsyncCallback(RcvMsgHandler), state);
            }

            }
            catch (Exception e)
            {
            applog.loggen(e.ToString());
            }
        }