Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.

Similar presentations


Presentation on theme: "1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets."— Presentation transcript:

1 1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets

2 Univ. of TehranIntroduction to computer network2 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

3 Univ. of TehranIntroduction to computer network3 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

4 Univ. of TehranIntroduction to computer network4 Process Model (a)(b) Process-per-ProtocolProcess-per-Message

5 Univ. of TehranIntroduction to computer network5 Socket programming Goal- Communication between two processes They use interface/services from the transport layer. The interface is called Application Programming Interface, API. Client process Server process TCP/UDP IP Ethernet Adaptor Socket API

6 Univ. of TehranIntroduction to computer network6 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)

7 Univ. of TehranIntroduction to computer network7 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), …

8 Univ. of TehranIntroduction to computer network8 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.

9 Univ. of TehranIntroduction to computer network9 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

10 Univ. of TehranIntroduction to computer network10 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

11 Univ. of TehranIntroduction to computer network11 Client- Server communication (TCP) socket() bind() to a receiving port listen () to socket Accept() connection socket() bind() to any port connect () to server send() recv() send() recv() server client

12 Univ. of TehranIntroduction to computer network12 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 */ };

13 Univ. of TehranIntroduction to computer network13 TCP Server program Make a socket #include int fd, newfd, nbytes, nbytes2; char buf[512], response[512]; struct sockaddr_in srv; fd = socket(AF_INET, SOCK_STREAM, 0);

14 Univ. of TehranIntroduction to computer network14 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);

15 Univ. of TehranIntroduction to computer network15 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 */

16 Univ. of TehranIntroduction to computer network16 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);

17 Univ. of TehranIntroduction to computer network17 Client- Server communication (UDP) socket() to create socket bind() to a receiving port recvfrom () sendto() socket() to create scoket bind() to any port recvfrom () sendto () server client

18 Univ. of TehranIntroduction to computer network18 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));


Download ppt "1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets."

Similar presentations


Ads by Google