Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

Problem with SysSockSelect on CoDeSys SP Win V3

DaveW
2010-02-23
2018-02-09
  • DaveW - 2010-02-23

    I am using CoDeSys version V3.3 SP1 patch 2 and using the socket libary SysSocket23 to make a TCP listening socket. Then I use SysSockSelect() to see if there is any connection on the socket. I expect a return of either 0 (timeout), or 1 because I have only 1 socket in my SOCKET_FD_SET structure. But it returns straight away with 2, why? I don't have any connection and the call to SysSockAccept() then blocks. Here is the code.

    // Process TCP sockets in steps using state

    CASE TCPState OF

    0:   // Create a Socket
    
        SocketCreateHandle := SysSockCreate(SOCKET_AF_INET, SOCKET_STREAM, 0);
    
        IF SocketCreateHandle <> SOCKET_INVALID THEN
    
            dwValue := 1;
    
            // Set the option that we can reuse 'old' port addresses
    
            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 ;
    
        InetAddr := '127.0.0.1'; // This just for loopback
    
        SocketAddress.sin_addr := SysSockInetAddr(InetAddr);
    
        SocketAddress.sin_port := SysSockHtons(TCP_LEUTWIL);
    
        bResult := SysSockBind(SocketCreateHandle, ADR(SocketAddress), SIZEOF(SocketAddress));
    
        IF bResult THEN
    
            TCPState := 20;
    
        END_IF
    
    20:  // Listen for a connection
    
        bResult := SysSockListen(SocketCreateHandle, diMaxConnections);
    
        IF bResult THEN
    
            MasterSocketList.fd_count := 1; // only socket is the listening
    
            MasterSocketList.fd_array[0] := SocketCreateHandle;
    
            TCPState := 30;
    
        END_IF
    
    30:  // Build a list of open sockets and use select to detect activity (read)
    
        FDCopy(source := ADR(MasterSocketList), dest := ADR(SlaveSocketList));
    
        SelectTimeOut.tv_sec := 30;
    
        SelectTimeOut.tv_usec := 0; // very short time out
    
        diSize := SysSockSelect(SOCKET_FD_SETSIZE, ADR(SlaveSocketList), 0, 0, ADR(SelectTimeOut));
    
        IF diSize > 0 THEN
    
            TCPState := 40;
    
            TCPindex := 0;
    
        END_IF
    
    40:  // Process next active socket in slave list
    
        SocketHandle := SlaveSocketList.fd_array[TCPindex];
    
        IF SocketHandle = SocketCreateHandle THEN
    
            // We have a new connection to handle
    
            diSize := SIZEOF(SocketAddress);
    
            SocketHandle := SysSockAccept(SocketCreateHandle, ADR(SocketAddress), ADR(diSize));
    
            IF SocketHandle <> SOCKET_INVALID THEN
    
                // Good Socket add it to master list
    
                TCPMax := SocketHandle; // new socket is bigger
    
                MasterSocketList.fd_array[MasterSocketList.fd_count] := SocketHandle;
    
                MasterSocketList.fd_count := MasterSocketList.fd_count + 1;
    
            END_IF
    
            TCPState := 100;
    
        ELSE
    
            // Try to read a data block
    
            diSize := SysSockRecv(SocketHandle, ADR(TCPData), TCP_BUFF_SIZE, 0);
    

    For target I am using the CoDeSys SP Win V3

    I based my code on the example for TCP on CoDeSys site so I think my code is correct but SysSockSelect() has bug.

    Help please anybody.

    Regards

    Dave Warren

     
  • DaveW - 2010-02-24

    Posting here is almost useless as I end up taking to myself

    I overcame my problems with SysSockSelect(), basically it is useless so rather than use it I set the socket to be non-blocking and check the return value from the SysSockAccept().

    But can someone fix the SysSockSelect() ?

     
  • wisher - 2010-11-11

    Hello Mr. Warren,

    Now I am developing a tcp server for the controller based on CodeSys.
    Could you send me last version of your server code please?
    Of course, if it is not a secret or non-secret part of it.

    Email: e listouque@gmail.com e

    Best regards.

     
  • danyamian - 2018-01-29

    wisher hat geschrieben:
    Hello Mr. Warren,
    Now I am developing a tcp server for the controller based on CodeSys.
    Could you send me last version of your server code please?
    Of course, if it is not a secret or non-secret part of it.
    Email: e listouque@gmail.com e
    Best regards.

    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.

     
  • Anonymous - 2018-01-31

    Originally created by: KevinR

    danyamian hat geschrieben:
    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.

    Hello danyamian,

    the topic is from 2010.

    In the meantime, there are ready-to-use libraries that contain complete function blocks for your application. Check out the CAA Net Base Services Library from the CAA Technical Workgroup (Namespace: NBS).

    Of course, you can also use the SysSocket Library. However, you should be familiar with SysSocket.

    IMG: CAA NBS.PNG

     
  • danyamian - 2018-02-06

    Thank you very much KevinR, I am testing this library and it really seems easier and safer than SysSocket. A greeting!

    KevinR hat geschrieben:
    Hello danyamian,
    the topic is from 2010.
    In the meantime, there are ready-to-use libraries that contain complete function blocks for your application. Check out the CAA Net Base Services Library from the CAA Technical Workgroup (Namespace: NBS).
    Of course, you can also use the SysSocket Library. However, you should be familiar with SysSocket.

     
  • Anonymous - 2018-02-09

    Originally created by: KevinR

    You are welcome

    Cheers,
    Kevin

    danyamian hat geschrieben:
    Thank you very much KevinR, I am testing this library and it really seems easier and safer than SysSocket. A greeting!

     

Log in to post a comment.