• Aucun résultat trouvé

tcpcliserv/str_cli11.c

1 #include "unp.h"

2 void

3 str_cli(FILE *fp, int sockfd) 4 {

5 char sendline [MAXLINE], recvline [MAXLINE];

6 while (Fgets(sendline, MAXLINE, fp) != NULL) { 7 Writen(sockfd, sendline, 1);

8 sleep(1);

9 Writen(sockfd, sendline + 1, strlen(sendline) - 1);

10 if (Readline(sockfd, recvline, MAXLINE) == 0)

11 err_quit("str_cli: server terminated prematurely");

12 Fputs(recvline, stdout);

13 } 14 }

7–9 All we have changed is to call writen two times: the first time the first byte of data is written to the socket, followed by a pause of one second, followed by the remainder of the line.

The intent is for the first writen to elicit the RST and then for the second writen to generate SIGPIPE.

If we run the client on our Linux host, we get:

[ Team LiB ]

Table of Contents

UNIX® Network Programming Volume 1, Third Edition: The Sockets Networking API

By W. Richard Stevens, Bill Fenner, Andrew M. Rudoff

Publisher: Addison Wesley Pub Date: November 21, 2003

ISBN: 0-13-141155-1 Pages: 1024

"Everyone will want this book because it provides a great mix of practical experience, historical perspective, and a depth of understanding that only comes from being intimately involved in the field. I've already enjoyed and learned from reading this book, and surely you will too."

-Sam Leffler

The classic guide to UNIX networking APIs... now completely updated!

To build today's highly distributed, networked applications and services, you need deep

mastery of sockets and other key networking APIs. One book delivers comprehensive, start-to-finish guidance for building robust, high-performance networked systems in any environment:

UNIX Network Programming, Volume 1, Third Edition.

Building on the legendary work of W. Richard Stevens, this edition has been fully updated by two leading network programming experts to address today's most crucial standards, implementations, and techniques. New topics include:

POSIX Single UNIX Specification Version 3

IPv6 APIs (including updated guidance on IPv6/IPv4 interoperability) The new SCTP transport protocol

IPsec-based Key Management Sockets

FreeBSD 4.8/5.1, Red Hat Linux 9.x, Solaris 9, AIX 5.x, HP-UX, and Mac OS X implementations

New network program debugging techniques

Source Specific Multicast API, the key enabler for widespread IP multicast deployment linux % tcpclill 127.0.0.1

hi there we type this line

hi there this is echoed by the server here we kill the server child

bye then we type this line

Broken pipe this is printed by the shell

We start the client, type in one line, see that line echoed correctly, and then terminate the server child on the server host. We then type another line ("bye") and the shell tells us the process died with a SIGPIPE signal (some shells do not print anything when a process dies without dumping core, but the shell we're using for this example, bash, tells us what we want to know).

The recommended way to handle SIGPIPE depends on what the application wants to do when this occurs. If there is nothing special to do, then setting the signal disposition to SIG_IGN is easy, assuming that subsequent output operations will catch the error of EPIPE and terminate.

If special actions are needed when the signal occurs (writing to a log file perhaps), then the signal should be caught and any desired actions can be performed in the signal handler. Be aware, however, that if multiple sockets are in use, the delivery of the signal will not tell us which socket encountered the error. If we need to know which write caused the error, then we must either ignore the signal or return from the signal handler and handle EPIPE from the write.

[ Team LiB ]

[ Team LiB ]

Table of Contents

UNIX® Network Programming Volume 1, Third Edition: The Sockets Networking API

By W. Richard Stevens, Bill Fenner, Andrew M. Rudoff

Publisher: Addison Wesley Pub Date: November 21, 2003

ISBN: 0-13-141155-1 Pages: 1024

"Everyone will want this book because it provides a great mix of practical experience, historical perspective, and a depth of understanding that only comes from being intimately involved in the field. I've already enjoyed and learned from reading this book, and surely you will too."

-Sam Leffler

The classic guide to UNIX networking APIs... now completely updated!

To build today's highly distributed, networked applications and services, you need deep

mastery of sockets and other key networking APIs. One book delivers comprehensive, start-to-finish guidance for building robust, high-performance networked systems in any environment:

UNIX Network Programming, Volume 1, Third Edition.

Building on the legendary work of W. Richard Stevens, this edition has been fully updated by two leading network programming experts to address today's most crucial standards, implementations, and techniques. New topics include:

POSIX Single UNIX Specification Version 3

IPv6 APIs (including updated guidance on IPv6/IPv4 interoperability) The new SCTP transport protocol

IPsec-based Key Management Sockets

FreeBSD 4.8/5.1, Red Hat Linux 9.x, Solaris 9, AIX 5.x, HP-UX, and Mac OS X implementations

New network program debugging techniques

Source Specific Multicast API, the key enabler for widespread IP multicast deployment [ Team LiB ]

5.14 Crashing of Server Host

This scenario will test to see what happens when the server host crashes. To simulate this, we must run the client and server on different hosts. We then start the server, start the client, type in a line to the client to verify that the connection is up, disconnect the server host from the network, and type in another line at the client. This also covers the scenario of the server host being unreachable when the client sends data (i.e., some intermediate router goes down after the connection has been established).

The following steps take place:

When the server host crashes, nothing is sent out on the existing network connections.

That is, we are assuming the host crashes and is not shut down by an operator (which we will cover in Section 5.16).

1.

We type a line of input to the client, it is written by writen (Figure 5.5), and is sent by the client TCP as a data segment. The client then blocks in the call to readline, waiting for the echoed reply.

2.

If we watch the network with tcpdump, we will see the client TCP continually

retransmitting the data segment, trying to receive an ACK from the server. Section 25.11 of TCPv2 shows a typical pattern for TCP retransmissions: Berkeley-derived

implementations retransmit the data segment 12 times, waiting for around 9 minutes before giving up. When the client TCP finally gives up (assuming the server host has not been rebooted during this time, or if the server host has not crashed but was unreachable on the network, assuming the host was still unreachable), an error is returned to the client process. Since the client is blocked in the call to readline, it returns an error.

Assuming the server host crashed and there were no responses at all to the client's data segments, the error is ETIMEDOUT. But if some intermediate router determined that the server host was unreachable and responded with an ICMP "destination unreachable' message, the error is either EHOSTUNREACH or ENETUNREACH.

3.

Although our client discovers (eventually) that the peer is down or unreachable, there are times when we want to detect this quicker than having to wait nine minutes. The solution is to place a timeout on the call to readline, which we will discuss in Section 14.2.

The scenario that we just discussed detects that the server host has crashed only when we send data to that host. If we want to detect the crashing of the server host even if we are not actively sending it data, another technique is required. We will discuss the SO_KEEPALIVE socket option in Section 7.5.

[ Team LiB ]

[ Team LiB ]

Table of Contents

UNIX® Network Programming Volume 1, Third Edition: The Sockets Networking API

By W. Richard Stevens, Bill Fenner, Andrew M. Rudoff

Publisher: Addison Wesley Pub Date: November 21, 2003

ISBN: 0-13-141155-1 Pages: 1024

"Everyone will want this book because it provides a great mix of practical experience, historical perspective, and a depth of understanding that only comes from being intimately involved in the field. I've already enjoyed and learned from reading this book, and surely you will too."

-Sam Leffler

The classic guide to UNIX networking APIs... now completely updated!

To build today's highly distributed, networked applications and services, you need deep

mastery of sockets and other key networking APIs. One book delivers comprehensive, start-to-finish guidance for building robust, high-performance networked systems in any environment:

UNIX Network Programming, Volume 1, Third Edition.

Building on the legendary work of W. Richard Stevens, this edition has been fully updated by two leading network programming experts to address today's most crucial standards, implementations, and techniques. New topics include:

POSIX Single UNIX Specification Version 3

IPv6 APIs (including updated guidance on IPv6/IPv4 interoperability) The new SCTP transport protocol

IPsec-based Key Management Sockets

FreeBSD 4.8/5.1, Red Hat Linux 9.x, Solaris 9, AIX 5.x, HP-UX, and Mac OS X implementations

New network program debugging techniques

Source Specific Multicast API, the key enabler for widespread IP multicast deployment [ Team LiB ]

5.15 Crashing and Rebooting of Server Host

In this scenario, we will establish a connection between the client and server and then assume the server host crashes and reboots. In the previous section, the server host was still down when we sent it data. Here, we will let the server host reboot before sending it data. The easiest way to simulate this is to establish the connection, disconnect the server from the network, shut down the server host and then reboot it, and then reconnect the server host to the network. We do not want the client to see the server host shut down (which we will cover in Section 5.16).

As stated in the previous section, if the client is not actively sending data to the server when the server host crashes, the client is not aware that the server host has crashed. (This assumes we are not using the SO_KEEPALIVE socket option.) The following steps take place:

We start the server and then the client. We type a line to verify that the connection is established.

1.

The server host crashes and reboots.

2.

We type a line of input to the client, which is sent as a TCP data segment to the server host.

3.

When the server host reboots after crashing, its TCP loses all information about connections that existed before the crash. Therefore, the server TCP responds to the received data segment from the client with an RST.

4.

Our client is blocked in the call to readline when the RST is received, causing readline to return the error ECONNRESET.

5.

If it is important for our client to detect the crashing of the server host, even if the client is not actively sending data, then some other technique (such as the SO_KEEPALIVE socket option or some client/server heartbeat function) is required.

[ Team LiB ]

[ Team LiB ]

Table of Contents

UNIX® Network Programming Volume 1, Third Edition: The Sockets Networking API

By W. Richard Stevens, Bill Fenner, Andrew M. Rudoff

Publisher: Addison Wesley Pub Date: November 21, 2003

ISBN: 0-13-141155-1 Pages: 1024

"Everyone will want this book because it provides a great mix of practical experience, historical perspective, and a depth of understanding that only comes from being intimately involved in the field. I've already enjoyed and learned from reading this book, and surely you will too."

-Sam Leffler

The classic guide to UNIX networking APIs... now completely updated!

To build today's highly distributed, networked applications and services, you need deep

mastery of sockets and other key networking APIs. One book delivers comprehensive, start-to-finish guidance for building robust, high-performance networked systems in any environment:

UNIX Network Programming, Volume 1, Third Edition.

Building on the legendary work of W. Richard Stevens, this edition has been fully updated by two leading network programming experts to address today's most crucial standards, implementations, and techniques. New topics include:

POSIX Single UNIX Specification Version 3

IPv6 APIs (including updated guidance on IPv6/IPv4 interoperability) The new SCTP transport protocol

IPsec-based Key Management Sockets

FreeBSD 4.8/5.1, Red Hat Linux 9.x, Solaris 9, AIX 5.x, HP-UX, and Mac OS X implementations

New network program debugging techniques

Source Specific Multicast API, the key enabler for widespread IP multicast deployment [ Team LiB ]

5.16 Shutdow n of Server Host

The previous two sections discussed the crashing of the server host, or the server host being unreachable across the network. We now consider what happens if the server host is shut down by an operator while our server process is running on that host.

When a Unix system is shut down, the init process normally sends the SIGTERM signal to all processes (we can catch this signal), waits some fixed amount of time (often between 5 and 20 seconds), and then sends the SIGKILL signal (which we cannot catch) to any processes still running. This gives all running processes a short amount of time to clean up and terminate. If we do not catch SIGTERM and terminate, our server will be terminated by the SIGKILL signal.

When the process terminates, all open descriptors are closed, and we then follow the same sequence of steps discussed in Section 5.12. As stated there, we must use the select or poll function in our client to have the client detect the termination of the server process as soon as it occurs.

[ Team LiB ]

[ Team LiB ]

Table of Contents

UNIX® Network Programming Volume 1, Third Edition: The Sockets Networking API

By W. Richard Stevens, Bill Fenner, Andrew M. Rudoff

Publisher: Addison Wesley Pub Date: November 21, 2003

ISBN: 0-13-141155-1 Pages: 1024

"Everyone will want this book because it provides a great mix of practical experience, historical perspective, and a depth of understanding that only comes from being intimately involved in the field. I've already enjoyed and learned from reading this book, and surely you will too."

-Sam Leffler

The classic guide to UNIX networking APIs... now completely updated!

To build today's highly distributed, networked applications and services, you need deep

mastery of sockets and other key networking APIs. One book delivers comprehensive, start-to-finish guidance for building robust, high-performance networked systems in any environment:

UNIX Network Programming, Volume 1, Third Edition.

Building on the legendary work of W. Richard Stevens, this edition has been fully updated by two leading network programming experts to address today's most crucial standards, implementations, and techniques. New topics include:

POSIX Single UNIX Specification Version 3

IPv6 APIs (including updated guidance on IPv6/IPv4 interoperability) The new SCTP transport protocol

IPsec-based Key Management Sockets

FreeBSD 4.8/5.1, Red Hat Linux 9.x, Solaris 9, AIX 5.x, HP-UX, and Mac OS X implementations

New network program debugging techniques

Source Specific Multicast API, the key enabler for widespread IP multicast deployment [ Team LiB ]

5.17 Summary of TCP Example

Before any TCP client and server can communicate with each other, each end must specify the socket pair for the connection: the local IP address, local port, foreign IP address, and foreign port. In Figure 5.15, we show these four values as bullets. This figure is from the client's perspective. The foreign IP address and foreign port must be specified by the client in the call to connect. The two local values are normally chosen by the kernel as part of the connect function. The client has the option of specifying either or both of the local values, by calling bind before connect, but this is not common.

Figure 5.15. Summary of TCP client/server from client's perspective.

As we mentioned in Section 4.10, the client can obtain the two local values chosen by the kernel by calling getsockname after the connection is established.

Figure 5.16 shows the same four values, but from the server's perspective.

Figure 5.16. Summary of TCP client/server from server's

perspective.

[ Team LiB ]

Table of Contents

UNIX® Network Programming Volume 1, Third Edition: The Sockets Networking API

By W. Richard Stevens, Bill Fenner, Andrew M. Rudoff

Publisher: Addison Wesley Pub Date: November 21, 2003

ISBN: 0-13-141155-1 Pages: 1024

"Everyone will want this book because it provides a great mix of practical experience, historical perspective, and a depth of understanding that only comes from being intimately involved in the field. I've already enjoyed and learned from reading this book, and surely you will too."

-Sam Leffler

The classic guide to UNIX networking APIs... now completely updated!

To build today's highly distributed, networked applications and services, you need deep

mastery of sockets and other key networking APIs. One book delivers comprehensive, start-to-finish guidance for building robust, high-performance networked systems in any environment:

UNIX Network Programming, Volume 1, Third Edition.

Building on the legendary work of W. Richard Stevens, this edition has been fully updated by two leading network programming experts to address today's most crucial standards, implementations, and techniques. New topics include:

POSIX Single UNIX Specification Version 3

IPv6 APIs (including updated guidance on IPv6/IPv4 interoperability) The new SCTP transport protocol

IPsec-based Key Management Sockets

FreeBSD 4.8/5.1, Red Hat Linux 9.x, Solaris 9, AIX 5.x, HP-UX, and Mac OS X implementations

New network program debugging techniques

Source Specific Multicast API, the key enabler for widespread IP multicast deployment The local port (the server's well-known port) is specified by bind. Normally, the server also specifies the wildcard IP address in this call. If the server binds the wildcard IP address on a multihomed host, it can determine the local IP address by calling getsockname after the connection is established (Section 4.10). The two foreign values are returned to the server by accept. As we mentioned in Section 4.10, if another program is execed by the server that calls accept, that program can call getpeername to determine the client's IP address and port, if necessary.

[ Team LiB ]

[ Team LiB ]

Table of Contents

UNIX® Network Programming Volume 1, Third Edition: The Sockets Networking API

By W. Richard Stevens, Bill Fenner, Andrew M. Rudoff

Publisher: Addison Wesley Pub Date: November 21, 2003

ISBN: 0-13-141155-1 Pages: 1024

"Everyone will want this book because it provides a great mix of practical experience, historical perspective, and a depth of understanding that only comes from being intimately involved in the field. I've already enjoyed and learned from reading this book, and surely you will too."

-Sam Leffler

The classic guide to UNIX networking APIs... now completely updated!

To build today's highly distributed, networked applications and services, you need deep

mastery of sockets and other key networking APIs. One book delivers comprehensive, start-to-finish guidance for building robust, high-performance networked systems in any environment:

UNIX Network Programming, Volume 1, Third Edition.

Building on the legendary work of W. Richard Stevens, this edition has been fully updated by two leading network programming experts to address today's most crucial standards, implementations, and techniques. New topics include:

POSIX Single UNIX Specification Version 3

IPv6 APIs (including updated guidance on IPv6/IPv4 interoperability) The new SCTP transport protocol

IPsec-based Key Management Sockets

FreeBSD 4.8/5.1, Red Hat Linux 9.x, Solaris 9, AIX 5.x, HP-UX, and Mac OS X implementations

New network program debugging techniques

Source Specific Multicast API, the key enabler for widespread IP multicast deployment [ Team LiB ]

5.18 Data Format

In our example, the server never examines the request that it receives from the client. The server just reads all the data up through and including the newline and sends it back to the client, looking for only the newline. This is an exception, not the rule, and normally we must worry about the format of the data exchanged between the client and server.