Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Understanding Sockets in Unix, NT, and Java

By Ken Nordby
2003-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 ActionClient System CallServer
Action
Server System CallDescription
Create socket descriptorsocket( )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/an/aAssociate network address with socketbind( )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/an/aWait for incoming messagelisten( ) 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 serverconnect( )n/an/aConnect( ) establishes a network connection with the server process.
Transmit and receive datawrite( ) read( )Transmit and receive datawrite( ) read( )This is similar to file I/O. The socket connection is duplex.
Terminate connectionclose( )Terminate connectionclose( )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


 | Bookmark
Related Tutorials:
» All about JAXP, Part 1
» Make Database Queries Without the Database
» Load List Values for Improved Efficiency
» 2 Ways To Implement Session Tracking
» A Simple Way to Read an XML File in Java
» Develop Aspect-Oriented Java Applications with Eclipse and AJDT