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
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: //CreateaSocketSocketCreateHandle :=SysSockCreate(SOCKET_AF_INET, SOCKET_STREAM, 0);IFSocketCreateHandle<>SOCKET_INVALIDTHENdwValue :=1;//Settheoptionthatwecanreuse'old'portaddressesSysSockSetOption(SocketCreateHandle, SOCKET_SOL, SOCKET_SO_REUSEADDR, ADR(dwValue), SIZEOF(dwValue));TCPState :=10;END_IF10: //BindthesockettoourIPaddressandportSocketAddress.sin_family :=SOCKET_AF_INET;InetAddr :='127.0.0.1'; // This just for loopbackSocketAddress.sin_addr :=SysSockInetAddr(InetAddr);SocketAddress.sin_port :=SysSockHtons(TCP_LEUTWIL);bResult :=SysSockBind(SocketCreateHandle, ADR(SocketAddress), SIZEOF(SocketAddress));IFbResultTHENTCPState :=20;END_IF20: //ListenforaconnectionbResult :=SysSockListen(SocketCreateHandle, diMaxConnections);IFbResultTHENMasterSocketList.fd_count :=1; // only socket is the listeningMasterSocketList.fd_array[0] :=SocketCreateHandle;TCPState :=30;END_IF30: //Buildalistofopensocketsanduseselecttodetectactivity(read)FDCopy(source :=ADR(MasterSocketList), dest :=ADR(SlaveSocketList));SelectTimeOut.tv_sec :=30;SelectTimeOut.tv_usec :=0; // very short time outdiSize :=SysSockSelect(SOCKET_FD_SETSIZE, ADR(SlaveSocketList), 0, 0, ADR(SelectTimeOut));IFdiSize>0THENTCPState :=40;TCPindex :=0;END_IF40: //ProcessnextactivesocketinslavelistSocketHandle :=SlaveSocketList.fd_array[TCPindex];IFSocketHandle=SocketCreateHandleTHEN//WehaveanewconnectiontohandlediSize :=SIZEOF(SocketAddress);SocketHandle :=SysSockAccept(SocketCreateHandle, ADR(SocketAddress), ADR(diSize));IFSocketHandle<>SOCKET_INVALIDTHEN//GoodSocketaddittomasterlistTCPMax :=SocketHandle; // new socket is biggerMasterSocketList.fd_array[MasterSocketList.fd_count] :=SocketHandle;MasterSocketList.fd_count :=MasterSocketList.fd_count+1;END_IFTCPState :=100;ELSE//TrytoreadadatablockdiSize :=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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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() ?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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
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
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() ?
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.
Originally created by: KevinR
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.
Thank you very much KevinR, I am testing this library and it really seems easier and safer than SysSocket. A greeting!
Originally created by: KevinR
You are welcome
Cheers,
Kevin