[r5]: / Socket Example / Device / Plc Logic / Application / PLC_PRG / svnobj  Maximize  Restore  History

Download this file

6 lines (6 with data), 15.4 kB

Gx_l;<!0┘jЭo\/√Р╦ЇgbP╠System.StringL{6f9dac99-8de1-4efc-8465-68ac443b7d08}SpecialFuncL{0db3d7bb-cde0-4416-9a7b-ce49a0124323}NoneImplementationL{3b83b776-fb25-43b8-99f2-3c507c9143fc}TextDocument	L{f3878285-8e4f-490b-bb1b-9acbb7eb04db}
TextLinesL{a5de0b0b-1cb5-4913-ac21-9d70293ec00d}Id
longTagTextbIF hMySocket = RTS_INVALID_HANDLE  AND xOpen THENT	// create a socket for an IPv4 TCP stream 	hMySocket := SysSockCreate(	iAddressFamily	:= SOCKET_AF_INET, (* SOCKET_AF_INET for ip4 connection. We do it locally, so it is a loopback *)F								diType 			:= SOCKET_STREAM,V								diProtocol 		:= SOCKET_IPPROTO_TCP,D								pResult 		:= ADR(result));&	xIsBound := FALSE; 	xOpen := FALSE;END_IF	 Z// go through the process of binding the portrIF hMySocket <> RTS_INVALID_HANDLE AND NOT xIsBound THEN		├	// set up port to reuse address so we can bind to ANY ip (0.0.0.0)h	result := SysSockSetOption(	hSocket 		:= hMySocket,`								diLevel 		:= SOCKET_SOL, // socket level т								diOption 		:= SOCKET_SO_REUSEADDR, // allow using an addres (e.g. localhost or ANY) multiple times!l								pdiOptionValue 	:= ADR(diReuseAddr), // enable"Z								diOptionLen 	:= SIZEOF(diReuseAddr));#		$%░	// fill out the struct used to pass along the binding information						&┬	socketAddress.sin_family := SOCKET_AF_INET; // same as on creation 'л	socketAddress.sin_addr.ulAddr := SOCKET_INADDR_ANY; // any means we bind it to all network interfaces(ь	socketAddress.sin_port := SysSockHtons(1200); // the port number we chose, converted to ethernet byte order):	// now do the actual binding*`	result := SysSockBind(		hSocket 		:= hMySocket,+T								pSockAddr 		:= ADR(socketAddress),,d								diSockAddrSize 	:= SIZEOF(socketAddress));-								.J	xIsBound := result = Errors.ERR_OK;	/t	// set socket to start listening for incoming connections0b	result := SysSockListen(	hSocket 		:= hMySocket,1<								diMaxConnections:= 1);2d	xIsBound := xIsBound AND result = Errors.ERR_OK;	3Ю	// set the option for non-blocking so accept() returns immediatly instead of waiting until a connection is made4(	diNonblocking := 1;5b	result := SysSockIoctl(		hSocket			:= hMySocket,6x								diCommand		:= SOCKET_FIONBIO, // non-blocking option7v								pdiParameter	:= ADR(diNonblocking)); // set it to 18z xIsBound := xIsBound AND result = Errors.ERR_OK;												9END_IF::// accept incoming connection;▓IF hMySocket <> RTS_INVALID_HANDLE AND xIsBound AND NOT xIsConnected THEN<з	// accept the connection if possible. As an output we get information about the socket we are connectiong to=v	hAcceptedSocket := SysSockAccept( 	hSocket 		:= hMySocket,>d										pSockAddr 		:= ADR(saConnectionPartner),?x										pdiSockAddrSize := ADR(diSaConnectionPartnerSize),@H										pResult 		:= ADR(result));A&																			Bn	xIsConnected := hAcceptedSocket <> RTS_INVALID_HANDLE;Cn	result := SysSockIoctl(		hSocket			:= hAcceptedSocket,D·// use the handle of the accepted connection to read the data that is arriving.E|IF hAcceptedSocket <> RTS_INVALID_HANDLE AND xIsConnected THENFh	// build the data structure needed to read a socketG2	socketSet.fd_count := 1;HT	socketSet.fd_array[0] := hAcceptedSocket;IR	// check if there is anyhting to be readJт	result := SysSockSelect(	pfdRead := ADR(socketSet), // pointer to set of the sockets we want to select onK>					pfdWrite := 0, // not usedL@					pfdExcept := 0, // not usedM≤					diWidth := SOCKET_FD_SETSIZE, // maximum number of sockets in socketSetNP					ptvTimeout := ADR(tvSelectTimeout),O^					pdiReady := ADR(diSocketsReadyToReceive));PH	IF diSocketsReadyToReceive > 0 THENQT		// read the bytes waiting in the socket.Rй		xiBytesReceived := SysSockRecv(		hSocket 		:= hAcceptedSocket, // the connected socket to read fromSz											pbyBuffer 		:= ADR(uiMessage), // where we read toT└											diBufferSize 	:= SIZEOF(uiMessage), // how much we readUh											diFlags 		:= SOCKET_MSG_NONE, // no flagsVL											pResult 		:= ADR(result)); W	END_IFX
					Yз	// if we don't have a timeout or we would not block, something failed, so connection must have dropped						Zт	IF result <> Errors.ERR_OK AND result <> Errors.ERR_TIMEOUT AND result <> Errors.ERR_SOCK_WOULDBLOCK THEN[0		xIsConnected := FALSE;\V		SysSockClose(hSocket := hAcceptedSocket);]P		hAcceptedSocket := RTS_INVALID_HANDLE;^(// close the socket._dIF hMySocket <> RTS_INVALID_HANDLE AND xClose THEN`H	SysSockClose(hSocket := hMySocket);aT	SysSockClose(hSocket := hAcceptedSocket);b$	xClose := FALSE; c.	xIsConnected := FALSE;dB	hMySocket := RTS_INVALID_HANDLE;eN	hAcceptedSocket := RTS_INVALID_HANDLE;f			gInterfacehL{a9ed5b7e-75c5-4651-af16-d2c27e98cb94}iPROGRAM PLC_PRGjVARk2	result : RTS_IEC_RESULT;l╙	hMySocket : RTS_IEC_HANDLE := RTS_INVALID_HANDLE; // handle for our receiving socketmх	hAcceptedSocket : RTS_IEC_HANDLE := RTS_INVALID_HANDLE; // handle for the connection to our partnern├	socketAddress : SOCKADDRESS; // temporary used as input for bind()o╦	saConnectionPartner : SOCKADDRESS; // socket address struct size of whoever connected to uspТ	diSaConnectionPartnerSize : DINT := SIZEOF(saConnectionPartner); // socket address struct size of whoever connected to usq≤	socketSet : SOCKET_FD_SET; // temporary variable used as input for select()r`	xClose : BOOL; // initiating closing the socketsZ	xOpen : BOOL; // initiate opening the sockett|	xIsBound : BOOL; // indicates if the socket is open and boundu░	xIsConnected : BOOL; // inidcates if someone is connected to the socketvl	uiMessage : UINT; // place to store incoming messageswt	xiBytesReceived : __XINT; // how many bytes were receivedx 	diReuseAddr	: DINT := 1; // always 1. It is passed into the set option call.y■	diNonblocking	: DINT := 1; // always 1. It is passed into the Ioctl call.zэ	tvSelectTimeout: SOCKET_TIMEVAL; // Timeout of select(). Left all 0 which means none, i.e. return immediatly.{з	diSocketsReadyToReceive : DINT; // output for select() ontaining the number of ports that can be received on|END_VAR}"UniqueIdGenerator~191POULevel─L{8e575c5b-1d37-49c6-941b-5c0ec7874787}│Standard┌ ChildObjectGuids┐8System.Collections.ArrayList└,AddAttributeSubsequent┘boolпп-пп	E
lпп
ґAзп-пп
ґ⌡зп-пп
ґзп-пп
ґзп-пп
ґзп-пп
ґзп-пп
ґFзп-пп
ґvзп-пп
ґ
зп-пп
ґ°зп-пп
ґDзп-пп
ґ?зп-пп
ґ█зп-пп
ґ▐зп-пп
ґ░зп-пп
ґ▒зп-пп
ґ▓зп- пп
ґ∙зп-!пп
ґ√зп-"пп
ґ⌠зп-#пп
ґ▌зп-$пп
ґQзп-%пп
ґIзп-&пп
ґuзп-'пп
ґ≥зп-(пп
ґKзп-пп
ґRзп-)пп
ґLзп-*пп
ґMзп-+пп
ґ@зп-,пп
ґ┘зп--пп
ґ├зп-.пп
ґwзп--пп
ґyзп-/пп
ґzзп-0пп
ґxзп-1пп
ґSзп-2пп
ґ┤зп-пп
ґ{зп-3пп
ґ└зп-4пп
ґзп-5пп
ґ─зп-6пп
ґ│зп-7пп
ґ|зп-8пп
ґ=зп-9пп
ґ²зп-$пп
ґCзп-:пп
ґзп-;пп
ґYзп-$пп
ґbзп-<пп
ґ\зп-=пп
ґ]зп->пп
ґ^зп-?пп
ґ_зп-@пп
ґ╡зп-Aпп
ґcзп-Bпп
ґЄзп-пп
ґІзп-3пп
ґЇзп-4пп
ґ╦зп-Cпп
ґ╧зп-6пп
ґ╣зп-7пп
ґWзп-9пп
ґ!зп-$пп
ґ+зп-Dпп
ґ,зп-Eпп
ґ÷зп-Fпп
ґkзп-Gпп
ґlзп-Hпп
ґёзп-$пп
ґ·зп-Iпп
ґ╨зп-Jпп
ґ╩зп-Kпп
ґ╪зп-Lпп
ґҐзп-Mпп
ґ╬зп-Nпп
ґ╗зп-Oпп
ґєзп-пп
ґ╘зп-Pпп
ґmзп-Qпп
ґnзп-Rпп
ґoзп-Sпп
ґpзп-Tпп
ґqзп-Uпп
ґdзп-Vпп
ґ╟зп-Wпп
ґ┬зп-Xпп
ґ═зп-Yпп
ґ┴зп-Zпп
ґ┼зп-[пп
ґ©зп-\пп
ґ▄зп-]пп
ґ▀зп-Wпп
ґeзп-9пп
ґ4зп-$пп
ґ7зп-пп
ґ╒зп-^пп
ґ8зп-_пп
ґ9зп-`пп
ґ╠зп-aпп
ґfзп-bпп
ґgзп-пп
ґhзп-cпп
ґiзп-dпп
ґjзп-eпп
ґ:зп-9пп
ґ0зп-fпп
ґ/зп-fпghп	E
пп
ґзп-iпп
ґзп-jпп
ґзп-kпп
ґ	зп-lпп
ґ'зп-mпп
ґзп-nпп
ґ#зп-oпп
ґ%зп-pпп
ґ*зп-qпп
ґ5зп-пп
ґ6зп-rпп
ґBзп-sпп
ґEзп-tпп
ґUзп-uпп
ґrзп-пп
ґsзп-vпп
ґtзп-wпп
ґ┌зп-пп
ґ■зп-xпп
ґ┐зп-yпп
ґ╙зп-пп
ґ╛зп-пп
ґ╞зп-zпп
ґ╚зп-{пп
ґзп-|п}-~п─-│┌┐п└┘ґ