Sockets Socket Berkeley Software Distribution Handlelike data structure
Sockets
Socket • Berkeley Software Distribution • Handle-like data structure for communicating • A socket is an endpoint w Send and receive w Attach a protocol § UDP § TCP user datagram (best effort) transmission control (reliable stream)
Sockets – Connection-Oriented
Sockets • Sockaddr_in w struct sockaddr_in { sin_family; u_short sin_port; struct inaddr sin_addr; sin_zero[8]; }; short char
A situation • Client can determine IP address of server w But how can it know the socket id? § Socket is a handle § Name server can’t deal with all the handles • Bind provides a way to map a socket to a port that exists in the network name space.
Client-Server • Client w w Create the socket Get the address of the server Fill in the sockaddr_in structure Connect to server • Server w w w Create the socket Fill in the sockaddr_in structure Bind to a port Listen Accept connections
Sockets • Created by OS. w int socket(int af, int type, int protocol) § af § type § protocol AF_INET SOCK_STREAM or SOCK_DGRAM 0 (determined by type)
Client filling in sockaddr_in • • char *server. Host. Name = “orion-16”; struct sockaddr_in addr; memset(&addr, 0, sizeof(SOCKADDR_IN)); addr. sin_family = AF_INET addr. sin_port = htons((u_short) port) struct hostent *host; host = gethostbyname(server. Host. Name); memcpy(&addr. sin_addr, host->h_addr_list[0], host->h_length);
Server filling in sockaddr_in • • • struct sockaddr_in addr; memset(&addr, 0, sizeof(SOCKADDR_IN)); addr. sin_family = AF_INET addr. sin_port = htons((u_short) port) addr. sin_addr. s_addr = INADDR_ANY
Server • Map to the network port w int bind(int sock, const struct sockaddr *name, int namelen) § name is pointer to sockaddr_in structure from previous § namelen is size of sockaddr_in • Set socket to listen mode w int listen(int sock, int backlog) § backlog max number of pending connections
Connections • Client initiate a connection w int connect(int sock, const struct sockaddr *name, int namelen); • Server accepting a connection w SOCKET accept(int sock, struct sockaddr *addr, int *addrlen); § creates a new socket for the communication § Server is free to accept another connection on that socket § best to fire off a thread to handle the connection. • send the new socket as an argument to the thread.
Socket Communication • Sending data w send(int sock, char *buffer, int bufflen, int flags) § flags is generally 0 • Receiving data w recv(int sock, char *buffer, int bufflen, int flags) § Make sure you have enough room § flags is generally 0
Socket Overview Server Client sc=socket(. . ) ss=socket(. . ) bind(ss, . . ) listen(ss, . . ) connect(sc, . . ) write(sc, buf, len) foo=accept(ss, . . ) read(foo, buf, len)
Socket Operations Creating a socket int socket(int domain, int type, int protocol) domain=PF_INET, PF_UNIX type=SOCK_STREAM, SOCK_DGRAM Passive open on server int bind(int socket, struct sockaddr *address, int addr_len) int listen(int socket, int backlog) int accept(int socket, struct sockaddr *address, int *addr_len) Active open on client int connect(int socket, struct sockaddr *address, int addr_len) Sending and receiving messages int write(int socket, char *message, int msg_len, int flags) int read(int socket, char *buffer, int buf_len, int flags)
#include <sys/types. h> #include <sys/socket. h> client() { int skt; struct sockaddr_in name; skt = socket(AF_INET, SOCK_STREAM, 0); // Fill in the name data structure sockaddr_in connect(skt, &name, sizeof(name)); // Communicate using send and recv close(skt); }
#include <sys/types. h> #include <sys/socket. h> server() { SOCKET listen. Skt, new. Skt; struct sockaddr_in server. Name, client. Name; listen. Skt = socket(AF_INET, SOCK_STREAM, 0); //Fill in server. Name bind(listen. Skt, &server. Name, sizeof(server. Name)); listen(listen. Skt, 5); new. Skt = accept(listen. Skt, &client. Name, sizeof(client. Name)); // Fire off a thread to do communication using send and recv on new. Skt // Loop back and accept another connection close(skt); }
- Slides: 16