The Echo Server Problem
Contents Basic Networking Concepts The Echo Server Problem
I. Basic Networking Concepts Connection-Oriented Service Port Socket
1. Connection-Oriented Server User establishes a connection and maintains that connection This is in contrast to the Web, where the protocol is transactional – the browser asks for a page, and the server sends it; the connection is then closed
2. Port A port is not a physical device, it is just a number that identifies a specific program on the server Port numbers from 0 to 1023 are reserved for well-known services. Don’t use them for your own server programs! If you’re writing services (server programs) to run on a company network, you should check with the sys-admins to find out which ports are already taken
3. Socket A socket is an abstraction for the network software that enables communication out of and into this program It represents a network connection between two machines It contains two streams, one is for reading data coming in, and the other is for writing data out
II. The Echo Server Problem Develop a simple server that listens to port 8189 and echoes back all client's input
Solution Main Class to Start the Echo Server Developing EchoServer Class to Serve One Client at a Time Improving EchoServer to Serve Multiple Clients at the Same Time
1. Main Class to Start the Echo Server
2. Developing EchoServer Class to Serve One Client at a Time 2.1. Getting Ready to Receive Incoming Connections 2.2. Waiting Indefinitely for a Connection to Come In 2.3. Serving the Client
2.1. Getting Ready to Receive Incoming Connections
2.2. Waiting Indefinitely for a Connection to Come In
2.3. Serving the Client The try-finally Template Checking Whether the Client has Sent a Message Echoing the Message to the Client Returning If the Client Stops Sending
The try-finally Template
Checking Whether the Client has Sent a Message
Echoing the Message to the Client
Returning If the Client Stops Sending
3. Improving EchoServer to Serve Multiple Clients at the Same Time At this moment, the program can serve only one client at a time. Furthermore, after serving that client, the service is closed down 3.1. Making the EchoServer Run Forever 3.2. Serving a Client in a Separate Thread
3.1. Making the EchoServer Run Forever
3.2. Serving a Client in a Separate Thread
The Client Develop a simple client that connects to port 8189 of the server, sends and receive data back from the server.
Solution Main Class to Start the Echo Client Developing EchoClient Class to Send and Receive from the Server
1. Main Class to Start the Echo Client
2. Developing EchoClient Class 2.1. Constructing a Socket to Connect to the Echo Server 2.2. Getting Input and Output Streams of the Socket 2.3. Getting Data from the Console, Sending and Retrieving from the Socket.
2.1. Constructing a Socket to Connect to the Echo Server
2.2. Getting Input and Output Streams of the Socket
2.3. Getting Data from the Console, Sending and Retrieving from the Socket
References Core Java, Volume II – Advanced Features, Eighth Edition, Chapter 3. Cay S. Horstmann and Gary Cornell. Prentice Hall, 2008 Building a Java chat server. Greg Travis. ibm.com/developerWorks