# TCPSocket Question

**URL:** <https://forum.xojo.com/t/tcpsocket-question/13312>\
**Category:** General\
**Created:** [December 31, 2013, 4:24am UTC](https://forum.xojo.com/t/tcpsocket-question/13312 "2013-12-31T04:24:06Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [December 31, 2013, 4:24am UTC](https://forum.xojo.com/t/tcpsocket-question/13312/1 "2013-12-31T04:24:06Z")

</div>

Hello all. I have a question regarding TCPSockets. Lets say you connect and are issuing commands inside of the TCPSocket.Connect event. Lets say at that point in the middle of a block of commands within the TCPSockets.Connect event you lose network connectivity.How do I get TCPSocket to realize that we no longer have an “IsConnected”=True state when it still thinks we do. I understand TCP is layer 4 so the disconnect at layer 1 would be obvious however I would imagine TCP would be able to realize this loss of connectivity mid stream.

Any advise would be appreciated.  
Thank you in advance.

---

<div class="post-metadata">

**Author:** ![Norman\_P](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/858c86/32.png) [@Norman\_P](https://forum.xojo.com/u/Norman_P)\
**Post date:** [December 31, 2013, 4:59am UTC](https://forum.xojo.com/t/tcpsocket-question/13312/2 "2013-12-31T04:59:27Z")

</div>

You try & write something 😛  
It has no clue until you try to do something more and THEN you get the error

---

<div class="post-metadata">

**Author:** ![Jakob\_Krabbe](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/71e660/32.png) [@Jakob\_Krabbe](https://forum.xojo.com/u/Jakob_Krabbe)\
**Post date:** [December 31, 2013, 12:45pm UTC](https://forum.xojo.com/t/tcpsocket-question/13312/3 "2013-12-31T12:45:35Z")

</div>

Maybe a Timer can be a useful assistance in this case?  
To check if IsConnected still = TRUE?

---

<div class="post-metadata">

**Author:** ![Greg\_O\_Lone](https://forum.xojo.com/user_avatar/forum.xojo.com/greg_o_lone/32/49_2.png) [@Greg\_O\_Lone](https://forum.xojo.com/u/Greg_O_Lone)\
**Post date:** [December 31, 2013, 3:39pm UTC](https://forum.xojo.com/t/tcpsocket-question/13312/4 "2013-12-31T15:39:27Z")

</div>

The Error event should fire when a disconnect occurs.

---

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [December 31, 2013, 4:49pm UTC](https://forum.xojo.com/t/tcpsocket-question/13312/5 "2013-12-31T16:49:13Z")

</div>

[quote=55885:@Norman Palardy]You try & write something 😛  
It has no clue until you try to do something more and THEN you get the error[/quote]

I am watching this all in the IDE while stepping through the debugger. I turn off my MAC’s wireless network purposely right before a write. The “write” never changes the IsConnected=True unfortunately.

> [@55957:@Greg O'Lone](#):
>
> The Error event should fire when a disconnect occurs.

The LastError always stays at 0 during this test. If i run normally and disconnect normally I get a 102 as expected however during a mid stream disconnect I don’t get any errors.

I also tried a manual socket poll after the write directly in the connected event and I didn’t get any different results.

Also this is happening in a Console App in case that matters. 🙂 Thanks again.

---

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [December 31, 2013, 5:36pm UTC](https://forum.xojo.com/t/tcpsocket-question/13312/6 "2013-12-31T17:36:25Z")

</div>

[quote=55929:@Jakob Krabbe]Maybe a Timer can be a useful assistance in this case?  
To check if IsConnected still = TRUE?[/quote]

Thats kind of my problem. I can’t ever get IsConnected to go False **Only** when I pull the network plug in mid connection… Works fine if I attempt to connect to an unknown host (Error 103) as expected.

---

<div class="post-metadata">

**Author:** ![Maurizio\_Rossi](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/ba8739/32.png) [@Maurizio\_Rossi](https://forum.xojo.com/u/Maurizio_Rossi)\
**Post date:** [December 31, 2013, 5:47pm UTC](https://forum.xojo.com/t/tcpsocket-question/13312/7 "2013-12-31T17:47:26Z")

</div>

Hi Mike,

I think it is an OS problem and not a Xojo problem.  
You can check the connection state of your socket with the “netstat” command.  
Xojo TCP sockets are an abstraction of the underlying OS network stack.

---

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [December 31, 2013, 5:55pm UTC](https://forum.xojo.com/t/tcpsocket-question/13312/8 "2013-12-31T17:55:11Z")

</div>

Maurizio, I have just done that and you are right thank you. My OS is delaying in recognizing the down interface. I gave it more time in my IDE debugging and it finally showed my LastError 22. Phew 🙂

---

<div class="post-metadata">

**Author:** ![Maurizio\_Rossi](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/ba8739/32.png) [@Maurizio\_Rossi](https://forum.xojo.com/u/Maurizio_Rossi)\
**Post date:** [December 31, 2013, 5:57pm UTC](https://forum.xojo.com/t/tcpsocket-question/13312/9 "2013-12-31T17:57:35Z")

</div>

Happy new year Mike.

---

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [December 31, 2013, 5:59pm UTC](https://forum.xojo.com/t/tcpsocket-question/13312/10 "2013-12-31T17:59:25Z")

</div>

Weird. Again when I give it enough time in the debug I get Error 22 as expected now. However when I run my console app in the IDE it never fires the Error 22. Is the App going to fast through the writes or something do you think?

---

<div class="post-metadata">

**Author:** ![Maurizio\_Rossi](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/ba8739/32.png) [@Maurizio\_Rossi](https://forum.xojo.com/u/Maurizio_Rossi)\
**Post date:** [December 31, 2013, 6:15pm UTC](https://forum.xojo.com/t/tcpsocket-question/13312/11 "2013-12-31T18:15:00Z")

</div>

I think that console Apps depends on user applications support on handling events using DoEvents(): I don’t know how you are handling this aspect,  
The OS network stack also has some timing involved in the error report part.  
Maybe your application is writing when the OS has not yet recognized and reported the disconnected state of the socket.

---

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [December 31, 2013, 6:42pm UTC](https://forum.xojo.com/t/tcpsocket-question/13312/12 "2013-12-31T18:42:02Z")

</div>

It is a time thing. However is this normal? The OS shows “Inactive” on the link within seconds after I kill the interface. However it takes Xojo about 160 seconds later to catch the Error.

---

<div class="post-metadata">

**Author:** ![Maurizio\_Rossi](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/ba8739/32.png) [@Maurizio\_Rossi](https://forum.xojo.com/u/Maurizio_Rossi)\
**Post date:** [December 31, 2013, 8:56pm UTC](https://forum.xojo.com/t/tcpsocket-question/13312/13 "2013-12-31T20:56:31Z")

</div>

In this case the TCP protocol decides when the error can be intended as transient or permanent.  
But this behaviour can vary from OS to OS i.e. not all networking stacks report error immediately in this specific case.

---

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [December 31, 2013, 9:35pm UTC](https://forum.xojo.com/t/tcpsocket-question/13312/14 "2013-12-31T21:35:07Z")

</div>

Thanks Maurizio.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 5:00pm UTC](https://forum.xojo.com/t/tcpsocket-question/13312/15 "2020-10-29T17:00:19Z")

</div>


