Contents
5
1.
BSD Sockets Concepts
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
Key Terms and Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
How You Can Use BSD Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20
The Client-Server Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21
Creating a Connection: the Client-Server Model . . . . . . . . . . . . . . . . .21
BSD Sockets Library Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25
2.
Using Internet Stream Sockets
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29
Preparing Address Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .31
Declaring Socket Address Variables . . . . . . . . . . . . . . . . . . . . . . . . . . .31
Getting the Remote Host's Internet Address . . . . . . . . . . . . . . . . . . . .32
Getting the Port Address for the Desired Service . . . . . . . . . . . . . . . .33
Using a Wildcard Local Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .34
Writing the Server Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35
Creating a Socket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35
Binding a Socket Address to the Server Process's Socket . . . . . . . . . .36
Setting Up the Server to Wait for Connection Requests . . . . . . . . . . .37
Accepting a Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38
Writing the Client Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .40
Creating a Socket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .40
Requesting a Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41
Sending and Receiving Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43
Sending Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43
Receiving Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44
Flag Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45
Closing a Socket. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47