471,337 Members | 921 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Checking wether a socket is closed

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.Avail able,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;
}
}

Dec 1 '05 #1
2 3967
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" <nu************@hotmail.com> wrote in message
news:11********************@g43g2000cwa.googlegrou ps.com...
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.Avail able,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;
}
}

Dec 1 '05 #2
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 Springer wrote:
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" <nu************@hotmail.com> wrote in message
news:11********************@g43g2000cwa.googlegrou ps.com...
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.Avail able,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;
}
}


Dec 1 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Michael Kennedy [UB] | last post: by
2 posts views Thread by Markus Pitha | last post: by
reply views Thread by Wayne M J | last post: by
reply views Thread by Jonathan | last post: by
6 posts views Thread by Abubakar | last post: by
3 posts views Thread by Dirk Reske | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.