JDK Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.32, 2006

Socket Communication

Part:   1   2  3  4 

JDK Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Internationalization

Character Set and Encoding

Socket Communication

Document Object Model (DOM)

XSD Validation in Java

XSL - Transformer in Java

JCA - Private and Public Key Pairs

JCE - Secret Key

SSL (Secure Socket Layer)

SSL - Client Authentication

... Table of Contents

Notes and sample codes bellow are based on J2SDK 1.4.1_01.

What Is a Socket

Socket: An concept represents one end-point of a two-way communication link between two programs running on the Internet network.

Internet Address: A unique number to identify each computer system on the Internet.

Port: A number representing an entry point of on a computer system on the Internet, where a two-way communication link can be established.

Local System: The computer system at this end of a two-way communication link.

Remote System: The computer system at the other end of a two-way communication link.

A two-way communication link on the Internet involves:

  • Two computer systems, each has its own Internet address.
  • A port number on each computer system that is used only by this communication link.
  • An application program on each computer system that uses this communication link.
  • A socket object created in the application program that represents this communication link.
  • The application program can use the socket as a logical input or output device to receive or send data from or to the application program running at the other end of the communication link.
  • The application program can also query the socket for information about the communication link, like: The Internet address and port number at this end of the communication link; The Internet address and port number at the other end of the communication link.

The follwing diagram illustrates how an application program can use a socket to talk with other application program running on another computer system throught a two-way communication link on the Internet:

Computer System I                            Computer System II
       i/o       |Address a|    Internet    |Address b|       i/o
App. A<--->Socket|Port 1   |<-------------->|   Port 2|Socket<--->App. X

Establishing a Socket Communication

In the previous section, we only discussed about how socket works with a communication link that has been established already. Now, let's see how two application programs can establish a communication link between them.

To establish a communication link, one application program must act as a server, create a server socket with a given port number, and set the server socket in the listen mode waiting for a connection request from other program.

With one program running as a server listening for a connection request at a specific port number, the other program can now create socket with a given local port number, the Internet address of the computer system where the first program is running, and the port number where the server socket is listening. At this time, a connect request will be send over to the server socket. The server socket should then accept the connect request and instantiate a socket object to complete communication link.

J2SDK offers two main classes to support socket communication:

java.net.ServerSocket with methods:

  • bind(): setting the server socket with the local system address, and a given local port number.
  • accept(): listening for a connection request, and instantiating a socket object when the request comes.

java.net.Socket with methods:

  • bind(): setting the socket with the local system addres, and a given local port number.
  • connect(): sending a connection request to a given remote system address, and a given remote port number.

(Continued on next part...)

Part:   1   2  3  4 

Dr. Herong Yang, updated in 2006
JDK Tutorials - Herong's Tutorial Notes - Socket Communication