Problem with TCP sockets after Warm start

DaveW
2011-02-14
2018-01-29
  • DaveW - 2011-02-14

    Hi

    I am using V3.4sp1 on a windows CE PLC. I use TPC/IP sockets for communication, and listen for a connection like this

    CASE TCPState OF
        0:   // Create a Socket
            SocketCreateHandle := SysSockCreate(SOCKET_AF_INET, SOCKET_STREAM, 0, ADR(SocketError)); 
            IF SocketCreateHandle <> SysTypes.RTS_INVALID_HANDLE THEN
                dwValue := 1;
                // Set the option that we can reuse 'old' port addresses
                SocketError := SysSockSetOption(SocketCreateHandle, 
                                SOCKET_SOL, SOCKET_SO_REUSEADDR, ADR(dwValue), SIZEOF (dwValue));
                TCPState := 10;
            END_IF
        10:  // Bind the socket to our IP address and port
            SocketAddress.sin_family := SOCKET_AF_INET ;
            SocketError := SysSockInetAddr(InetAddr, ADR(SocketAddress.sin_addr));
            SocketAddress.sin_port := SysSockHtons(TCP_LEUTWIL);
            bResult := SysSockBind(SocketCreateHandle, ADR(SocketAddress), SIZEOF(SocketAddress));
            IF bResult <> SysTypes.RTS_INVALID_HANDLE THEN
                TCPState := 20;
            END_IF
        20:  // Listen for a connection
            bResult := SysSockListen(SocketCreateHandle, diMaxConnections);
            IF bResult <> SysTypes.RTS_INVALID_HANDLE THEN
                MasterSocketList.fd_count := 1; // only socket is the listening
                MasterSocketList.fd_array[0] := SocketCreateHandle;
                dwValue := 1;
                //SysSockIoctl(SocketCreateHandle, SOCKET_FIONBIO, ADR(dwValue)); // set to non blocking
                TCPState := 30;
            END_IF
        30:  // Build a list of open sockets and use select to detect activity (read)
            FDCopy(); // Copy master list to slave
            SelectTimeOut.tv_sec := 0;
            SelectTimeOut.tv_usec := 10; // very short time out
            bResult := SysSockSelect(SOCKET_FD_SETSIZE, ADR(SlaveSocketList), 0, 0, ADR(SelectTimeOut), ADR(diSize));
            IF diSize > 0 THEN  // Positive result sockets have activity (count of active sockets)
                TCPState := 40;
                TCPindex := 0;
            END_IF
    

    But after a warm start very often I can no longer connect to the PLC with TCP. The application is still running in state 30 above but socket will not accept a connection. Anybody got any ideas or is it a problem with the library ?

    Regards

    Dave W

     
  • danyamian - 2018-01-29

    DaveW hat geschrieben:
    Hi
    I am using V3.4sp1 on a windows CE PLC. I use TPC/IP sockets for communication, and listen for a connection like this
    CASE TCPState OF
    0: // Create a Socket
    SocketCreateHandle := SysSockCreate(SOCKET_AF_INET, SOCKET_STREAM, 0, ADR(SocketError));
    IF SocketCreateHandle <> SysTypes.RTS_INVALID_HANDLE THEN
    dwValue := 1;
    // Set the option that we can reuse 'old' port addresses
    SocketError := SysSockSetOption(SocketCreateHandle,
    SOCKET_SOL, SOCKET_SO_REUSEADDR, ADR(dwValue), SIZEOF (dwValue));
    TCPState := 10;
    END_IF
    10: // Bind the socket to our IP address and port
    SocketAddress.sin_family := SOCKET_AF_INET ;
    SocketError := SysSockInetAddr(InetAddr, ADR(SocketAddress.sin_addr));
    SocketAddress.sin_port := SysSockHtons(TCP_LEUTWIL);
    bResult := SysSockBind(SocketCreateHandle, ADR(SocketAddress), SIZEOF(SocketAddress));
    IF bResult <> SysTypes.RTS_INVALID_HANDLE THEN
    TCPState := 20;
    END_IF
    20: // Listen for a connection
    bResult := SysSockListen(SocketCreateHandle, diMaxConnections);
    IF bResult <> SysTypes.RTS_INVALID_HANDLE THEN
    MasterSocketList.fd_count := 1; // only socket is the listening
    MasterSocketList.fd_array[0] := SocketCreateHandle;
    dwValue := 1;
    //SysSockIoctl(SocketCreateHandle, SOCKET_FIONBIO, ADR(dwValue)); // set to non blocking
    TCPState := 30;
    END_IF
    30: // Build a list of open sockets and use select to detect activity (read)
    FDCopy(); // Copy master list to slave
    SelectTimeOut.tv_sec := 0;
    SelectTimeOut.tv_usec := 10; // very short time out
    bResult := SysSockSelect(SOCKET_FD_SETSIZE, ADR(SlaveSocketList), 0, 0, ADR(SelectTimeOut), ADR(diSize));
    IF diSize > 0 THEN // Positive result sockets have activity (count of active sockets)
    TCPState := 40;
    TCPindex := 0;
    END_IF
    But after a warm start very often I can no longer connect to the PLC with TCP. The application is still running in state 30 above but socket will not accept a connection. Anybody got any ideas or is it a problem with the library ?
    Regards
    Dave W

    Hi, I'm working on a similar project. I have a raspberry in which I need to run a TCP server where I will receive messages from clients by sockets. I can not get it to work properly, have you managed to get it to work properly? Do you have more information about the server or an example project?

    Thank you very much.

     

Log in to post a comment.