RobotStudio event

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

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.

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 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

    Per Svensson
    Robotics and Vision Specialist
    Consat Engineering