Understanding Sockets in Unix, NT, and Java
By Ken Nordby2003-05-26
A simple example
What does a typical sockets application look like? The following table describes a simple connection-oriented client-server sockets application.
| Client Action | Client System Call | Server Action | Server System Call | Description |
| Create socket descriptor | socket( ) | Create socket descriptor | socket( ) | The socket( ) call creates a socket descriptor, which is similar to a file descriptor. The protocol family must be chosen when socket( ) is issued. |
| n/a | n/a | Associate network address with socket | bind( ) | The server socket must be network-addressable. A typical use of bind( ) is to associate the server socket with a specific Internet Protocol (IP) address. |
| n/a | n/a | Wait for incoming message | listen( ) and accept( ) | Listen( ) notifies the operating system that the server process is ready to receive messages. Accept( ) suspends the server process until a message arrives. |
| Contact server | connect( ) | n/a | n/a | Connect( ) establishes a network connection with the server process. |
| Transmit and receive data | write( ) read( ) | Transmit and receive data | write( ) read( ) | This is similar to file I/O. The socket connection is duplex. |
| Terminate connection | close( ) | Terminate connection | close( ) | This is analogous to closing a file. |
This example illustrates the fundamental paradigm of sockets communication: a server system creates a socket, establishes network addressability, and waits for messages. A client system, cognizant of the server's address, sends a connection request. If a connection is established, data flows between client and server.
Basic steps
The basic steps in a typical sockets session can be summarized as follows:
| Server: | Create socket, establish network addressability, wait for connection request | |
| Client: | Create socket, send connection request to server | |
| Server and Client: | Establish connection | |
| Server and Client: | Transmit and receive data | |
| Server and Client: | Close connection. |
Tutorial Pages:
» Understanding Sockets in Unix, NT, and Java
» Basic sockets concepts
» A simple example
» Sample sockets application
» Server program (Unix)
» Client program (NT)
» Client program (Java)
» Further study
First published by IBM DeveloperWorks
