











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












CS 241 Spring 2007System Programming
CS241 Administrative
Read Stallings Chapter 13, R&R 18.1-18.
LMP3 (Part I) Due April 23
LMP3 (Part II) Due April 30
Last Quiz will be on Friday, April 27 onNetworking
Connection-Oriented Communications Illustration
Multiple Clients (Serial-server Strategy)
Threaded-server Strategy
thread
What is a socket?
An interface between application and network
The application creates a socketThe socket
type
dictates the style of communication
reliable vs. best effortconnection-oriented vs. connectionless
Once configured, the application can
pass data to the socket for network transmissionreceive data from the socket (transmitted through the
network by some other host)
Socket-Based APIs^ #include <sys/socket.h>^ int socket (int domain, int type, int protocol);^ int bind (int s, const struct sockaddr *address,size_t address_len);^ int listen (int s, int backlog);^ int accept (int s, struct sockaddr *restrict address,int *restrict address_len);
Socket Creation in C: socket int s = socket(domain, type, protocol);
s: socket descriptor, an integer (like a file-handle) domain: integer, communication domain
e.g., AF_INET typically used (Internet domain)
type: communication type
SOCK_STREAM: reliable, 2-way, connection-based service SOCK_DGRAM: unreliable, connectionless, other values: need root permission, rarely used, or obsolete
protocol: specifies protocol (see file /etc/protocols for a list of
options) - usually set to 0 (IP)
NOTE: socket call does not specify where data will be
coming from, nor where it will be going to – it justcreates the interface!
Bind Function int bind(int s, const struct sockaddr *address, size_t
address_len);
Bind function
Associates the handle for a socket communication endpoint with a
specific logical network connection.
Note: Internet domain protocols specify logical connection by a
port number s is the file descriptor returned by socket( ) address
contains info about the family, port and machine
address_len
is the size of the structure used for the address
Socket Structure (1) The format of the address struct sockaddr is
determined by the address family (domain). For AF_INET it is a struct sockaddr_in Socket structure is defined in netinet/in.h Socket structure has at least the following
members in network byte order:
sa_family_t sin_family; in_port_t sin_port; /* Host Port Number / struct in_addr sin_addr; / IP Address*/
Socket Structure: Example struct sockaddr_in server; int sock; server.sin_family = AF_INET; server.sin_addr.s_addr = htonl(INADDR_ANY); server.sin_port = htons((short)8652); if (bind(sock, (struct sockaddr *)&server, sizeof(server)) == -1)
perror(“Failed to bind the socket to port”);
Sending / Receiving Data - UDP
Without a connection (SOCK_DGRAM):
int count = sendto(sock, &buf, len, flags, &addr, addrlen);
count, sock, buf, len, flags: same as send addr: struct sockaddr, address of the destination addrlen: sizeof(addr)
int count = recvfrom(sock, &buf, len, flags, &addr,
&addrlen);
count, sock, buf, len, flags: same as recv name: struct sockaddr, address of the source namelen: sizeof(name): value/result parameter
Calls are
blocking
[returns only after data is sent (to socket
buf) / received]
Summary Client-Server Process Communication
Connection-oriented Server Strategies Implementation of UDP