© MCS Electronics, 1995-2007
config tcpip , we need to enable the interrupts
Config Tcpip = Int0 , Mac = 12.128.12.34.56.78 , Ip = 192.168.0.8 , Submask = 255.255.255.0 , Gateway = 0.0.0.0 , Localport = 1000 , Tx = $55 , Rx = $55
'Use the line below if you have a gate way
'Config Tcpip = Int0 , Mac = 12.128.12.34.56.78 , Ip = 192.168.0.8 , Submask = 255.255.255.0 , Gateway = 192.168.0.1 , Localport = 1000 , Tx = $55 , Rx = $55
Dim Idx As Byte |
' socket number |
Dim Result As Word |
' result |
Dim S(80) As Byte |
|
Dim Sstr As String * 20 |
' temp bytes |
Dim Temp As Byte , Temp2 As Byte |
'----------------------------------------------------------------------------- |
|
---------------
'When you use UDP, you need to dimension the following variables in exactly the same order !
Dim Peersize As Integer , Peeraddress As Long , Peerport As Word '-----------------------------------------------------------------------------
--------------- |
' a handy function |
Declare Function Ipnum(ip As Long) As String |
'like with TCP, we need to get a socket first |
|
'note that for UDP we specify sock_dgram |
' get socket for |
Idx = Getsocket(idx , Sock_dgram , 5000 , 0) |
UDP mode, specify port 5000 |
|
Print "Socket " ; Idx ; " " ; Idx |
|
'UDP is a connection less protocol which means that you can not listen, connect or can get the status
'You can just use send and receive the same way as for TCP/IP. 'But since there is no connection protocol, you need to specify the destination IP address and port
'So compare to TCP/IP you send exactly the same, but with the addition of the IP and PORT
Do
Temp = Inkey() |
' wait for |
|
terminal input |
' ESC pressed |
|
If Temp = 27 Then |
|
Sstr = "Hello" |
|
|
Result = Udpwritestr(192.168.0.3 , 5000 , Idx , Sstr , 255) |
|
End If |
' get number of |
Result = Socketstat(idx , Sel_recv) |
bytes waiting |
|
|
If Result > 0 Then |
|
|
Print "Bytes waiting : " ; Result |
'the first 8 bytes |
Temp2 = Result - 8 |
are always the UDP header which consist of the length, IP number and port |
|
address |
' read the result |
Temp = Udpread(idx , S(1) , Result) |
For Temp = 1 To Temp2 |
' print result |
|
Print S(temp) ; " " ; |
|
Next |
|
|
Print |
' these are |
|
Print Peersize ; " " ; Peeraddress ; " " ; Peerport |
|
assigned when you use UDPREAD |
' print IP in |
|
Print Ipnum(peeraddress) |
|
usual format |
|
' |
Result = Udpwrite(192.168.0.3 , Peerport , Idx , S(1) , Temp2) |
write the received data back |
|
|
End If |
|
|
Loop |
|
|
'the sample above waits for data and send the data back for that reason temp2 is subtracted with 8, the header size