[r2]: / linux-integr-prj,linux-integr,code / Device / Plc Logic / Application / PLC_PRG / svnobj  Maximize  Restore  History

Download this file

39 lines (37 with data), 15.4 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
IF hMySocket = RTS_INVALID_HANDLE  AND xOpen THEN
	// create a socket for an IPv4 TCP stream
	// set up port to reuse address so we can bind to ANY ip (0.0.0.0)
	result := SysSockSetOption(	hSocket 		:= hMySocket,
								diLevel 		:= SOCKET_SOL, // socket level
	// fill out the struct used to pass along the binding information						
	socketAddress.sin_addr.ulAddr := SOCKET_INADDR_ANY; // any means we bind it to all network interfaces
	// set the option for non-blocking so accept() returns immediatly instead of waiting until a connection is made
	diNonblocking := 1;
	result := SysSockIoctl(		hSocket			:= hMySocket,
								diCommand		:= SOCKET_FIONBIO, // non-blocking option
								pdiParameter	:= ADR(diNonblocking)); // set it to 1
 xIsBound := xIsBound AND result = Errors.ERR_OK;												
END_IF
// accept incoming connection
	// accept the connection if possible. As an output we get information about the socket we are connectiong to
	hAcceptedSocket := SysSockAccept( 	hSocket 		:= hMySocket,
										pSockAddr 		:= ADR(saConnectionPartner),
										pdiSockAddrSize := ADR(diSaConnectionPartnerSize),
										pResult 		:= ADR(result));
																			
	xIsConnected := hAcceptedSocket <> RTS_INVALID_HANDLE;
	result := SysSockIoctl(		hSocket			:= hAcceptedSocket,
	result := SysSockSelect(	pfdRead := ADR(socketSet), // pointer to set of the sockets we want to select on
					pfdWrite := 0, // not used
					pfdExcept := 0, // not used
		xiBytesReceived := SysSockRecv(		hSocket 		:= hAcceptedSocket, // the connected socket to read from
											pbyBuffer 		:= ADR(uiMessage), // where we read to
	// if we don't have a timeout or we would not block, something failed, so connection must have dropped						
	hMySocket : RTS_IEC_HANDLE := RTS_INVALID_HANDLE; // handle for our receiving socket
	socketAddress : SOCKADDRESS; // temporary used as input for bind()
	diSaConnectionPartnerSize : DINT := SIZEOF(saConnectionPartner); // socket address struct size of whoever connected to us
	xIsConnected : BOOL; // inidcates if someone is connected to the socket
	uiMessage : UINT; // place to store incoming messages
	xiBytesReceived : __XINT; // how many bytes were received
	diNonblocking	: DINT := 1; // always 1. It is passed into the Ioctl call.
	diSocketsReadyToReceive : DINT; // output for select() ontaining the number of ports that can be received on
END_VAR