In c# when simulating a lan-messenger I am using the loopback address just for testing the current code and am being able to receive the first message I am sending.However after that there aren't any messages reaching the socket.Is there anything to do with clearing a socket buffer?.please help This is the callback function when a connection is made:
private void accepted(IAsyncResult iar)
{
Socket server = (Socket)iar.AsyncState;
Socket client = server.EndAccept(iar);
if (client.Connected)
{
try
{
client.BeginReceive(receive, 0, receive.Length, System.Net.Sockets.SocketFlags.None, new AsyncCallback(rec), client);
}
catch (ArgumentException)
{
MessageBox.Show("arguments incorrect in begin-receive call", "Error", MessageBoxButtons.OK);
}
catch (SocketException)
{
MessageBox.Show("error in accessing socket while receiving", "Error", MessageBoxButtons.OK);
}
catch (ObjectDisposedException)
{
MessageBox.Show("socket closed while receiving", "Error", MessageBoxButtons.OK);
}
catch (Exception)
{
MessageBox.Show("error while receiving", "Error", MessageBoxButtons.OK);
}
}
This is the callback function when the begin-receive method is executed:
void rec(IAsyncResult ar)
{
StringBuilder receivedData;
//String oldvalue;
Socket remote = (Socket)ar.AsyncState;
int recv = remote.EndReceive(ar);
receivedData = new StringBuilder(Encoding.ASCII.GetString(receive, 0, recv));
//MessageBox.Show(receivedData.ToString(), "received", MessageBoxButtons.OK);
StringBuilder sb = new StringBuilder(this.textBox1.Text);
sb.AppendLine(receivedData.ToString());
if (textBox1.InvokeRequired)
{
this.Invoke((MethodInvoker)delegate { this.textBox1.Text = sb.ToString(); });
}
remote.Close(5);
return;
}
Does it have anything to do with a stringbuilder or string datatype for assigning the data received to a variable.