WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Winsock

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Read UDP Packet Using Winsocket

 Keywords: UDP Packet Winsock MSWinsock Get Read 

Attached is a working Example of reading a UDP packet using Microsoft Winsocket Control.

It displays the Packet contents (in Hex) as Packets are received. This code does nothing with the received packet. The code runs for about 120 seconds before exiting.

To show it working I have linked the code the the Netbios name service port (port 137) and thus it will read incoming Netbios name service packets.

You can then use the 'nbtstat' dos command to send/receive Netbios packets I have found that using the command 'nbtstat -a winbatch' at a DOS prompt will trigger some receive packets which are then display.

The DOS command will report 'Host not found' this is normal as WinBatch is not a known Host Name. We just want the network to respond to the command.

You can use something like WireShark to look at the actual packets in more detail.

**** Warning **** This is reading the Netbios Port (137) data and thus this data will not get to the intended recipient.

This code is intended as an Example only.

#DefineSubRoutine GetWinSockData(NumBytes)                        ; UDF called when DataArrival Event is triggered
   ObjectEventRemove(objSocket, "DataArrival")                     ; Break link to DataArrival Event so no more are triggered once you are processing the Event
   vbByte = 17          ; Byte subtype
   vbArray = 8192       ; Array

   objSocket.GetData(RPacket, vbByte + vbArray, NumBytes)         ; Read Packet into Buffer
   BinaryEodSet(RPacket, NumBytes)
   Received = Received + 1                                           ; Packet Count
   ObjectEventAdd(objSocket, "DataArrival", "GetWinSockData")      ; Re-Enable DataArrival Event
#EndSubRoutine


AddExtender("wilx44i.dll")

objSocket = ObjectCreate("MSWinsock.Winsock")                        ; Open Microsoft Winsocket Controll (mswinsck.ocx)
objSocket.Protocol = 1                                             ; UDP
objSocket.LocalPort = 137                                          ; Port Number to monitor (137 Netbios)
objSocket.bind()                                                   ; Bind Socket to Local Port

BoxOpen("UDP Packet Reader", "Waiting.......")
Received = 0
RPacket=BinaryAlloc(2000)                                          ; Alocate Buffer for Receive Data (must be large enough for incoming packets
BinaryOleType(RPacket,302,0,0,0)                                    ; Set Packet type to BSTR* (direction input/output)

ObjectEventAdd(objSocket, "DataArrival", "GetWinSockData")         ; Link UDF to DataArrival Event UDF will be called when event happens

For A = 1 To 120                                                   ; Wait and display current status of Buffer (this waits about 120 Seconds before exiting)
   Text = StrCat(Received, @CR)

   byte = 1

   For B = 1 To 10
      Text = StrCat(Text, @CR)

      For c = 1 To 30
         Text = StrCat(Text, " " , StrFixLeft(xBaseConvert(BinaryPeek(RPacket, Byte), 10, 16),"0", 2))
         Byte = Byte + 1
      Next C
   Next B
   BoxText(Text)                                                ; Display Buffer contents
   TimeDelay(1)
Next A

BinaryFree(RPacket)
ObjectEventRemove(objSocket, "DataArrival")                     ; Remove Link to UDF

objSocket = 0

Article ID:   W18215
Filename:   Read UDP Packet Using  Winsocket.txt
File Created: 2011:05:27:07:51:50
Last Updated: 2011:05:27:07:51:50