How to 'reset' or destroy socket before using it with SocketAccept
41600: Socket errorDescriptionTask: NetworkingThe specified client socket is already in use. The client socket must not be created before calling SocketAccept.Program Ref. /Networking/createListeningSocket/SocketAccept/272
Any ideas how I can solve this?PROC createListeningSocket()server_ss:=SocketGetStatus(server_socket);IF server_ss<>SOCKET_CREATED THENSocketCreate 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);ENDIFclient_ss:=SocketGetStatus(client_socket);IF client_ss=SOCKET_CREATED THENTPWrite "Client socket already created.";ELSEIF client_ss=SOCKET_CONNECTED THENTPWrite "Client socket connected";ELSETPWrite "Unhandled socket state: "+ValToStr(client_ss);ENDIF.....
Comments
-
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 likeRETRY: retries the instruction that made you end upp in the error handlerTRYNEXT: 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
Per Svensson
Robotics and Vision Specialist
Consat Engineering0
Categories
- All Categories
- 5.5K RobotStudio
- 396 UpFeed
- 18 Tutorials
- 13 RobotApps
- 297 PowerPacs
- 405 RobotStudio S4
- 1.8K Developer Tools
- 250 ScreenMaker
- 2.8K Robot Controller
- 316 IRC5
- 60 OmniCore
- 7 RCS (Realistic Controller Simulation)
- 797 RAPID Programming
- AppStudio
- 3 RobotStudio AR Viewer
- 18 Wizard Easy Programming
- 105 Collaborative Robots
- 5 Job listings