Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have develop socket server and receiving response from client asynchronously. If first user connects and sending one message and send again one message server get receive both messages and when second user logged in and send message can receive the message.

Second user sends message again it can also receive. But,after that when first client sends message I am getting blank byte array first time but if first user send it again then I can receive.

Can you please help why its happen and my client are iPhone client.

I am sending my On Receive code
C#
private void OnReceive(IAsyncResult ar)
        {
            try
            {
                Socket clientSocket = (Socket)ar.AsyncState;
                if (!(clientSocket.Poll(100, SelectMode.SelectRead) && clientSocket.Available == 0))
                {
               clientSocket.EndReceive(ar);

            string msgToSend string.empty;
                txtMessageLog.Text +=    Encoding.UTF8.GetString(data);; + Environment.NewLine;
             
                Data msgToSend = new Data();

                byte[] message;

               switch (  Encoding.UTF8.GetString(data);)
                {
                    case "Login":

                       msgToSend="Logged in successfully"

                        message = msgToSend.ToByte();
                        //Send the name of the users in the  room
                        clientSocket.BeginSend(message, 0, message.Length, SocketFlags.None,
                                new AsyncCallback(OnSend), clientSocket);
                        break;
                    case "Logout":

                       msgToSend="Logged out successfully"

                        message = msgToSend.ToByte();
                        //Send the name of the users in the  room
                        clientSocket.BeginSend(message, 0, message.Length, SocketFlags.None,
                                new AsyncCallback(OnSend), clientSocket);
                        break;
                   
                }

           
                    //Receive other message from client.
                                   _byteData = new byte[1024];
                    clientSocket.BeginReceive(_byteData, 0, _byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), clientSocket);
              
               
                }
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Server", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Posted
Updated 13-Dec-13 6:48am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900