Checking wether a socket is closed

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;
}
}
 
C

Chris Springer

I believe that you have to call the socket.Connect method on your socket
before sending the data with both a connection-oriented and connectionless
protocol. When you call connect you have to send it a variable of type
IPEndPoint according to the Microsoft Online Documentation. Here is the
sample code they send with MSVS2003

[C#]
IPHostEntry lipa = Dns.Resolve("host.contoso.com");
IPEndPoint lep = new IPEndPoint(lipa.AddressList[0], 11000);

Socket s = new Socket(lep.Address.AddressFamily,
SocketType.Stream,
ProtocolType.Tcp);
try{
s.Connect(lep);
}
catch (Exception e){
Console.WriteLine("Exception Thrown: " + e.ToString());
}

byte[] msg = Encoding.ASCII.GetBytes("This is a test");

// Blocks until send returns.
int i = s.Send(msg);

// Blocks until read returns.
byte[] bytes = new byte[1024];
s.Receive(bytes);

//Displays to the screen.
Console.WriteLine(Encoding.ASCII.GetString(bytes));
s.Shutdown(SocketShutdown.Both);
s.Close();

hope this helps,

Chris

--
Securing your systems is much like fighting off disease -- as long as you
maintain basic hygiene, you're likely to be okay, but you'll never be
invulnerable.

Steve Shah - Unix Systems Network Administrator
 
N

Nuno Magalhaes

That didn't help. My socket receive and send functions don't block and
what I asked was if there is a method to check if the connection is
active. Socket.Connection is not a method, but a property and is
updated only with the last Socket.Send operation that throws an
exception if the connection was closed. What I asked was a solution for
the synchronization of my code because I was losing the last chunk
sometimes.

while(true)
{
try
{
Socket.Receive
Socket.Send
}
catch(Exception)
{
break;
}
}

If the data arrives between socket.Receive and socket.Send I can't get
the last piece of data.
Thanks for your reply, anyway. That was I asked.

Nuno Magalhaes.

P.S.: It will always give an exception because the server will close
the connection at the end of the data transmission.

Chris said:
I believe that you have to call the socket.Connect method on your socket
before sending the data with both a connection-oriented and connectionless
protocol. When you call connect you have to send it a variable of type
IPEndPoint according to the Microsoft Online Documentation. Here is the
sample code they send with MSVS2003

[C#]
IPHostEntry lipa = Dns.Resolve("host.contoso.com");
IPEndPoint lep = new IPEndPoint(lipa.AddressList[0], 11000);

Socket s = new Socket(lep.Address.AddressFamily,
SocketType.Stream,
ProtocolType.Tcp);
try{
s.Connect(lep);
}
catch (Exception e){
Console.WriteLine("Exception Thrown: " + e.ToString());
}

byte[] msg = Encoding.ASCII.GetBytes("This is a test");

// Blocks until send returns.
int i = s.Send(msg);

// Blocks until read returns.
byte[] bytes = new byte[1024];
s.Receive(bytes);

//Displays to the screen.
Console.WriteLine(Encoding.ASCII.GetString(bytes));
s.Shutdown(SocketShutdown.Both);
s.Close();

hope this helps,

Chris

--
Securing your systems is much like fighting off disease -- as long as you
maintain basic hygiene, you're likely to be okay, but you'll never be
invulnerable.

Steve Shah - Unix Systems Network Administrator
Nuno Magalhaes said:
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;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top