ICT374 Network Programming with Sockets
_____________________________________________________________________________________
Network Programming with Sockets Objectives
• HaveageneralunderstandingoftheTCP/IPprotocolsuite.
Copyright By PowCoder代写 加微信 powcoder
• Understanddifferentmodesofcommunicationservices.
• Knowthedifferencesbetweenconnection-orientedand
connectionless communication services.
• Understandandbeabletousethebasicsocketinterface,
including commonly used functions.
• UnderstandandbeabletouseUnixdomainprotocoltosolve
inter-process communication problems.
• Beawareofdifferentbyteordersforstoringnumbersandbe
able to convert between them.
• Understandandbeabletouseinternetsocketaddresses.
• Understandandbeabletowriteconcurrentservers.
• Understandandbeabletohandleterminatedchildserver
processes.
• Understandthedifferencesbetweenbytestreamsand
• BeabletohandlesimpleerrorrecoveryinUDPprograms
• BeabletouseTCPtodesignandimplementnetwork
applications.
• BeabletouseUDPtodesignandimplementnetwork
applications.
• Thislecturenotes
• Stevens&Rago:Ch16
• Skim:Stallings:Chapter13
Page 1 / 72
ICT374 Network Programming with Sockets
_____________________________________________________________________________________
Notes to Students
The first part of this lecture covers some essential data communication concepts. If you are already familiar with these concepts, you may skip these sections and start from Section 5 on the socket interface.
The rest of the lecture evolves around 8 examples. The first six examples deal with the same problem – provide a reverse string service. We start from a very crude example (Example 1) containing the bare minimum of features. We then improve each example progressively by identifying a few problems and then solving them in each example.
The first three examples use the Unix domain sockets to communicate between the client and the server. The Unix domain sockets allow a client and its server that are running on the same computer to communicate with each other.
The remaining examples use the TCP or UDP protocol. It is important that you read through each example and understand how each program works. Parts of these examples may be used in your project (Project 2 on simple file transfer protocol, or Project 3 on a simple HTTP client and server).
Page 2 / 72
ICT374 Network Programming with Sockets
_____________________________________________________________________________________
1. TCP/IP Protocol Suite
TCP: Transmission Control Protocol. This is a connection- oriented protocol that provides a reliable (sequencing, flow control, error control), full-duplex, byte-stream for a user process. Most Internet application programs use TCP. Since TCP uses IP, the entire Internet Protocol Suite is often called the TCP/IP protocol family.
UDP: User Datagram Protocol. This is a connectionless protocol for user processes. Unlike TCP, which is a reliable protocol, there is no guarantee that UDP datagrams will ever reach their intended destinations.
ICMP: Internet Control Message Protocol. The protocol handles error and control information between gateways and hosts. While ICMP messages are transmitted using IP datagrams, these messages are normally guaranteed and processed by the TCP/IP networking software itself, not user processes.
Page 3 / 72
ICT374 Network Programming with Sockets
_____________________________________________________________________________________
IP: Internet Protocols: IP is the protocol that provides packet delivery services for TCP, UDP, and ICMP. Note that normally a user process does not directly use the IP protocol.
ARP: Address Resolution Protocol. This protocol maps an Internet address into a hardware address, such as an Ethernet address.
RARP: Reverse Address Resolution Protocol. This protocol maps a hardware address (e.g., Ethernet address) into an Internet address.
Page 4 / 72
ICT374 Network Programming with Sockets
_____________________________________________________________________________________
2. Modes Of Communication Services
(1) Connection-Oriented versus Connectionless
A connection-oriented service requires two processes establishing a logical connection with each other before communication taking place.
• connectionestablishment
• datatransfer
• connectiontermination
A connectionless service is also called a datagram service. In this type of services, messages are transmitted from one system to the other. Since each message is transmitted independently, it must contain all the information required for its delivery.
• Connection-oriented: -telephonecalls • Connectionless: -sendingletters
Internet Protocols:
TCP – connection-oriented UDP – connectionless
(2) Sequencing
Sequencing describes the property that data are received in the same order with which they were sent.
In a packet-switched network, two consecutive packets can take different routes from the source computer to the destination computer, thus arriving at their destination in a different order from the order in which they were sent.
TCP – providing sequencing UDP – no sequencing
Page 5 / 72
ICT374 Network Programming with Sockets
_____________________________________________________________________________________
Error Control
Error control guarantees that error-free data will be delivered to the receiving process. There are two types of lower-level errors:
• Datagetcorruptedduringtransmission(mayusethe checksum to detect).
• Packetsgetlostduringtransmission(maywaitfor acknowledgment to detect)
The two protocols in the transport layer provide the lower- level error control. Once a message is received, you can assume it is error-free. However, UDP does not guarantee that there is no message loss.
TCP – providing error control
UDP – no guarantee that messages will be delivered
Flow Control
Flow control ensures that the sender does not overwhelm the receiver by sending data at a rate faster than the receiver can process the data. If flow control is not provided, the receiver may lose data due to a lack of resources.
TCP – with flow control UDP – without flow control
Byte-Stream versus Messages
A byte-stream service does not provide any message boundaries to the data stream. The converse of this feature is a message-oriented service that preserves the sender’s message boundaries for the receiver.
TCP – byte-stream protocol
UDP – message-oriented protocol
Page 6 / 72
ICT374 Network Programming with Sockets
_____________________________________________________________________________________
3. Identifying a Process on the Internet
In a network application, two processes (usually a client and a server) may run on two different hosts (i.e., computers) located in two different networks linked together by an internet. To identify a process on an internet, we need to identify the socket attached to the process. A socket is identified by:
(1) Network ID, to identify the network
(2) Host ID, to identify the host within the network
(3) Port Number, to identify the process running on the host
In TCP/IP (v4), the network ID and the host ID are specified together in a single 32-bit integer. This 32-bit integer is also known as IP number or IP address. The port number is a 16-bit integer. The port spaces for TCP and UPD are separate. For example, TCP port 23 and UDP port 23 are two different ports. IPv6 address consists of 128 bits, of which the first half are used to identify the network and the second half are used to identify the host in the network.
An internet host is often referred to by its domain name, such as ceto.murdoch.edu.au, rather than by its IP number. When the domain name of a host is used, the network program must resolve to its IP number using the domain name resolver gethostbyname. The name resolver will first check the file /etc/hosts to find out the IP number of the host. Failing that, it will request the DNS to resolve the name.
The servers of most well-known applications (such as ssh and ftp) have a “well-known” port number. E.g.,
ssh 22/TCP telnet 23/TCP ftp 21/TCP smtp 25/TCP
Page 7 / 72
ICT374 Network Programming with Sockets
_____________________________________________________________________________________
talk 517/UDP httpd 80/TCP
Port Numbers between 1 and 1023 are reserved for servers of well-known applications. These well-known port numbers are assigned by the Internet Assigned Numbers Authority (IANA). On Unix systems, only processes with superuser privileges may use a port between 1 and1023.
The clients of well-known applications can use any unused port above 1023. All the other applications (servers & clients) should use ports above 1023.
Some of the well-known servers and their well-known ports are listed in file /etc/services. A client application may use the server port number directly or use the protocol name defined in file/etc/services. Thelibraryfunctiongetservbynameis then used to find out the corresponding port number from that file.
Page 8 / 72
ICT374 Network Programming with Sockets
_____________________________________________________________________________________
4. Programming Interface
There are two programming interfaces for network programming:
(1) Socket Interface
BSD provides the following system calls for network programming:
socket, bind, connect, listen, accept, read, write, send, recv, sendto, recvfrom, ….
(2) TLI (Transport Layer Interface)
System V provides the following system calls for network programming:
t_open, t_bind, t_connect, t_listen,
t_accept, t_alloc, t_snd, t_rev, ….
Both programming interfaces are available on Unix systems. These programming interfaces allow the use of not only TCP/IP protocol suite but other protocol families as well, such as:
▪ XNS (Xerox Network System)
▪ SNA (IBM System Network Architecture)
Of the two programming interfaces, the socket is more popular (e.g., Windows adopted socket interface). In this unit, we only cover TCP/IP programming using the socket interface.
Page 9 / 72
ICT374 Network Programming with Sockets
_____________________________________________________________________________________
5. Socket Interface
A socket is one end of a communication channel. For two processes to communicate with each other, each process must create a socket and binds an address (e.g., IP number and port number) to that socket. The two sockets must also use the same protocol to communicate with each other. For example:
• UnixDomainProtocol • TCP
Internet Protocol Family
• SPP(SequencedPacketProtocol) • PEX(PacketExchangeProtocol) • IDP(InternetDatagramProtocol)
XNS – Xerox Network Systems
It is important to understand that the socket interface is designed not just for the TCP/IP protocol family. It was designed for several different protocol families. Since different protocol families use different address formats, different data types are used to represent protocol addresses. For example, TCP/IP (IPv4) uses a 32-bit IP number and a 16-bit port number to identify a socket. While XNS uses a 32-bit network ID, a 48-bit host ID, and a 16-bit port number to identify a socket. The Unix domain protocol simply uses a file name to represent a socket. In the following section, we will introduce the socket system calls without going deep into the protocol addresses.
Page 10 / 72
ICT374 Network Programming with Sockets
_____________________________________________________________________________________
6. The socket System Calls #include
#include
int socket(int family, int type,
int protocol);
(a) familyspecifiestheAddressFamily(AF_)or
the Protocol Family (PF_) used in communication:
AF_UNIX(orPF_UNIX) – UnixDomainProtocol AF_INET(orPF_INET) – InternetProtocols(TCP/IP) AF_NS (or PF_NS) – Xerox NS protocols
(b) typespecifiesthetypeofcommunicationservice,i.e., whether it is stream-like (connection-oriented) or datagram- like (connectionless), or it uses the lower-level protocol. The parameter type can be one of the following values:
SOCK_STREAM – SOCK_DGRAM – SOCK_RAW –
stream socket datagram socket raw socket
Not all combinations of family and type are valid. For Unix domain protocol and TCP/IP, the following combinations are valid:
SOCK_STREAM SOCK_DGRAM SOCK_RAW
AF_UNIX AF_INET
X
Page 11 / 72
ICT374 Network Programming with Sockets
_____________________________________________________________________________________
(c) The last argument protocol is usually implied in the first two arguments. Therefore, we usually use the value 0 (for default) for this argument.
When protocol = 0, the implied protocol to be used is given in the following table:
SOCK_STREAM SOCK_DGRAM SOCK_RAW
Unix Domain Protocol TCP Unix Domain Protocol UDP
Examples: Assume the following declarations: int sd1, sd2, sd3, sd4, sd5;
the following statements create various sockets:
sd1 = socket(AF_UNIX, SOCK_STREAM, 0);
sd2 = socket(AF_INET, SOCK_STREAM, 0);
sd3 = socket(AF_INET, SOCK_DGRAM, 0);
sd4 = socket(AF_INET, SOCK_RAW, 0);
sd5 = socket(AF_UNIX, SOCK_DGRAM, 0);
Page 12 / 72
ICT374 Network Programming with Sockets
_____________________________________________________________________________________
7. The bind System Call
The bind system call assigns a name or address to a local
#include
#include
int bind (int sd,
struct sockaddr *addr, int addrlen);
The first argument, sd, is the descriptor of a socket (created with the socket call), the second argument, addr, is a pointer to a protocol-specific address, and the third argument addrlen is the size of the address (the number of bytes).
A server must always register a “well-known” address with the operating system using the bind system call. This effectively says that “any message destined to this address should be given to this socket.”
A client may choose to assign a specific address to its socket using the bind system call. But it is not always necessary.
The type struct sockaddr is a generic representation of a protocol address. The actual type and content of a protocol
address vary considerably from one protocol family to another. We will consider the data types for the Unix domain protocol as well as TCP/IP later. For the time being, it suffices to think that addr is just an address for a socket.
Page 13 / 72
ICT374 Network Programming with Sockets
_____________________________________________________________________________________
8. The connect System Call
With a connection-oriented protocol, the client process must use connect to establish a connection between the local socket and the remote listening socket in the server process. The connect call returns only after the connection between the client and server is established.
With a connectionless protocol, the client may also use the connectsystemcall. Butthemeaningoftheconnectcallis different from that of the connection-oriented protocol. It merely attaches a remote address to the local socket, so that the client can use read and recv calls rather than recvfrom call to receive the messages from the remote address, and use write and send calls rather than sendto call to send messages to the remote address.
The system calls recvfrom and sendto require the socket address of the remote process (either the server or the client).
#include
#include
int connect (int sd,
struct sockaddr * remoteSockAddr, int addrlen);
Page 14 / 72
ICT374 Network Programming with Sockets
_____________________________________________________________________________________
9. The listen System Call
A connection-oriented server must use the system call listen to inform the operating system that it is ready to receive connection requests from its clients via the socket.
int listen (int sd, int backlog);
The argument sd is the listening socket. The argument backlog specifies how many connection requests can be queued by the system while it waits for the server to execute the accept system call. This argument is usually specified as 5.
Page 15 / 72
ICT374 Network Programming with Sockets
_____________________________________________________________________________________
10. The accept System call
After a connection-oriented server called the listen system call, an actual connection request from a client process is waited for by having the server executing the accept system call:
#include
#include
int accept (int sd,
struct sockaddr * peer, int * addrlen);
On return, this system call creates a new socket for the actual exchange of data between the server and the remote client process that made the request. The client address and its size will also be returned through pointers peer and addrlen.
Page 16 / 72
ICT374 Network Programming with Sockets
_____________________________________________________________________________________
11. Sending and Receiving Data
The actual data exchange between the client and the server is done using: write or send, and read or recv calls if the destination/source address is not required. Otherwise use the following system calls sendto and recvfrom.
#include
#include
int send (int sd, char *buf, int nbytes, int flag);
int sendto (int sd, char *buf, int nbytes,
int flag, struct sockaddr *to, int addrlen);
int recv (int sd, char *buf, int nbytes, int flag);
int recvfrom (int sd, char *buf, int nbytes, int flag, sockaddr *from, int *addrlen);
All four system calls return the actual number of bytes written or read, or an error number (negative number).
If no flag is set (i.e., flag=0), recv and send behave identically as read and write respectively. The parameter flag affects
the way data are sent or received. For example, if flag=MSG_PEEK, the recv call will peek through the incoming data without removing it from the incoming data queue.
Page 17 / 72
ICT374 Network Programming with Sockets
_____________________________________________________________________________________
12. The Structure of a Client-Server Pair Connection-Oriented
Connectionless
s = socket(….);
bind(s, &serv_addr,….);
listen(s,…..);
ns = accept(s, ….);
read(ns,……); process message write(ns,…..); ……
close(ns);
s = socket(….);
connect(s, &serv_addr,….);
write(s,…..);
read(s,……); …… close(s);
s = socket (….);
bind (s, &serv_addr,….); while(continue)
recvfrom(s,&cli_addr,….);
process message;
sendto(s, &cli_addr,…..);
s = socket(…);
bind(s, &cli_addr,….); ……
sendto(s, …, &serv_addr, ….);
recvfrom(s, …, &serv_addr,…);
Page 18 / 72
ICT374 Network Programming with Sockets
_____________________________________________________________________________________
13. The Unix Domain Protocol
The Unix Domain Protocol is just an IPC (Interprocess Communication mechanism), rather than a network communication protocol. It allows two processes on the same computer to communicate with each other. The purpose of introducing Unix Domain Protocol is:
(1) Introduce yet another IPC;
(2) Become familiar with socket related system calls;
(3) Understand that the socket interface was designed for
multiple protocol families, not just for TCP/IP.
With a Unix domain socket, its address is a file name, just as with FIFOs. Recall that the socket related system calls we have introduced all use the following generic structure for addresses:
struct sockaddr
u_short sa_family; // address family
char sa_data[14]; // protocol-specific address
The actual type to be used depends on the address family (or protocol family). For Unix Domain Protocol, the following type should be used:
#include
struct sockaddr_un
short sun_family; // AF_UNIX
char sun_path[104]; // socket name
The address is cast back to struct sockaddr for the sake of type compatibility.
Page 19 / 72
ICT374 Network Programming wi
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com