# How to 'reset' or destroy socket before using it with SocketAccept

**URL:** <https://tech-community.robotics.abb.com/t/how-to-reset-or-destroy-socket-before-using-it-with-socketaccept/6378>\
**Category:** RobotStudio\
**Tags:** rapid, socket, networking\
**Created:** [November 14, 2014, 6:54pm UTC](https://tech-community.robotics.abb.com/t/how-to-reset-or-destroy-socket-before-using-it-with-socketaccept/6378 "2014-11-14T18:54:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pollux](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/pollux/32/4028_2.png) [@pollux](https://tech-community.robotics.abb.com/u/pollux)\
**Post date:** [November 14, 2014, 6:54pm UTC](https://tech-community.robotics.abb.com/t/how-to-reset-or-destroy-socket-before-using-it-with-socketaccept/6378/1 "2014-11-14T18:54:12Z")

</div>

Hi,

I’m working on a project that uses a (listening) socket.  
Sometimes when the client disconnects and I recreate the listening socket I get an error like:

> **41600: Socket error**
> 
> **Description**
> 
> Task: Networking
> 
> The specified client socket is already in use. The client socket must not be created before calling SocketAccept.
> 
> Program Ref. /Networking/createListeningSocket/SocketAccept/272

I’m wondering if there is a safe way to completely destroy all sockets so I can call the SocketCreate, SocketAccept functions when I catch an “ERROR”. ?

The relevant code is this;

> PROC createListeningSocket()
> 
> server\_ss:=SocketGetStatus(server\_socket);
> 
> IF server\_ss\<\>SOCKET\_CREATED THEN
> 
> SocketCreate server\_socket;
> 
> SocketBind server\_socket,“127.0.0.1”,1025;
> 
> SocketListen server\_socket;
> 
> SocketAccept server\_socket,client\_socket \Time:=WAIT\_MAX;
> 
> TPWrite “Accepted a new connection”;
> 
> server\_created:=TRUE;
> 
> drawing\_ready:=FALSE;
> 
> state:=“r”;
> 
> server\_ok:=TRUE;
> 
> TPWrite "SocketStatus: "+ValToStr(server\_ss);
> 
> ENDIF
> 
> client\_ss:=SocketGetStatus(client\_socket);
> 
> IF client\_ss=SOCKET\_CREATED THEN
> 
> TPWrite “Client socket already created.”;
> 
> ELSEIF client\_ss=SOCKET\_CONNECTED THEN
> 
> TPWrite “Client socket connected”;
> 
> ELSE
> 
> TPWrite "Unhandled socket state: "+ValToStr(client\_ss);
> 
> ENDIF
> 
> …

Any ideas how I can solve this?

p.

---

<div class="post-metadata">

**Author:** ![PerSvensson](https://dub1.discourse-cdn.com/flex005/user_avatar/tech-community.robotics.abb.com/persvensson/32/1040_2.png) [@PerSvensson](https://tech-community.robotics.abb.com/u/PerSvensson)\
**Post date:** [November 14, 2014, 7:48pm UTC](https://tech-community.robotics.abb.com/t/how-to-reset-or-destroy-socket-before-using-it-with-socketaccept/6378/2 "2014-11-14T19:48:37Z")

</div>

The way to do it is to add ERROR handler to your procedure.  
Here is an example from the Rapid manual that will hopefully will give you some ideas.  
And there is a lot of predefined ERRNO in the system. There is also a couple of instructions that is commonly used in error handling like  
RETRY: retries the instruction that made you end upp in the error handler  
TRYNEXT: will take the program pointer to the instruction after the failing one

VAR socketdev server\_socket;

VAR socketdev client\_socket;

VAR string client\_ip;

PROC server\_messaging()

VAR string receive\_string;

…

! Create, bind, listen and accept of sockets in error handlers

SocketReceive client\_socket \Str := receive\_string;

SocketSend client\_socket \Str := “Hello client with ip‑address”+client\_ip;

! Wait for acknowlegde from client

…

SocketClose server\_socket;

SocketClose client\_socket;

ERROR

IF ERRNO=ERR\_SOCK\_TIMEOUT THEN

RETRY;

ELSEIF ERRNO=SOCK\_CLOSED THEN

server\_recover;

RETRY;

ELSE

! No error recovery handling

ENDIF

ENDPROC

PROC server\_recover()

SocketClose server\_socket;

SocketClose client\_socket;

SocketCreate server\_socket;

SocketBind server\_socket, “192.168.0.1”, 1025;

SocketListen server\_socket;

SocketAccept server\_socket, client\_socket\ClientAddress:=client\_ip;

ERROR

IF ERRNO=ERR\_SOCK\_TIMEOUT THEN

RETRY;

ELSEIF ERRNO=ERR\_SOCK\_CLOSED THEN

RETURN;

ELSE

! No error recovery handling

ENDIF

ENDPROC
