(10-30-2015, 09:54 PM)ExtremeDude2 Wrote: Uh oh, I'm doing a networking project in C#
then take a look at this on how i fixed it, incase you stumble upon the same issue
i had the issue when i dc'd the client in the middle of my protocol's handshake. only on the 2nd network action the server was like "wait a minute..."
Code:
static public int SendData(ref byte[] buffer, ref Socket socket)
{
return SendData(ref buffer, 0, ref socket);
}
static public int SendData(ref byte[] buffer, int size, ref Socket socket)
{
if (buffer == null || socket == null)
return -1;
int ret = 0;
if (CheckConnection(ref socket))
{
if (size == 0)
{
ret = socket.Send(buffer);
}
else
ret = socket.Send(buffer, size, 0);
}
else
ret = -2;
return ret;
}
static public bool CheckConnection(ref Socket socket)
{
if (socket == null)
return false;
try
{
//if the socket says connected we test it to check if its true or not. C# likes saying its connected while its not it seems...
//if it says not connected then its clearly not connected
if (socket.Connected)
{
//poll the connection. the poll will return true if :
// - a connection is pending
// - data is available for reading
// - connection is closed or unavailable
//
// so if it returns false, the connection is there but nothing can be read
// if its true, we need to check if we can read data. if we can't, its either pending (useless) or closed (even more useless)
// so we mark it as closed.
bool bpoll = false;
bpoll = socket.Poll(0, SelectMode.SelectRead);
if (bpoll)
{
byte[] buff = new byte[1];
int recv_value = socket.Receive(buff, SocketFlags.Peek);
if (recv_value <= 0)
{
// Client disconnected
return false;
}
}
}
else
return false;
}
catch (SocketException ex)
{
//something went wrong. im guessing connection is closed but meh. lets just assume connection is a no go
return false;
}
return true;
}


![[Image: PeachSig.jpg]](http://www.dacotaco.com/PeachSig.jpg)
![[Image: 566286.png]](http://valid.canardpc.com/cache/banner/566286.png)
![[Image: 2280403.png]](http://valid.canardpc.com/cache/banner/2280403.png)