© J. Liebeherr 1997 CS 457 - Sockets 1
Network Programming with Sockets
This section is a brief introduction to the basics of net- working programming using the BSD Socket interface on the Unix Operating System.
Processes in Unix
Sockets Stream Sockets Datagram Sockets
System Calls
Process Management in UNIX
• pid = fork();
• Creates a process.
• The calling process is called “parent”, created process is called “child”.
• The child process is identical to the parent pro- cess (One exception: the process number of parent and child are different).
•
fork()returns value
“0”to child and
“child’s pro- cess number”to parent.)
© J. Liebeherr 1997 CS 457 - Sockets 3
Process Management in UNIX
• exit(status);
• Terminates a process and returns status to waiting parent.
• Processes are created by the fork system call.
• The child is an identical copy of the parent (exception: the pro- cess id is different).
A
child A of A
fork()
Process Management in UNIX
• Result: Process structure is hierarchical.
main() {
pid = fork();
if (pid != 0 ) {
/* original process */
printf(“original process prints this !”);
} else {
/* newly created process */
printf(“new process prints this !”);
}
© J. Liebeherr 1997 CS 457 - Sockets 5
BSD Sockets
•
Sockets are endpoints of two-way communication paths between two Unix processes.
•
Sockets are one of many mechanisms in a Unix operating system to establish communication between processes.
•
In our programming assignments, we will make extensive use of sockets to enable processes at different machines to talk to another.
• In fact: Most network applications of the Internet (including: ftp, telnet, mosaic) are written using the
socket interface.
• The abstraction provided by the socket interface is that of a file. A process opens a socket, reads from a socket,
writes to a socket, etc.
Sockets
Process A
Kernel
Network
Process B
Kernel socket
© J. Liebeherr 1997 CS 457 - Sockets 7
Socket Types and Families
•
Stream Sockets:
Bidirectional, reliable, sequenced, and unduplicat- ed data flow.
•
Datagram Sockets:
Bidirectional, but unreliable and without guar- antee of sequence or duplication.
•
Raw Sockets:
Allows direct access to underlying communica- tion protocols. (Requires special privileges.)
Internet Family Sockets
“Families” (Domains) of Sockets:
• UNIX (AF_UNIX)
• INTERNET (AF_INET)
• Other: XNS, IMP etc.
© J. Liebeherr 1997 CS 457 - Sockets 9
Internet Domain Sockets
• Sockets and the Internet protocol suite:
Socket Layer
Network Interface Internet Protocol (IP)
TCP UDP
Stream Sockets Datagram Sockets User Level
Stream Sockets
• Stream Sockets establish a communication path that provides a “connection-oriented”
service.
• The mode of communication between two processes is asymmetric:
•
One process (the “Client”) requests a connection (connect).•
The other process (the “Server”) is waiting for a connection (listen) and accepts connection re- quests (accept).© J. Liebeherr 1997 CS 457 - Sockets 11
Stream Sockets
Client Algorithm:
1. Find the address (=IP address and port number) of the server with which communication is desired.
2. Allocate a socket.
3. Specify that the connection needs an unused protocol port on the local machine.
4. Connect the socket to the server.
5. Communicate with the server (send and receive).
6. Close the connection.
Stream Sockets
Server Algorithm:
1. Create a socket and bind to a well-known address (=IP address and port number) for the service.
2. Place the socket in passive mode, waiting for connections from clients.
3. Accept the next connection request from the socket, and obtain a second socket for the connection.
4. Read and write with client over the second socket.
5. When finished, close the second socket, and continue with Step 3.
© J. Liebeherr 1997 CS 457 - Sockets 13
Stream Sockets
•
Sequence of system calls
:socket connect write read close Client
socket bind listen accept read Server
write close
Datagram Sockets
• “Connectionless” paradigm.
•
Symmetric interface to data exchange.
•
No requirements for connection establishment.
•
Destination address explicitly specified in every
message.
© J. Liebeherr 1997 CS 457 - Sockets 15
Datagram Sockets
Client Algorithm:
1. Find the address (=IP address and port number) of the server with which communication is desired.
2. Allocate a socket.
3. Specify that the connection needs an unused protocol port on the local machine.
4. Specify the server to which messages are sent.
5. Communicate with the server (send and receive).
6. Close the socket.
Datagram Sockets
Server Algorithm:
1. Create a socket and bind to a well-known address (=IP address and port number) for the service.
2. Repeatedly read the next request from a client, and send back the response.
© J. Liebeherr 1997 CS 457 - Sockets 17
Datagram Sockets
•
Sequence of system calls
: socketsendto recvfrom close CLIENT
socket bind recvfrom SERVER
sendto close bind
Socket
s = socket(family, type, protocol);
family: AF_INET for Internet Family.
type: SOCK_STREAM, SOCK_DGRAM, SOCK_RAW
protocol: 0 (for default), other protocol number.
• Creates a socket and returns a socket descriptor s.
• s is an index in the open file table which points to a socket structure.
© J. Liebeherr 1997 CS 457 - Sockets 19
Bind
• To make the socket addressable from another process, a socket must have a name bound to it.
• The bind call is always used by servers, which need to specify a well-known port number. Connectionless clients also need to assure that the system assigns it a unique address.
• Syntax:
bind(s, name, namelen)
name: local address which contains an Internet ad- dress and a port number; name is of type
“sockaddr_in”.
• Port numbers:
• Internet ports below 1024 are reserved for root.
• If bind is called with port number “0”, operating system will as- sign an unused port number.
Connect
• Syntax:
connect(s, servaddr, addrlen)
servaddr: server address which contains an Inter- net address and a port number; servaddr is of type
“sockaddr_in”.
• For stream-oriented connections, results in connection estab- lishment between the local system and the foreign system.
• Assigns association elements: local_addr, local_process, foreign_addr, and foreign_process.
• After connecting, the process can write to or read from the socket without specifying the foreign address.
• A process that uses connect does not need to call bind.
© J. Liebeherr 1997 CS 457 - Sockets 21
Listen
• Syntax:
listen(s, backlog)
backlog: specifies how many outstanding connec- tion requests can be queued while waiting for the server to execute accept.
• Specifies that a connection-oriented server is willing to receive connections.
• Maximum backlog is 5.
Accept
• Syntax:
accept(s, peer, addrlen)
peer: address of the connected peer process (the client).
s:
specifies socket from which a connection should be accepted.
• Used after calling socket to create a socket, bind to specify a local endpoint address, and listen to place it in passive mode.
• Creates a new socket for each new connection request, and returns the descriptor of the new socket to its caller.
• Original Socket: used to accept new connection.
© J. Liebeherr 1997 CS 457 - Sockets 23
Write / Read
• Syntax:
write(s, buff, nbytes)
read(s, buff, nbytes)
s: Socket to use for reading/writing. (akin to file de- scriptor)
• Similar to reading and writing to files.
• Used with connection-oriented sockets (which use the connect and accept system calls).
• Returns the number of bytes read or written.
• Also the send call:
send(s, buff, nbytes, flags)
• The flags specify options. (e.g. MSG_OOB, MSG_PEEK, etc.)
Sendto / Recvfrom
• Syntax:
sendto(s,buff,nbytes,flags,to,adrlen)
recvfrom(s,buff,nbytes,flags,from,adrlen)
to (from): Address to (from) which packet is to be sent.
adrlen
:Size of
to(or
from) argument.
• For connectionless communication: address is specified for each packet sent by sendto.
• For a connectionless server, recvfrom fills in protocol-specific address of who sent the data into from.
• Returns the number of bytes read or written.
© J. Liebeherr 1997 CS 457 - Sockets 25
Types of Servers
iterative
connectionless iterative
connection-oriented
concurrent connection-oriented concurrent
connectionless