JDK (Java Development Kit) Tutorials
Dr. Herong Yang, Version 5.00

Socket Network Communication

This chapter provides tutorial notes and example codes on socket network communication. Topics include introduction to socket communication; creating a server socket and listening for connection requests; creating a client socket and connects to a server socket; creating server application for multiple connections; sample programs: ReverseEchoer.java, SocketClient.java, and ReverseEchoServer.java.

What Is a Socket?

Establishing a Socket Communication Link

ReverseEchoer.java - A Simple Server Socket Application

SocketClient.java - A Simple Client Socket Application

ReverseEchoServer.java - A Multi-Connection Socket Server

Binding Sockets to Specific Ports

Conclusion:

  • JDK java.net.ServerSocket class allows us to create a server socket with bind() and accept() methods.
  • JDK java.net.Socket class allows us to create a client socket with bind() and connect() methods.
  • The "telnet" command program is a nice tool to send socket connection requests to server sockets.
  • To handle multiple connections, the server application must run a separate thread for each new connection.

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

Dr. Herong Yang, updated in 2008
Socket Network Communication