Table of contents
- Preface
- General background information
- Discussion and sample code
- Run the programs
- Miscellaneous
- Complete program listings
Preface
This module is one in a collection of modules designed for teaching INEW 2338 Advanced Java (Web) at Austin Community College in Austin, TX. More specifically, it is one in asub-collection of modules designed for teaching network programming in that course. The purpose of this module is to introduce the student to the Socket class.
Viewing tip
I recommend that you open another copy of this module in a separate browser window and use the following links to easily find and view the Figuresand Listings while you are reading about them.
Figures
- Figure 1 . Successful daytime query.
- Figure 2 . Output for a successful echo test.
- Figure 3 . Daytime program output.
- Figure 4 . Output from the simple browser program.
Listings
- Listing 1 . Beginning of the program named Java4660a.
- Listing 2 . Instantiate a Socket object.
- Listing 3 . Get I/O stream objects.
- Listing 4 . Send a line of text to the server and display the echo.
- Listing 5 . Beginning of the program named Java4660b.
- Listing 6 . Get an input stream.
- Listing 7 . Read and display incoming data.
- Listing 8 . Beginning of the program named Java4660c.
- Listing 9 . Create input and output streams.
- Listing 10 . Send a GET command.
- Listing 11 . Read and display text from the server.
- Listing 12 . The program named Java4660a.
- Listing 13 . The program named Java4660b.
- Listing 14 . The program named Java4660c.
General background information
Sockets in Java come in at least three varieties that are implemented by the following Java classes
- Socket
- DatagramSocket
- ServerSocket
The first two socket classes represent TCP and UDP communications respectively.
Generally, these two socket classes are used to implement both clients and servers, while the ServerSocket class is only used to implement servers.
This module will concentrate on the use of the Socket class. The DatagramSocket class will be covered a future module. In the meantime, you can read about the DatagramSocket class and other interesting topics in my earlier publications at:
- Network Programming - Server Sockets
- Network Programming - Datagram Clients
- Network Programming - Datagram Servers
- Network Programming - Stubs, Skeletons, and Remote Objects
- Wireless Home Security and Java
In addition, you may find a great deal of useful information in my older publications that are accessible at Programming Oldies But Goodies .
A low-level mechanism
Socket programming provides a low-level mechanism by which you can connect two computers for the exchange of data. One of those is generally considered tobe the client while the other is considered to be the server .
The client initiates a connection with a server. Servers wait for a clients to initiate connections.
A mutually acceptable application protocol
The governing protocol will determine what happens after the connection is made. In order for two computers to communicate effectively, they must eachimplement some mutually acceptable application protocol.