UDP
Jump to navigation
Jump to search
Sending data over UDP
Step 1: Allocate a new UDP device.
- Use udpAlloc to get a device number for a udp device. If allocation fails, the device number returned will be SYSERR.
dev = tcpAlloc())
Step 2: Get the local ip address
- Use netLookup to find our local ip address.
- If interface is NULL, then we don't have a valid network interface.
This usually happens when a user forgets to run the netup command.
interface = netLookup((ethertab[0].dev)->num); localhost = &(interface->ip);
Step3: Open the UDP device
- To open the device simply call open( ) with the following arguments:
- The UDP device number
- Our local ip address
- The destination's ip address
- The local port to use
- The remote port to connect to
open(dev, localIP, dstIP, localPort, remotePort)
Again if open fails, SYSERR is returned.
Step 4: Send the packet
- To actually send the data, use write( ) with the device, buffer, and length arguments.
- The actual data you want to send will be written to the buffer. The length arg is the length of the buffer.
write(dev, buf, MSG_MAX_LEN)
As usual, write returns SYSERR if it fails.
Step 5: Close the connection
- Once the device is finish being used, be responsible and call close to return the device.
close(dev);