# TCP/IP disconnect not handled correctly

**URL:** <https://tech-community.robotics.abb.com/t/tcp-ip-disconnect-not-handled-correctly/3795>\
**Category:** Robot Controller\
**Created:** [September 13, 2010, 1:16pm UTC](https://tech-community.robotics.abb.com/t/tcp-ip-disconnect-not-handled-correctly/3795 "2010-09-13T13:16:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeroen](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/jeroen/32/3305_2.png) [@jeroen](https://tech-community.robotics.abb.com/u/jeroen)\
**Post date:** [September 13, 2010, 1:16pm UTC](https://tech-community.robotics.abb.com/t/tcp-ip-disconnect-not-handled-correctly/3795/1 "2010-09-13T13:16:53Z")

</div>

Hi,

I have a TCP/IP connection between my robot to a control program running on a PC. The code below runs in a separate semi-static task.  
If I physically disconnect the robot (plug out cable), after a while this task crashes on the SocketSend function call shown below.  
The socket\_status keeps reporting SOCKET\_CONNECTED however.  
How can I fix/work around this?

Regards,

! --------------------------------------------------------------------------------  
! Send status to client.  
! --------------------------------------------------------------------------------  
TRAP TrapStatus  
VAR socketStatus sockStat;  
sockStat:=SocketGetStatus(client\_socket);  
IF sockStat=SOCKET\_CONNECTED THEN  
! Send status to client.  
SocketSend client\_socketStr:=Status+ByteToStr(13Char)+ByteToStr(10Char);  
ELSE  
! Connection lost?  
! …  
ENDIF  
! Acknowledge.  
SetDO doStatus,0;  
RETURN;  
ERROR  
!TryNext;  
! Acknowledge.  
SetDO doStatus,0;  
RETURN;  
ENDTRAP

---

<div class="post-metadata">

**Author:** ![Henrik\_Berlin](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/henrik_berlin/32/1011_2.png) [@Henrik\_Berlin](https://tech-community.robotics.abb.com/u/Henrik_Berlin)\
**Post date:** [September 17, 2010, 4:04pm UTC](https://tech-community.robotics.abb.com/t/tcp-ip-disconnect-not-handled-correctly/3795/2 "2010-09-17T16:04:00Z")

</div>

Moved to RobotWare forum.

---

<div class="post-metadata">

**Author:** ![James\_NC](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@James\_NC](https://tech-community.robotics.abb.com/u/James_NC)\
**Post date:** [September 24, 2010, 9:46pm UTC](https://tech-community.robotics.abb.com/t/tcp-ip-disconnect-not-handled-correctly/3795/3 "2010-09-24T21:46:45Z")

</div>

I’ve been working through this myself, and I think I can help. SOCKET\_CONNECTED will be true unless the client or server explicitly closes the connection. However, if the cable is unplugged, you should be getting a Timeout Error. You can handle error with the following error catcher:

ERROR  
IF ERRNO=ERR\_SOCK\_TIMEOUT THEN  
…

Just be careful not to spend too much time in your TRAP, as I believe code ignores any other interrupts while inside a TRAP.

---

<div class="post-metadata">

**Author:** ![lubosko105](https://avatars.discourse-cdn.com/v4/letter/l/ecccb3/32.png) [@lubosko105](https://tech-community.robotics.abb.com/u/lubosko105)\
**Post date:** [January 24, 2012, 9:57pm UTC](https://tech-community.robotics.abb.com/t/tcp-ip-disconnect-not-handled-correctly/3795/4 "2012-01-24T21:57:41Z")

</div>

Hallo jeroen.  
In your topic I see you are writing TCP communication. I want to make TCP server on the server side. Do you know if is posible to start new thread with socket (returned by SocketAccept fnc) ? Your code looks working like this.  
thanks for help.

---

<div class="post-metadata">

**Author:** ![jeroen](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/jeroen/32/3305_2.png) [@jeroen](https://tech-community.robotics.abb.com/u/jeroen)\
**Post date:** [January 26, 2012, 8:04am UTC](https://tech-community.robotics.abb.com/t/tcp-ip-disconnect-not-handled-correctly/3795/5 "2012-01-26T08:04:06Z")

</div>

What exactly do you mean by a new thread?

I don’t think you can make a server with a thread for each connection; you just have to handle all connections in the same task.

In my application I just need to handle a single connection.

I have a ServerStart procedure:

PROC ServerStart(bool Recover)

IF Recover THEN  
Log “ServerStart with recovery need”;

SocketClose server\_socket;

SocketClose client\_socket;

ELSE

Log “ServerStart without the need for recovery”;

ENDIF

SocketCreate server\_socket;

SocketBind server\_socket, IPAddress, IPPort;

SocketListen server\_socket;

Log “Wait for connection”;

SocketAccept server\_socket, client\_socketClientAddress:=client\_ipTime:=WAIT\_MAX;

Log "Connection with " + client\_ip;

ERROR

IF ERRNO=ERR\_SOCK\_TIMEOUT THEN

Log “ERR\_SOCK\_TIMEOUT”;

Retry;

ELSEIF ERRNO=ERR\_SOCK\_CLOSED THEN

Log “ERR\_SOCK\_CLOSED”;

Return;

ELSE

Log “Unknown error”

ENDIF

ENDPROC

And in the main loop I handle the server like:

ServerStart(FALSE);  
– Some more initialization

WHILE TRUE DO

SocketReceive client\_socket Str:=ReceiveTime:=nHeartbeatTimeout;

Log Receive;

! Process data

ENDWHILE

ERROR

IF ERRNO=ERR\_SOCK\_TIMEOUT THEN

Log “Timeout”;

! Close connection and restart.

ServerStart(TRUE);

Retry;

ELSEIF ERRNO=ERR\_SOCK\_CLOSED THEN

Log “Socket closed by peer!”;

ServerStart(TRUE);

Retry;

ELSE

Log “Unknown error”;

ENDIF

ENDPROC

---

<div class="post-metadata">

**Author:** ![niggos](https://avatars.discourse-cdn.com/v4/letter/n/a3d4f5/32.png) [@niggos](https://tech-community.robotics.abb.com/u/niggos)\
**Post date:** [February 10, 2012, 12:29am UTC](https://tech-community.robotics.abb.com/t/tcp-ip-disconnect-not-handled-correctly/3795/6 "2012-02-10T00:29:38Z")

</div>

Hi there!

I’m quiet new to RAPID, but I spent some time on developing  
a TCP-based communication from client-PC to server-IRC5.

just a thought:  
If you have the option “Multitasking” installed, you can use some additional tasks for separate server sockets that listen continously for incoming data.

@jeroen:  
You can try to use

_ResetRetrycount;_

in your error handling rigth after

ServerStart(TRUE);

The standard number of retry counts is 4 (configurable somewhere …), so after four times of handling the same error, the

Retry;

will not be executed and the loop/proc will come to end/be finished.

hope this might help!?
