man 2 socket ;; the 2 is for system calls; ;; open, close, read, write ;; a 3 would be for library function ;; printf, fgets, fopen Create a socket: socket(AF_INET, SOCK_DGRAM, 0); ^^^^^^^ "internet" : ip addresses, potentially ipv4 addresses ^^^^^^^^^^ datagram -> UDP (messages that fit in a packet) (as opposed to SOCK_STREAM ->TCP, text, ordered bytes, reliable) ^ switches between datagram socket implementations, of which there's only one. Fragmentation ; to be covered a bit more later. Large IP packet, typically larger than 1500 bytes, would be split into fragments to be reassembled at the destination. For long-distance links, we use Path MTU discovery. MTU == Maximum Transmission Unit ; for Ethernet, 1500 bytes. This informs the sender what maximum size packet won't experience fragmentation. Fragmentation does happen; typically for NFS (network file system) traffic. Servers tend to be local because they trust IP addresses, you want to send 4096 bytes at a time. The maximum size of an IP packet is 65,535 bytes. (deduct 20 for the IP header, 8 for the udp header) Jumbo frames (gig ether, -> ~9000 byte MTUs) to be loosely standardized. Small MTU -> multiplexing (other people get a chance) ability to detect bit errors. (2-bit errors, 3-bit errors) Large MTU -> efficiency, perhaps fewer headers wasting bandwidth, fewer times to lookup where a packet goes. Ports 1. Ephemeral - allocated to clients, doesn't matter what they are, allow the kernel to tell which conversation a packet belongs to. 2. Bound - allocated to servers, often well known ( 41710 ), allows clients to contact a specific service. Conversation is identified by the 5-tuple: Source port, Destination port, Source address, Destination IP address, Protocol (TCP or UDP) Implication is that you can send a message to one of our servers (at dest port 41710) from a socket (bound to) a different port. /etc/services lists all the IANA-allocated ports. ssh = 22, http = 80, ntp = 123. assignment: send/broadcast a message, then wait for messages and print them. man 2 bind ;; all the include files you need, are listed here. bind - will give us a port. we can ask for one, or let it come. bind - the addresses and ports are in network byte order. #include struct sockaddr_in { /* internet endpoint address, IP address and port */ sa_family_t sin_family; /* AF_INET ; PF_INET */ in_port_t sin_port; /* ...sin_port = htons(41710); */ struct in_addr sin_addr; /* ...sin_addr.s_addr = "224.0.50.112"; */ /* ...sin_addr.s_addr = inet_addr("224.0.50.112");*/ /* ...sin_addr.s_addr = htonl(0xc000.....);*/ /* ...sin_addr.s_addr = INADDR_ANY; if you were a server */ /* ...sin_addr.s_addr = INADDR_LOCALHOST; */ /* ...sin_addr.s_addr = inet_addr("127.0.0.1"); */ /* ...sin_addr.s_addr = htonl(0x7f000001); */ */ }; ** that was an address, how to fill in the fields setsockopt allow more than one process to bind the same port. --- for most servers, this would be bad. int one = 1; setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); before the bind. allow us to configure the multicast membership bits. --- man 2 sendto ;; takes the struct sockaddr_in as destination (don't let me forget the relevant setsockopt) packets