N
Nuno Magalhaes
I've got a simple problem I guess. How do I know when a connection is
terminated without losing any data?
I do something like the code below, but sometimes between
socket.Receive and socket.Send I get the last chunk of data and am not
able to retrieve it anymore cause the socket will be dead.
Loop:
{
socket.Receive
<----------- data arrives
socket.Send(testData)
<----------- exception, socket closed, out of loop (can't receive
anymore because it throws an exception)
(shall I put a thread delay here to assure that the Receive method
receives the last chunk)
}
Here is the relevant code:
***************************************
while(true)
{
try
{
int
j=socket.Receive(bytes,receivedOffset,socket.Available,SocketFlags.None);
receivedOffset+=j;
}
catch(Exception)
{
}
//Check if the connection is closed
//and there is no more data to get
try
{
byte[] buffer=Encoding.UTF8.GetBytes("\r\n");
socket.Send(buffer);
}
catch(Exception)
{
//MessageBox.Show(socket.Connected.ToString());
break;
}
}
terminated without losing any data?
I do something like the code below, but sometimes between
socket.Receive and socket.Send I get the last chunk of data and am not
able to retrieve it anymore cause the socket will be dead.
Loop:
{
socket.Receive
<----------- data arrives
socket.Send(testData)
<----------- exception, socket closed, out of loop (can't receive
anymore because it throws an exception)
(shall I put a thread delay here to assure that the Receive method
receives the last chunk)
}
Here is the relevant code:
***************************************
while(true)
{
try
{
int
j=socket.Receive(bytes,receivedOffset,socket.Available,SocketFlags.None);
receivedOffset+=j;
}
catch(Exception)
{
}
//Check if the connection is closed
//and there is no more data to get
try
{
byte[] buffer=Encoding.UTF8.GetBytes("\r\n");
socket.Send(buffer);
}
catch(Exception)
{
//MessageBox.Show(socket.Connected.ToString());
break;
}
}