200 likes | 609 Views
An Introduction to Computer Networks. Lecture 3: Sockets. University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani. Outline. Protocol-to-protocol interface. Process model Socket programming What is a Socket? Why do we need sockets? Types of sockets
E N D
An Introductionto Computer Networks Lecture 3: Sockets University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani
Outline • Protocol-to-protocol interface. • Process model • Socket programming • What is a Socket? • Why do we need sockets? • Types of sockets • Uses of sockets • Example applications • Code Introduction to computer network
Protocol-to-Protocol Interface • Configure multiple layers • static versus extensible • Process Model • avoid context switches • Buffer Model • avoid data copies • Use shared buffer among protocol layers Introduction to computer network
(a) (b) Process Model Process-per-Protocol Process-per-Message Introduction to computer network
Socket programming • Goal- Communication between two processes • They use interface/services from the transport layer. The interface is called Application Programming Interface, API. Server process Client process TCP/UDP TCP/UDP Socket API IP IP Ethernet Adaptor Ethernet Adaptor Introduction to computer network
What are Sockets? • It is an API between applications and network • protocol software provided by the OS Functions it provides: – Define an “end- point” for communication – Initiate and accept a connection – Send and receive data – Terminate a connection gracefully • Supports multiple protocol families – Examples: Unix inter- process communication, TCP/ IP – Only Internet sockets will be covered in this lecture – Berkeley Sockets are the most common (from BSD Unix) Introduction to computer network
Type of Sockets • Two different types of sockets : – stream vs. datagram • Stream socket :( a. k. a. connection- oriented socket) – It provides reliable, connected networking service – Error free; no out- of- order packets (uses TCP) – applications: telnet/ ssh, http, … • Datagram socket :( a. k. a. connectionless socket) – It provides unreliable, best- effort networking service – Packets may be lost; may arrive out of order (uses UDP) – applications: streaming audio/ video (realplayer), … Introduction to computer network
Socket usages • Network applications use sockets at some level, often using higher level protocols on top of sockets – File transfer apps (FTP), Web browsers (HTTP), Email (SMTP/ POP3), etc… • Simplify and expedite application development process • Most exploits client-server model. Introduction to computer network
A generic server (FTP) • Wait for connections on a port • When a client connection comes in, loop: – Read in the client’s request – Read data from a file – Send the data to the client – Disconnect when we have reached EOF Introduction to computer network
A generic client, high level • Connect to a given server • loop: – Send a request to the server – Read server’s response – Read server’s data – Disconnect when we have reached EOF Introduction to computer network
Client- Server communication (TCP) server bind() to a receiving port Accept() connection socket() listen () to socket send() recv() client bind() to any port send() recv() socket() connect () to server Introduction to computer network
Socket • Transport protocol needs the following information for multiplexing/demultiplexing. (source port, source address, destination port, destination address) • Socket- the end point of connection. Process read/write from/to socket. A socket is specified by a socket descriptor. struct sockaddr_in { u_char sin_family; /* Address Family */ u_short sin_port; /* Port number */ struct in_addr sin_addr; /* IP address */ char sin zero[8]; /* unused */ }; Introduction to computer network
TCP Server program • Make a socket #include <sys/ types. h> #include <sys/ socket. h> int fd, newfd, nbytes, nbytes2; char buf[512], response[512]; struct sockaddr_in srv; fd = socket(AF_INET, SOCK_STREAM, 0); Introduction to computer network
TCP Server program • The socket was created now bind it to a port and host. srv.sin_family = AF_INET; srv.sin_port = htons(80); srv.sin_addr.s_addr = inet_addr(``128.2.15.9''); bind(fd, (struct sockaddr*) &srv, sizeof(srv)); • Now sit and listen; listen(fd, 5); • Now, accept any connection. First, clear the structure. struct sockaddr_ in cli; int cli_ len; memset(& cli, 0, sizeof( cli)); /* clear it */ newfd = accept(fd, (struct sockaddr*) &cli, &cli len); Introduction to computer network
TCP Server program • Now it can read from socket, newfd, and write to socket, newfd. int BUF_ SIZE = 1024, bytesrecv = 0; char buf[ BUF_ SIZE]; /* receives up to BUF_ SIZE bytes from sock and stores them in buf. */ bytesrecv = recv( newfd, buf, BUF_ SIZE, 0); /* send up BUF_ SIZE bytes */ bytesrecv = send( newfd, buf, BUF_ SIZE, 0); • At the end, we need to close both sockets by close command. close( newfd); /* closes the socket newfd */ Introduction to computer network
TCP client program int fd, newfd, nbytes, nbytes2; char buf[512], response[512]; struct sockaddr_in srv; fd = socket(AF_INET, SOCK_STREAM, 0); The same as server. Now, it needs to connect to server. srv.sin_family = AF_INET; srv.sin_port = htons(80); srv.sin_addr.s_addr = inet_addr(``128.2.15.9''); connect(fd, (struct sockaddr*) &srv, sizeof(srv)); Connect is blocking and send a SYN signal and is blocked until receive SYNACK, (three way handshaking) It can start now reading and writing sprintf(request, ``Here's my request''); nbytes2 = write(fd, request, strlen(response)); close(fd); Introduction to computer network
Client- Server communication (UDP) server recvfrom () sendto() bind() to a receiving port socket() to create socket client socket() to create scoket recvfrom () sendto () bind() to any port Introduction to computer network
UDP Server/client program /* fill in declarations */ fd = socket(AF_INET, SOCK_DGRAM, 0); The socket was created now bind it to a port and host exactly the same way as TCP. Now listen; no connection, start reading from the port by nbytes = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr*) &cli, &cli len); UDP client is the same TCP, except instead of read/write, it uses nbytes = sendto(fd, request, sizeof(request), 0, (struct sockaddr*) &srv, sizeof(srv)); Introduction to computer network