Socket Disconnect

M

MDB

Hello All, I am using the below code to create a socket connection via
wi-fi to another device. The problem is that if the device I am connecting
to losing power and reboots I lose the socket connection however, my device
dosn't know it was dropped and dosn't receive any type of error when it
sends messages. From what I have read, CF dosn't support SetSocketOption
so my question is does anyone know of a good way to work around this?


****SocketConnect****
g_socClient = new Socket (AddressFamily.InterNetwork,SocketType.Stream
,ProtocolType.Tcp );
IPAddress ip = IPAddress.Parse (strRemoteIPBase);
IPEndPoint ipEnd = new IPEndPoint (ip,intTCPListenPort);
g_socClient.Connect ( ipEnd );

WaitForData();



****WaitForData ****

if ( pfnCallBack == null )
{
pfnCallBack = new AsyncCallback (OnDataReceived);
}
CSocketPacket theSocPkt = new CSocketPacket ();
theSocPkt.thisSocket = PMMobile.Classes.Globals.g_socClient;

m_asynResult = g_socClient.BeginReceive (theSocPkt.dataBuffer
,0,theSocPkt.dataBuffer.Length ,SocketFlags.None,pfnCallBack,theSocPkt);
 
P

Paul G. Tobey [eMVP]

To notice that a connection has been broken, you either have to try to send
something over the socket or you have to tell the network stack to do it for
you. That is, if you are periodically sending packets, you'll find out
roughly two minutes after you send the first packet after the other device
dies that the connection is broken. In that case, the stack will detect
that there has been no acknowledgement. It will retry a number of times,
but, when all of those attempts fail, it will close the socket and you'll
get an error/exception.

If you are sitting around waiting to receive packets, though, you'll never
realize that the other end of the socket is gone, unless you tell the stack
to use keep-alive packets. When you turn that on, the network stack, when
it detects a long period of inactivity, will periodically send a packet to
the other end of the connection. If it replies, the stack goes back to
sleep. If it fails to reply, it will retry, in a certain pattern and, if
there is still no reply, will close the socket.

You have to tell your sockets to use keep-alives, using the SetSocketOption
code KeepAlive.

Note that, even if keep-alives are on, the default idle time before sending
them is 2 hours...

Paul T.
 
M

MDB

First thanks for your reply however, how do you set the SetSocketOption
using the compactframework? I have tried several different ways only to
receive errors when the code is ran (not when compiled).
 
P

Paul G. Tobey [eMVP]

g_socClient.SetSocketOption( Socket, KeepAlive, 1 ), is what I'd try. It
would help if you'd say what you did, when things don't work, as well as
telling us what error occurred.

Paul T.
 
M

MDB

Yes I know, I realize I forgot to post my error. Any way, this is what I
was doing:

g_socClient.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.SendTi
meout, 5000);

This is the error I was getting when this bit of code was excuted:
System.SystemException {"Could not find resource assembly"}

I just noticed that I was doing a SendTimeout instead of KeepAlive and
switched my code to use KeepAlive and I no longer get the error.

This is what it looks like now:
g_socClient = new Socket
(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp );
g_socClient.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.KeepAl
ive, 1 );
IPAddress ip = IPAddress.Parse (strRemoteIPBase);
IPEndPoint ipEnd = new IPEndPoint (ip,intTCPListenPort);
g_socClient.Connect ( ipEnd );


Although, I no longer get an error while excuting the program, it dosn't
raise any type of error when the socket becomes disconected.
 
P

Paul G. Tobey [eMVP]

Did you read my first message? Please reread *all* of it, especially the
last sentence.

Paul T.

MDB said:
Yes I know, I realize I forgot to post my error. Any way, this is what I
was doing:

g_socClient.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.SendTi
meout, 5000);

This is the error I was getting when this bit of code was excuted:
System.SystemException {"Could not find resource assembly"}

I just noticed that I was doing a SendTimeout instead of KeepAlive and
switched my code to use KeepAlive and I no longer get the error.

This is what it looks like now:
g_socClient = new Socket
(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp );
g_socClient.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.KeepAl
ive, 1 );
IPAddress ip = IPAddress.Parse (strRemoteIPBase);
IPEndPoint ipEnd = new IPEndPoint (ip,intTCPListenPort);
g_socClient.Connect ( ipEnd );


Although, I no longer get an error while excuting the program, it dosn't
raise any type of error when the socket becomes disconected.


Paul G. Tobey said:
g_socClient.SetSocketOption( Socket, KeepAlive, 1 ), is what I'd try. It
would help if you'd say what you did, when things don't work, as well as
telling us what error occurred.

Paul T.

MDB said:
First thanks for your reply however, how do you set the
SetSocketOption
using the compactframework? I have tried several different ways only
to
receive errors when the code is ran (not when compiled).



"Paul G. Tobey [eMVP]" <ptobey no spam AT no instrument no spam DOT
com>
wrote in message To notice that a connection has been broken, you either have to try to
send
something over the socket or you have to tell the network stack to do it
for
you. That is, if you are periodically sending packets, you'll find
out
roughly two minutes after you send the first packet after the other
device
dies that the connection is broken. In that case, the stack will detect
that there has been no acknowledgement. It will retry a number of times,
but, when all of those attempts fail, it will close the socket and you'll
get an error/exception.

If you are sitting around waiting to receive packets, though, you'll
never
realize that the other end of the socket is gone, unless you tell the
stack
to use keep-alive packets. When you turn that on, the network stack,
when
it detects a long period of inactivity, will periodically send a
packet
to
the other end of the connection. If it replies, the stack goes back
to
sleep. If it fails to reply, it will retry, in a certain pattern and, if
there is still no reply, will close the socket.

You have to tell your sockets to use keep-alives, using the
SetSocketOption
code KeepAlive.

Note that, even if keep-alives are on, the default idle time before
sending
them is 2 hours...

Paul T.

"MDB" <nospam> wrote in message
Hello All, I am using the below code to create a socket connection via
wi-fi to another device. The problem is that if the device I am
connecting
to losing power and reboots I lose the socket connection however, my
device
dosn't know it was dropped and dosn't receive any type of error when it
sends messages. From what I have read, CF dosn't support
SetSocketOption
so my question is does anyone know of a good way to work around
this?


****SocketConnect****
g_socClient = new Socket (AddressFamily.InterNetwork,SocketType.Stream
,ProtocolType.Tcp );
IPAddress ip = IPAddress.Parse (strRemoteIPBase);
IPEndPoint ipEnd = new IPEndPoint (ip,intTCPListenPort);
g_socClient.Connect ( ipEnd );

WaitForData();



****WaitForData ****

if ( pfnCallBack == null )
{
pfnCallBack = new AsyncCallback (OnDataReceived);
}
CSocketPacket theSocPkt = new CSocketPacket ();
theSocPkt.thisSocket = PMMobile.Classes.Globals.g_socClient;

m_asynResult = g_socClient.BeginReceive (theSocPkt.dataBuffer
,0,theSocPkt.dataBuffer.Length
,SocketFlags.None,pfnCallBack,theSocPkt);
 
M

MDB

I was thinking the 1 in
g_socClient.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.KeepAl
ive, 1 ) was to set the timeout to 1 and didn't realize it wasn't. Does
anyone know how to change this timeout.



Paul G. Tobey said:
Did you read my first message? Please reread *all* of it, especially the
last sentence.

Paul T.

MDB said:
Yes I know, I realize I forgot to post my error. Any way, this is what I
was doing:

g_socClient.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.SendTi
meout, 5000);

This is the error I was getting when this bit of code was excuted:
System.SystemException {"Could not find resource assembly"}

I just noticed that I was doing a SendTimeout instead of KeepAlive and
switched my code to use KeepAlive and I no longer get the error.

This is what it looks like now:
g_socClient = new Socket
(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp );
g_socClient.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.KeepAl
ive, 1 );
IPAddress ip = IPAddress.Parse (strRemoteIPBase);
IPEndPoint ipEnd = new IPEndPoint (ip,intTCPListenPort);
g_socClient.Connect ( ipEnd );


Although, I no longer get an error while excuting the program, it dosn't
raise any type of error when the socket becomes disconected.


Paul G. Tobey said:
g_socClient.SetSocketOption( Socket, KeepAlive, 1 ), is what I'd try. It
would help if you'd say what you did, when things don't work, as well as
telling us what error occurred.

Paul T.

"MDB" <nospam> wrote in message
First thanks for your reply however, how do you set the
SetSocketOption
using the compactframework? I have tried several different ways only
to
receive errors when the code is ran (not when compiled).



"Paul G. Tobey [eMVP]" <ptobey no spam AT no instrument no spam DOT
com>
wrote in message To notice that a connection has been broken, you either have to try to
send
something over the socket or you have to tell the network stack to
do
it
for
you. That is, if you are periodically sending packets, you'll find
out
roughly two minutes after you send the first packet after the other
device
dies that the connection is broken. In that case, the stack will detect
that there has been no acknowledgement. It will retry a number of times,
but, when all of those attempts fail, it will close the socket and you'll
get an error/exception.

If you are sitting around waiting to receive packets, though, you'll
never
realize that the other end of the socket is gone, unless you tell the
stack
to use keep-alive packets. When you turn that on, the network stack,
when
it detects a long period of inactivity, will periodically send a
packet
to
the other end of the connection. If it replies, the stack goes back
to
sleep. If it fails to reply, it will retry, in a certain pattern
and,
if
there is still no reply, will close the socket.

You have to tell your sockets to use keep-alives, using the
SetSocketOption
code KeepAlive.

Note that, even if keep-alives are on, the default idle time before
sending
them is 2 hours...

Paul T.

"MDB" <nospam> wrote in message
Hello All, I am using the below code to create a socket
connection
via
wi-fi to another device. The problem is that if the device I am
connecting
to losing power and reboots I lose the socket connection however, my
device
dosn't know it was dropped and dosn't receive any type of error
when
it
sends messages. From what I have read, CF dosn't support
SetSocketOption
so my question is does anyone know of a good way to work around
this?


****SocketConnect****
g_socClient = new Socket (AddressFamily.InterNetwork,SocketType.Stream
,ProtocolType.Tcp );
IPAddress ip = IPAddress.Parse (strRemoteIPBase);
IPEndPoint ipEnd = new IPEndPoint (ip,intTCPListenPort);
g_socClient.Connect ( ipEnd );

WaitForData();



****WaitForData ****

if ( pfnCallBack == null )
{
pfnCallBack = new AsyncCallback (OnDataReceived);
}
CSocketPacket theSocPkt = new CSocketPacket ();
theSocPkt.thisSocket = PMMobile.Classes.Globals.g_socClient;

m_asynResult = g_socClient.BeginReceive (theSocPkt.dataBuffer
,0,theSocPkt.dataBuffer.Length
,SocketFlags.None,pfnCallBack,theSocPkt);
 
P

Paul G. Tobey [eMVP]

According to the CE.NET help for 4.2,

KeepAliveTime : REG_DWORD Default setting is 7,200,000 (two hours).
This value controls how often TCP attempts to verify that an idle connection
is still intact by sending a keep-alive packet. If the remote system is
still reachable and functioning, it will acknowledge the keep-alive
transmission. Keep-alive packets are not sent by default. This feature may
be enabled on a connection by an application. The valid range for this value
is 1-0xFFFFFFFF (hexadecimal). For more information, see TCP Keep-Alive
Messages.


That's basically the same entry as on the desktop. I don't know if this
would be the same for 3.0 or not; I don't see any documentation for it. By
the way, don't set this value really small or you'll be pouring crap onto
your network. I wouldn't set this value any shorter than two or three
minutes...

Paul T.

MDB said:
I was thinking the 1 in
g_socClient.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.KeepAl
ive, 1 ) was to set the timeout to 1 and didn't realize it wasn't. Does
anyone know how to change this timeout.



Paul G. Tobey said:
Did you read my first message? Please reread *all* of it, especially the
last sentence.

Paul T.

MDB said:
Yes I know, I realize I forgot to post my error. Any way, this is
what I
was doing:

g_socClient.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.SendTi
meout, 5000);

This is the error I was getting when this bit of code was excuted:
System.SystemException {"Could not find resource assembly"}

I just noticed that I was doing a SendTimeout instead of KeepAlive and
switched my code to use KeepAlive and I no longer get the error.

This is what it looks like now:
g_socClient = new Socket
(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp );
g_socClient.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.KeepAl
ive, 1 );
IPAddress ip = IPAddress.Parse (strRemoteIPBase);
IPEndPoint ipEnd = new IPEndPoint (ip,intTCPListenPort);
g_socClient.Connect ( ipEnd );


Although, I no longer get an error while excuting the program, it
dosn't
raise any type of error when the socket becomes disconected.


"Paul G. Tobey [eMVP]" <ptobey no spam AT no instrument no spam DOT
com>
wrote in message g_socClient.SetSocketOption( Socket, KeepAlive, 1 ), is what I'd try. It
would help if you'd say what you did, when things don't work, as well as
telling us what error occurred.

Paul T.

"MDB" <nospam> wrote in message
First thanks for your reply however, how do you set the
SetSocketOption
using the compactframework? I have tried several different ways
only
to
receive errors when the code is ran (not when compiled).



"Paul G. Tobey [eMVP]" <ptobey no spam AT no instrument no spam DOT
com>
wrote in message To notice that a connection has been broken, you either have to try to
send
something over the socket or you have to tell the network stack to do
it
for
you. That is, if you are periodically sending packets, you'll find
out
roughly two minutes after you send the first packet after the other
device
dies that the connection is broken. In that case, the stack will
detect
that there has been no acknowledgement. It will retry a number of
times,
but, when all of those attempts fail, it will close the socket and
you'll
get an error/exception.

If you are sitting around waiting to receive packets, though,
you'll
never
realize that the other end of the socket is gone, unless you tell the
stack
to use keep-alive packets. When you turn that on, the network stack,
when
it detects a long period of inactivity, will periodically send a
packet
to
the other end of the connection. If it replies, the stack goes
back
to
sleep. If it fails to reply, it will retry, in a certain pattern and,
if
there is still no reply, will close the socket.

You have to tell your sockets to use keep-alives, using the
SetSocketOption
code KeepAlive.

Note that, even if keep-alives are on, the default idle time before
sending
them is 2 hours...

Paul T.

"MDB" <nospam> wrote in message
Hello All, I am using the below code to create a socket connection
via
wi-fi to another device. The problem is that if the device I am
connecting
to losing power and reboots I lose the socket connection however, my
device
dosn't know it was dropped and dosn't receive any type of error when
it
sends messages. From what I have read, CF dosn't support
SetSocketOption
so my question is does anyone know of a good way to work around
this?


****SocketConnect****
g_socClient = new Socket
(AddressFamily.InterNetwork,SocketType.Stream
,ProtocolType.Tcp );
IPAddress ip = IPAddress.Parse (strRemoteIPBase);
IPEndPoint ipEnd = new IPEndPoint (ip,intTCPListenPort);
g_socClient.Connect ( ipEnd );

WaitForData();



****WaitForData ****

if ( pfnCallBack == null )
{
pfnCallBack = new AsyncCallback (OnDataReceived);
}
CSocketPacket theSocPkt = new CSocketPacket ();
theSocPkt.thisSocket = PMMobile.Classes.Globals.g_socClient;

m_asynResult = g_socClient.BeginReceive (theSocPkt.dataBuffer
,0,theSocPkt.dataBuffer.Length
,SocketFlags.None,pfnCallBack,theSocPkt);
 
M

MDB

Right, I was going to set it to 60,000 once I got everything working .
This connection is actually a wireless ad-hoc to another device so it
shouldn't affect it too much.

Thank you for your help.



Paul G. Tobey said:
According to the CE.NET help for 4.2,

KeepAliveTime : REG_DWORD Default setting is 7,200,000 (two hours).
This value controls how often TCP attempts to verify that an idle connection
is still intact by sending a keep-alive packet. If the remote system is
still reachable and functioning, it will acknowledge the keep-alive
transmission. Keep-alive packets are not sent by default. This feature may
be enabled on a connection by an application. The valid range for this value
is 1-0xFFFFFFFF (hexadecimal). For more information, see TCP Keep-Alive
Messages.


That's basically the same entry as on the desktop. I don't know if this
would be the same for 3.0 or not; I don't see any documentation for it. By
the way, don't set this value really small or you'll be pouring crap onto
your network. I wouldn't set this value any shorter than two or three
minutes...

Paul T.

MDB said:
I was thinking the 1 in
g_socClient.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.KeepAl
ive, 1 ) was to set the timeout to 1 and didn't realize it wasn't. Does
anyone know how to change this timeout.



Paul G. Tobey said:
Did you read my first message? Please reread *all* of it, especially the
last sentence.

Paul T.

"MDB" <nospam> wrote in message
Yes I know, I realize I forgot to post my error. Any way, this is
what I
was doing:
g_socClient.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.SendTi
meout, 5000);

This is the error I was getting when this bit of code was excuted:
System.SystemException {"Could not find resource assembly"}

I just noticed that I was doing a SendTimeout instead of KeepAlive and
switched my code to use KeepAlive and I no longer get the error.

This is what it looks like now:
g_socClient = new Socket
(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp );
g_socClient.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.KeepAl
ive, 1 );
IPAddress ip = IPAddress.Parse (strRemoteIPBase);
IPEndPoint ipEnd = new IPEndPoint (ip,intTCPListenPort);
g_socClient.Connect ( ipEnd );


Although, I no longer get an error while excuting the program, it
dosn't
raise any type of error when the socket becomes disconected.


"Paul G. Tobey [eMVP]" <ptobey no spam AT no instrument no spam DOT
com>
wrote in message g_socClient.SetSocketOption( Socket, KeepAlive, 1 ), is what I'd
try.
It
would help if you'd say what you did, when things don't work, as
well
as
telling us what error occurred.

Paul T.

"MDB" <nospam> wrote in message
First thanks for your reply however, how do you set the
SetSocketOption
using the compactframework? I have tried several different ways
only
to
receive errors when the code is ran (not when compiled).



"Paul G. Tobey [eMVP]" <ptobey no spam AT no instrument no spam DOT
com>
wrote in message To notice that a connection has been broken, you either have to
try
to
send
something over the socket or you have to tell the network stack
to
do
it
for
you. That is, if you are periodically sending packets, you'll find
out
roughly two minutes after you send the first packet after the other
device
dies that the connection is broken. In that case, the stack will
detect
that there has been no acknowledgement. It will retry a number of
times,
but, when all of those attempts fail, it will close the socket and
you'll
get an error/exception.

If you are sitting around waiting to receive packets, though,
you'll
never
realize that the other end of the socket is gone, unless you tell the
stack
to use keep-alive packets. When you turn that on, the network stack,
when
it detects a long period of inactivity, will periodically send a
packet
to
the other end of the connection. If it replies, the stack goes
back
to
sleep. If it fails to reply, it will retry, in a certain pattern and,
if
there is still no reply, will close the socket.

You have to tell your sockets to use keep-alives, using the
SetSocketOption
code KeepAlive.

Note that, even if keep-alives are on, the default idle time before
sending
them is 2 hours...

Paul T.

"MDB" <nospam> wrote in message
Hello All, I am using the below code to create a socket connection
via
wi-fi to another device. The problem is that if the device I am
connecting
to losing power and reboots I lose the socket connection
however,
my
device
dosn't know it was dropped and dosn't receive any type of error when
it
sends messages. From what I have read, CF dosn't support
SetSocketOption
so my question is does anyone know of a good way to work around
this?


****SocketConnect****
g_socClient = new Socket
(AddressFamily.InterNetwork,SocketType.Stream
,ProtocolType.Tcp );
IPAddress ip = IPAddress.Parse (strRemoteIPBase);
IPEndPoint ipEnd = new IPEndPoint (ip,intTCPListenPort);
g_socClient.Connect ( ipEnd );

WaitForData();



****WaitForData ****

if ( pfnCallBack == null )
{
pfnCallBack = new AsyncCallback (OnDataReceived);
}
CSocketPacket theSocPkt = new CSocketPacket ();
theSocPkt.thisSocket = PMMobile.Classes.Globals.g_socClient;

m_asynResult = g_socClient.BeginReceive (theSocPkt.dataBuffer
,0,theSocPkt.dataBuffer.Length
,SocketFlags.None,pfnCallBack,theSocPkt);
 
P

Paul G. Tobey [eMVP]

Fine. Also note that it affects *all* sockets in the system, not just
yours. By the way, the registry path is
HKEY_LOCAL_MACHINE\Comm\Tcpip\Parms.

Paul T.

MDB said:
Right, I was going to set it to 60,000 once I got everything working .
This connection is actually a wireless ad-hoc to another device so it
shouldn't affect it too much.

Thank you for your help.



Paul G. Tobey said:
According to the CE.NET help for 4.2,

KeepAliveTime : REG_DWORD Default setting is 7,200,000 (two hours).
This value controls how often TCP attempts to verify that an idle connection
is still intact by sending a keep-alive packet. If the remote system is
still reachable and functioning, it will acknowledge the keep-alive
transmission. Keep-alive packets are not sent by default. This feature
may
be enabled on a connection by an application. The valid range for this value
is 1-0xFFFFFFFF (hexadecimal). For more information, see TCP Keep-Alive
Messages.


That's basically the same entry as on the desktop. I don't know if this
would be the same for 3.0 or not; I don't see any documentation for it. By
the way, don't set this value really small or you'll be pouring crap onto
your network. I wouldn't set this value any shorter than two or three
minutes...

Paul T.

MDB said:
I was thinking the 1 in
g_socClient.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.KeepAl
ive, 1 ) was to set the timeout to 1 and didn't realize it wasn't.
Does
anyone know how to change this timeout.



"Paul G. Tobey [eMVP]" <ptobey no spam AT no instrument no spam DOT
com>
wrote in message Did you read my first message? Please reread *all* of it, especially the
last sentence.

Paul T.

"MDB" <nospam> wrote in message
Yes I know, I realize I forgot to post my error. Any way, this is
what
I
was doing:


g_socClient.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.SendTi
meout, 5000);

This is the error I was getting when this bit of code was excuted:
System.SystemException {"Could not find resource assembly"}

I just noticed that I was doing a SendTimeout instead of KeepAlive and
switched my code to use KeepAlive and I no longer get the error.

This is what it looks like now:
g_socClient = new Socket
(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp );

g_socClient.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.KeepAl
ive, 1 );
IPAddress ip = IPAddress.Parse (strRemoteIPBase);
IPEndPoint ipEnd = new IPEndPoint (ip,intTCPListenPort);
g_socClient.Connect ( ipEnd );


Although, I no longer get an error while excuting the program, it
dosn't
raise any type of error when the socket becomes disconected.


"Paul G. Tobey [eMVP]" <ptobey no spam AT no instrument no spam DOT
com>
wrote in message g_socClient.SetSocketOption( Socket, KeepAlive, 1 ), is what I'd try.
It
would help if you'd say what you did, when things don't work, as well
as
telling us what error occurred.

Paul T.

"MDB" <nospam> wrote in message
First thanks for your reply however, how do you set the
SetSocketOption
using the compactframework? I have tried several different ways
only
to
receive errors when the code is ran (not when compiled).



"Paul G. Tobey [eMVP]" <ptobey no spam AT no instrument no spam DOT
com>
wrote in message To notice that a connection has been broken, you either have to try
to
send
something over the socket or you have to tell the network stack to
do
it
for
you. That is, if you are periodically sending packets, you'll find
out
roughly two minutes after you send the first packet after the other
device
dies that the connection is broken. In that case, the stack
will
detect
that there has been no acknowledgement. It will retry a number of
times,
but, when all of those attempts fail, it will close the socket and
you'll
get an error/exception.

If you are sitting around waiting to receive packets, though,
you'll
never
realize that the other end of the socket is gone, unless you
tell
the
stack
to use keep-alive packets. When you turn that on, the network
stack,
when
it detects a long period of inactivity, will periodically send a
packet
to
the other end of the connection. If it replies, the stack goes
back
to
sleep. If it fails to reply, it will retry, in a certain
pattern
and,
if
there is still no reply, will close the socket.

You have to tell your sockets to use keep-alives, using the
SetSocketOption
code KeepAlive.

Note that, even if keep-alives are on, the default idle time before
sending
them is 2 hours...

Paul T.

"MDB" <nospam> wrote in message
Hello All, I am using the below code to create a socket
connection
via
wi-fi to another device. The problem is that if the device I am
connecting
to losing power and reboots I lose the socket connection however,
my
device
dosn't know it was dropped and dosn't receive any type of
error
when
it
sends messages. From what I have read, CF dosn't support
SetSocketOption
so my question is does anyone know of a good way to work
around
this?


****SocketConnect****
g_socClient = new Socket
(AddressFamily.InterNetwork,SocketType.Stream
,ProtocolType.Tcp );
IPAddress ip = IPAddress.Parse (strRemoteIPBase);
IPEndPoint ipEnd = new IPEndPoint (ip,intTCPListenPort);
g_socClient.Connect ( ipEnd );

WaitForData();



****WaitForData ****

if ( pfnCallBack == null )
{
pfnCallBack = new AsyncCallback (OnDataReceived);
}
CSocketPacket theSocPkt = new CSocketPacket ();
theSocPkt.thisSocket = PMMobile.Classes.Globals.g_socClient;

m_asynResult = g_socClient.BeginReceive (theSocPkt.dataBuffer
,0,theSocPkt.dataBuffer.Length
,SocketFlags.None,pfnCallBack,theSocPkt);
 

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