Professional Documents
Culture Documents
e.g. ftp
Application Layer
Transport Layer
Transport Layer
Network Layer
e.g. IP
Network Layer
Link Layer
Ethernet
Link Layer
4
Location
Applications (e.g. browser, game, ftp)
Application Programming Interface (API) (e.g. network API)
Link Layer
Network API
Operating system provides Application Programming Interface (API) for network application API is defined by a set of function types, data structures, and constants Desirable characteristics of the network interface
Simple to use Flexible
independent from any application allows program to use all functionality of the network
Standardized
allows programmer to learn once, write anywhere
Sockets
Sockets provide mechanisms to communicate between computers across a network There are different kind of sockets
DARPA Internet addresses (Internet Sockets) Unix interprocess communication (Unix Sockets) CCITT X.25 addresses and many others
Internet Sockets
Support stream and datagram packets (e.g. TCP, UDP, IP) Is Similar to UNIX file I/O API (provides a file descriptor) Based on C, single thread model
connection-oriented two way communication reliable (error free), in order delivery can use the Transmission Control Protocol (TCP) e.g. telnet, ssh, http connectionless, does not maintain an open connection, each packet is independent can use the User Datagram Protocol (UDP) e.g. IP telephony
10
high-order byte
D3
F2
low-order byte
high-order byte
11
Answer:
Example:
Message is: [Hello,512]
48 45 4C 4C 6F 01 00
48 45 4C 4C 6F 01 00
12
They must know the kind of architecture of the sending computer (bad solution, it has not been implemented) Introduction of a network byte order. The functions are:
Note: use for all integers (short and long), which are sent across the network
14
IP Address
written as dotted octets (e.g. 10.0.0.1) 32 bits. Not a number! But often needs to be converted to a 32-bit to use.
Port number
identifies a process on a host 16 bit number
15
Client-Server Architecture
response
Client
request
Server
Client requests service from server Server responds with sending service or error message to client
16
Client
request
Data response
recv() close()
End-of-file notification
send message to server (send() ) receive (recv() ) data from server and process it
18
s_addr;
// 4 bytes
Needs socket descriptor, Buffer containing the message, and Length of the message Can also use write()
22
receive buffer
slide through
Receiving Packets
int receive_packets(char *buffer, int buffer_len, int *bytes_read) { int left = buffer_len - *bytes_read; received = recv(chat_sock, buffer + *bytes_read, left, 0); if (received < 0) { buffer_len buffer perror (recv"); } if (received <= 0) { return close_connection(); } *bytes_read *bytes_read += received; while (*bytes_read > RECORD_LEN) { process_packet(buffer, RECORD_LEN); *bytes_read -= RECORD_LEN; memmove(buffer, buffer + RECORD_LEN, *bytes_read); } return 0; }
accept user connection and create a new socket (accept() ) data arrives from client (recv() ) data has to be send to client (send() )
25
26
bind():
bind() tells the OS to assign a local IP address and local port number to the socket. Many applications let the OS choose an IP address.
Use wildcard INADDR_ANY as local address in this case.
At server, user process must call bind() to assign a port At client, bind() is not required since OS may assign available port and IP address
The server will get the port number of the client through the UDP/TCP packet header
Puts socket in a listening state, willing to handle incoming TCP connection request. Backlog: number of TCP connections that can be queued at the socket.
28
Server Example
#include <stdio.h> #include <string.h> #include <unistd.h> #include <pthread.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> #define MYPORT 3490 // the port users will be connecting to #define BACKLOG 10 // how many pending connections queue will hold int main(void) { int sockfd, new_fd; // listen on sockfd, new connection on new_fd struct sockaddr_in my_addr; // my address information struct sockaddr_in their_addr; // connector's address information int sin_size; if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { exit(1); } perror("socket");
my_addr.sin_family = AF_INET; // host byte order my_addr.sin_port = htons(MYPORT); // short, network byte order my_addr.sin_addr.s_addr = INADDR_ANY; // auto. filled with local IP memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct 29
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { perror("bind"); exit(1); } if (listen(sockfd, BACKLOG) == -1) { perror("listen"); exit(1); } while(1) { // main accept() loop sin_size = sizeof(struct sockaddr_in); if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) { perror("accept"); continue; } printf("server: got connection from %s\n", inet_ntoa(their_addr.sin_addr)); if (send(new_fd, "Hello, world!\n", 14, 0) == -1) perror("send"); close(new_fd); } return 0; } 30
Client Example
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #define PORT 3490 #define MAXDATASIZE 100 int main(int argc, char *argv[]) { int sockfd, numbytes; char buf[MAXDATASIZE]; struct hostent *he; struct sockaddr_in their_addr; // the port client will be connecting to // max number of bytes we can get // at once
if (argc != 2) { fprintf(stderr,"usage: client hostname\n"); exit(1); } if ((he=gethostbyname(argv[1])) == NULL) { // get the host info perror("gethostbyname"); exit(1); } if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); }
31
their_addr.sin_family = AF_INET; // host byte order their_addr.sin_port = htons(PORT); // short, network byte order their_addr.sin_addr = *(struct in_addr *)*he->h_addr_list; // already network byte order memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1){ perror("connect"); exit(1); } if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) { perror("recv"); exit(1); } buf[numbytes] = '\0'; printf("Received: %s",buf); close(sockfd); return 0; }
32
I/O Blocking
socket(); bind() ; listen();
while
accept(); recv() ; send() ;
Suppose 5 connections accepted. Suppose next accept() blocks. Other connections cannot send and receive. Cannot get keyboard input either.
33
advantages
simple application does not consume CPU cycles while waiting
disadvantages
37
Datagram sockets use recvfrom() and sendto() for receiving and sending data
38
Server
$sand>gcc o server server.c $rain>gcc o client client.c
Run
Server
$sand>./server
Client
$rain>./client sand.cise.ufl.edu
39