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
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();
clientSocket.BeginSend(message, 0, message.Length, SocketFlags.None,
new AsyncCallback(OnSend), clientSocket);
break;
case "Logout":
msgToSend="Logged out successfully"
message = msgToSend.ToByte();
clientSocket.BeginSend(message, 0, message.Length, SocketFlags.None,
new AsyncCallback(OnSend), clientSocket);
break;
}
_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);
}
}