Tutorial 8 Socket Programming Internet Networking Tutorial 8 Socket Programming
What is socket? First appeared in 4.1 BSD UNIX 1982. An integer associated with a network connection. Implemented as file descriptor. Types of Internet Sockets: Stream Sockets for TCP connection. Datagram Socket for UDP communication. Raw Sockets.
Socket Interface User Space App1 App2 Socket I/F IP Stack Kernel ND1
Structures ‘Struct sockaddr’ holds socket address information: unsigned short sa_family; // AF_INET for internet char sa_data[14]; // 14 bytes of protocol address. contains a destination address and port number for the socket. };
Structures ‘struct sockaddr_in’ is used to deal with ‘struct sockaddr’ for internet family (AF_INET): struct sockaddr_in { short int sin_family; // AF_INET for internet unsigned short int sin_port; // Port number struct in_addr sin_addr; // Internet address unsigned char sin_zero[8]; // padding (must be zero) }; A pointer to a ‘struct sockaddr_in’ can be cast to a pointer to a ‘struct sockaddr’ and vice-versa, since they both have the same size.
Structures In order to set the IP address one should use ‘struct in_addr’: struct in_addr { unsigned long s_addr; // IPv4 address };
Bytes Order The CPU and the NIC may store and read data from the memory in a different order. Memory CPU R/W R/W NIC
Bytes Order Two ways to store data in the memory: Big-Endian Little-Endian One should convert the order of the bytes (if necessary) in order to transmit them in network in the same way. 1 2 3 4 4 3 2 1
Bytes Order Accessories Function: htons(x) – Convert short from host to network order. htonl(x) ntohs(x) ntohl(x) In order to be portable one should use this function even if not necessary (i.e. in a little-endian machine).
System Calls - Socket int socket(int domain, int type, int protocol); Return a file descriptor (or -1 in case of error). domain: AF_INET for internet. type: SOCK_STREAM for TCP, SOCK_DGRAM for UDP and SOCK_RAW for raw socket.
System Call - Bind int bind(int sockfd, struct sockaddr *my_addr, int addrlen); ‘sockfd’ is the socket file descriptor. Associate a socket with a port on the local machine (usually used by servers). ‘my_addr’ is a pointer to a struct sockaddr that contains information about the port. ‘addrlen’ is set to sizeof(struct sockaddr). return 0 on success or -1 on failure (when?).
System Call - Connect int connect(int sockfd, struct sockaddr *serv_addr, int addrlen); Used by clients to connect a socket to a server. ‘sockfd’ is the socket file descriptor. ‘struct sockaddr’ contains the destination port and IP address ‘addrlen’ can be set to sizeof(struct sockaddr).
System Call - Listen int listen(int sockfd, int backlog); Define the maximum of pending TCP connections. ‘sockfd’ is the socket file descriptor. ‘backlog is the number of pending connections.
System Call - Accept int accept(int sockfd, void *addr, int *addrlen); The accept system call is used by a TCP server to accept pending connection. Blocked until a connection is arrived. Return a new socket file descriptor that is used to receive and send data on this connection. ‘sockfd’ is the socket file descriptor. ‘addr’ contains information (port and IP address) of the incoming connection. ‘addrlen’ is a local integer variable that should be set to sizeof(struct sockaddr_in).
Data Transfer int send(int sockfd, const void *msg, int len, int flags); Transmit data to through a socket. Can be used only when the socket is connected. Return the number of bytes that has been sent (or -1 in case of error). ‘buf’ is a pointer to the buffer to be sent, while ‘len’ is its length.
Data Transfer int recv(int sockfd, void *buf, int len, unsigned int flags); Obtain data from the socket. Used only when the socket is in a connected state. Return the number of bytes that has been obtailed input (or -1 in case of error). ‘buf’ is a pointer to the buffer for holding the received data and ‘len’ is its length.
Data Transfer int sendto(int sockfd, const void *msg, int len, unsigned int flags, const struct sockaddr *to, int tolen); Transmit data to through a socket. The destination (port and IP address) is specified in the call. Used for UDP communication.
Data Transfer int recvfrom(int sockfd, void *buf, int len, unsigned int flags, struct sockaddr *from, int *fromlen); Obtain data from the socket. ‘sockaddr’ contains information about the originator of the data. Used for UDP communication.
Data Transfer Since ‘send’ and ‘recv’ are used in a connected mode, the usually used in TCP connection while ‘recvfrom’ and ‘sendto’ are used for UDP communication.
Other Services Dealing with IP Addresses DNS ina.sin_addr.s_addr = inet_addr("168.68.37.90"); printf("%s", inet_ntoa(ina.sin_addr)); DNS int gethostname(char* name, int namelen) struct hostent *gethostbyname(char* name)
Client-Server Model UDP Client sd=socket(…) sendto(sd,…) recvfrom(sd,…) close(sd) UDP Server sd=socket(…) bind(sd,…) recvfrom(sd,…) sendto(sd,…) close(sd)
Client-Server Model TCP Client sd=socket(…) connect(sd) send(sd,…) recv(sd,…) close(sd) TCP Server sd=socket(…) bind(sd,…) listen(sd,…) new_sd=accept(sd,…) fork() recv(new_sd,…) send(new_sd,…) close(new_sd)
Port Assignment Ports 0 through 1023 are reserved. In order to let the system to assign the port number, one should set the port number (e.g. in ‘bind’) to zero.