Home |
Sockets, IP, and Port Addresses |
Sockets, IP, and Port AddressesEach network connection has an associated socket which is used to specify which connection an API call is referencing. It is analogous to the file pointer type when doing file I/O. Sockets are allocated by the API by calling function socket and are freed by calling closesocket or xn_abort. Each socket has a local IP and a port address associated with it if it is bound through function bind. The IP address consists of four bytes/32 bits. It specifies the local machine. Every machine on a network has a unique IP address and different interfaces on the same machine may or may not have unique IP addresses. An IP address consists of a network part as specified by the network mask and a host part which distinguishes the machines on the network. For example, a network with addresses 205.161.8.xx has a mask of 0xFFFFFF00. Generally, all machines on the same network have the same address in the network part of the address. The host part of the address (which is the last byte in the address in the example) is unique for each interface/machine and distinguishes the machines on the network. The port address specifies the protocol of the socket. The special BROADCAST address (IP address 0xFFFFFF) indicates the message needs to be sent to all hosts on the network (including any RS232 connections). If a packet is sent to a broadcast address, the Ethernet address is set to 0xFFFFFFFFFF. MULTICAST addresses provides the ability to send to a group of Ethernet interfaces. A multicast IP address has the first four bits set to 1110 (0xE). Packets sent to a multicast address are sent out on the interface specified by the routing table. Packets are received on an interface if the multicast address has been set up for the interface by function xn_interface_mcast or setsockopt. Broadcast messages are sent for UDP and ARP protocols only and multicast messages are sent for the UDP protocol only. Broadcast messages are sent out on every open interface except loopback. In order to send to broadcast or multicast addresses, use function sendto or connect with the remote address set to the broadcast address (INADDR_BROADCAST) or to the multicast address, respectively. In order to receive broadcasts, use function bind with the local address set to INADDR_ANY. To receive multicasts, you can bind to the local IP address, INADDR_ANY, or to the multicast address. You may also bind to INADDR_ANY to receive all packets sent to a specific port number. Blocking and Non-Blocking Sockets
|