UDPSocketClose
Closes an opened socket.
Syntax
UDPSocketClose( SocketID )
Example #1
' A simple UDP client example sending data to another device and receiving data back
'UDP Variables
Const RecvBufferSize = 200
Public SockID As Long
Public bytesWritten As Long
Public bytesReceived As Long
Public recvDatagramStr As String * RecvBufferSize 'Leave space for null terminator
Public remoteIP As String * 100
Public remotePort As Long
Public recvTimeouts As Long 'increment this if we time out waiting for a response
Public PTemp, Batt_volt
Dim inputSize As Long
Dim recvDatagramBytes(RecvBufferSize) As UINT1 'We are receiving raw bytes back, so use UINT1
DataTable (Test,1,-1)
DataInterval (0,15,Sec,10)
Minimum (1,Batt_volt,FP2,False,False)
Sample (1,PTemp,FP2)
EndTable
'Main Program
BeginProg
Scan (1,Sec,0,0)
Battery (Batt_volt)
CallTable Test
NextScan
SlowSequence
Scan(30,Sec,0,0)
UDPSocketOpen(SockID, 0, 3) 'We are a client, so get an ephemeral port
If (SockID >= 0) Then
'Socket successfully opened
'If you were trying to send to a remote host with a domain name, you would use the DNSQuery instruction here to get the IP associated
' with that domain
'example:
'
' Public ipAddr As String * 100
' DNSQuery(Ret, ipAddr, "domain.com")
' If Ret = 0 Then
' ' We successfully resolved the domain to an IP, put rest of code in here
UDPSocketSend(bytesWritten,SockID,"10.34.10.12", 40000,"Bytes sent over UDP",19) 'size 19 to not include null character
If (bytesWritten >= 0) Then
'Successfully wrote out the message, now get a response
UDPSocketRecv(bytesReceived,SockID,recvDatagramBytes,0,remoteIP,remotePort, 500) 'Wait half a second for a response
If (bytesReceived > 0) Then
'We got a response with a non-zero payload size
If (bytesReceived >= RecvBufferSize) Then
inputSize = RecvBufferSize
'They sent us more bytes than our buffer can hold, so the input was truncated to the size of the buffer.
'Since this is just binary data, we need to add the null terminator ourselves before copying into our string. Since the buffer
'is full, we'll overwrite the last byte of data with a null terminator
recvDatagramBytes(RecvBufferSize) = &h00
Else
inputSize = bytesReceived + 1 'Add 1 for null terminator we are going to add
'We know that we have space in the buffer to add a null terminator without overwriting any data
recvDatagramBytes(inputSize) = &h00
EndIf
'Copy into string variable (this code makes the assumption that all of these bytes are printable characters, which is not an
'assumption actual application should make)
MoveBytes(recvDatagramStr, 0, recvDatagramBytes, 0, inputSize)
ElseIf (bytesReceived = -1) Then
'We timed out waiting for a response
recvTimeouts += 1
EndIf
EndIf
UDPSocketClose(SockID) 'If a socket is successfully opened, it will stay open until it is closed
EndIf
NextScan
EndSequence
EndProg
Remarks
UDPSocketClose() closes an opened socket. This includes freeing all associated memory (including discarding any queued up received datagrams that have not been processed by calling UDPSocketRecv()).
Parameters
SocketID
Socket ID returned from a call to UDPSocketOpen().
Type: Constant or Variable of type Long