I am struggling for some days with this problem. I have a server machine and I need to implement a client . Firstly I am sending a logon message and I am receiving the response. After , I will need to send and receive different messages. From 10 to 10 seconds I need to send a "heartbeat message" , so the server needs to know that the connexion is still active. I have made it a simple asynchronous client.
private static void Send(Socket client, String data)
{
byte[] byteData = charTobyte(data.ToCharArray());
client.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), client);
}
private static void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = client.EndSend(ar);
Console.WriteLine("Sent {0} bytes to server.", bytesSent);
sendDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace + " " + e.Message);
}
}
private static void Receive(Socket client)
{
try
{
// Create the state object.
StateObject state = new StateObject();
state.workSocket = client;
// Begin receiving the data from the remote device.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void ReceiveCallback(IAsyncResult ar)
{
try
{
// Retrieve the state object and the client socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;
// Read data from the remote device.
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0)
{
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
}
else
{
// All the data has arrived; put it in response.
if (state.sb.Length > 1)
{
response = state.sb.ToString();
Console.WriteLine("Response: " + response);
}
receiveDone.Set();
}
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace + " " + e.Message);
}
}
private static void Heartbeat(object source, System.Timers.ElapsedEventArgs e, Socket client)
{
try
{
Console.WriteLine("Heartbeat...sent");
SendHeart(client, DPLHeader(HeartBeat));
sendHeart.WaitOne();
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace + " " + ex.Message);
}
finally
{
Console.WriteLine("value: " + client.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive) + " : " + client.Connected);
}
}
This is what I am doing in the StartClient():
public static void StartClient()
{
// Connect to a remote device.
try
{
client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
// Connect to the remote endpoint.
client.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
timer.Interval = 10000;
timer.Elapsed += new System.Timers.ElapsedEventHandler((source, e) => Heartbeat(source, e, client));
timer.Enabled = false;
timer.AutoReset = false;
timer.Start();
message = DPLHeader(Messages.ElementAt(j));
Send(client, message);
sendDone.WaitOne();
Console.WriteLine("value: " + client.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive) + " : " + client.Connected);
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace + " " + e.Message);
}
}
The SendHeart() is just as a normal Send , just is sending the keepalive message.
The problem that I am facing is the next: After the timer is firstly consumed and it triggers the Heartbeat() the appplication is only sending just the keepallive message, so it doesn t send the others messages . If it's sending just the keepallive message the connection is maintaining alive. Like the client can t access anymore the Send callback, and it remains just for the Heartbeat.
In another case I have made 3 timers , one for Send(),one for Receive(), and one for Heartbeat(). In this order and when one timer is expired it stops and starts the next one and so on. I send a message , I read a response and send a Heartbeat , after that any move I try to do BeginSend a message or BeginReceive a message , the connection is aborted by the host machine.
For the second case , another problem would be that if after sending a message, and starts the Receive, if the Heartbeat is sent the receive thread is lost so I can t read the message anymore.
Sorry if it's too tangled. I am waiting for questions if you didn't understood something. Thanks