Previous | Next | Trail Map | Custom Networking and Security | Table of Contents


All About Sockets

You use URLs and URLConnections to communicate over the network at a relatively high level and for a specific purpose: accessing resources on the Internet. Sometimes your programs require lower level network communication, for example, when you want to write a client-server application.

In client-server applications, the server provides some service, such as processing database queries or sending out current stock prices. The client uses the service provided by the server to some end: displaying database query results to the user or making stock purchase recommendations to an investor. The communication that occurs between the client and the server must be reliable--no data can be dropped and it must arrive on the client side in the same order that it was sent by the server.

TCP provides a reliable, point-to-point communication channel, which client-server applications on the Internet use to communicate. The Socket and ServerSocket classes in java.net provide a system-independent communication channel using TCP.

What Is a Socket?

A socket is one end-point of a two-way communication link between two programs running on the network. Socket classes are used to represent the connection between a client program and a server program. The java.net package provides two classes--Socket and ServerSocket--that implement the client side of the connection and the server side of the connection, respectively.

Reading from and Writing to a Socket

This page contains a small example that illustrates how a client program can read from and write to a socket.

Writing the Server Side of a Socket

The previous page showed an example of how to write a client program that interacts with an existing server via a Socket object. This page shows you how to write a program that implements the other side of the connection--a server program.


Previous | Next | Trail Map | Custom Networking and Security | Table of Contents