A Concurrent Echo Server Using. Rohan Murty Hitesh Ballani. Client/server socket interaction: TCP (Java) wait for incoming connection request. Concurrent TCP Servers. Writing the Server Side of a Socket (The Java. The server in the client/server pair serves up Knock Knock jokes. Basic Client-Server Programming in Java. A Simple TCP/IP Socket Client. Recently I was working on a project where a non-java program had to be.Knock Knock jokes are favored by children and are usually vehicles for bad puns. They go like this: Server: . The client program is implemented by a single class. Knock. Knock. Client, and is very similar to the. Echo. Client example from the previous section. The server program is implemented by two classes. Knock. Knock. Server and. Knock. Knock. Protocol. Knock. Knock. Server, which is similar to. Echo. Server, contains the main method for the server program and performs the work of listening to the port, establishing connections, and reading from and writing to the socket. The class. Knock. Knock. Protocol serves up the jokes. It keeps track of the current joke, the current state (sent knock knock, sent clue, and so on), and returns the various text pieces of the joke depending on the current state. This object implements the protocol—the language that the client and server have agreed to use to communicate. The following section looks in detail at each class in both the client and the server and then shows you how to run them. The Knock Knock Server. This section walks through the code that implements the Knock Knock server program. Knock. Knock. Server. The server program begins by creating a new. Server. Socket object to listen on a specific port (see the statement in bold in the following code segment). When running this server, choose a port that is not already dedicated to some other service. For example, this command starts the server program Knock. Knock. Server so that it listens on port 4. Socket Communications. The client program expects an echo from the server and prints the.
Knock. Knock. Server 4. The server program creates the Server. Socket object in a try- with- resources statement. Number = Integer. Int(args. The constructor for Server. Socket throws an exception if it can't listen on the specified port (for example, the port is already being used). In this case, the Knock. Knock. Server has no choice but to exit. If the server successfully binds to its port, then the Server. Socket object is successfully created and the server continues to the next step—accepting a connection from a client (the next statement in the try- with- resources statement). Socket = server. Socket. When a connection is requested and successfully established, the accept method returns a new. Socket object which is bound to the same local port and has its remote address and remote port set to that of the client. The server can communicate with the client over this new Socket and continue to listen for client connection requests on the original Server. Socket This particular version of the program doesn't listen for more client connection requests. However, a modified version of the program is provided in. Supporting Multiple Clients. After the server successfully establishes a connection with a client, it communicates with the client using this code. Step 2 is shown in bold and is worth a few comments. The bold statements in the code segment above initiate the conversation with the client. The code creates a Knock. Knock. Protocol object—the object that keeps track of the current joke, the current state within the joke, and so on. After the Knock. Knock. Protocol is created, the code calls Knock. Knock. Protocol's process. Input method to get the first message that the server sends to the client. For this example, the first thing that the server says is ! As long as the client and server still have something to say to each other, the server reads from and writes to the socket, sending messages back and forth between the client and the server. The server initiated the conversation with a ! The read. Line method waits until the client responds by writing something to its output stream (the server's input stream). When the client responds, the server passes the client's response to the Knock. Knock. Protocol object and asks the Knock. Knock. Protocol object for a suitable reply. The server immediately sends the reply to the client via the output stream connected to the socket, using a call to println. If the server's response generated from the Knock. Knock. Server object is . This class keeps track of where the client and the server are in their conversation and serves up the server's response to the client's statements. The Knock. Knock. Protocol object contains the text of all the jokes and makes sure that the client gives the proper response to the server's statements. It wouldn't do to have the client say ! The protocol that your own clients and servers use depends entirely on the communication required by them to accomplish the task. The Knock Knock Client. The. Knock. Knock. Client class implements the client program that speaks to the Knock. Knock. Server. Knock. Knock. Client is based on the Echo. Client program in the previous section. Reading from and Writing to a Socket and should be somewhat familiar to you. But we'll go over the program anyway and look at what's happening in the client in the context of what's going on in the server. When you start the client program, the server should already be running and listening to the port, waiting for a client to request a connection. So, the first thing the client program does is to open a socket that is connected to the server running on the specified host name and port. String host. Name = args. The Knock. Knock. Client example uses the second command- line argument as the port number when creating its socket. This is a remote port number—the number of a port on the server computer—and is the port to which Knock. Knock. Server is listening. For example, the following command runs the Knock. Knock. Client example with knockknockserver. Knock. Knock. Server and 4. Knock. Knock. Client knockknockserver. The client's socket is bound to any available local port—a port on the client computer. Remember that the server gets a new socket as well. If you run the Knock. Knock. Client example with the command- line arguments in the previous example, then this socket is bound to local port number 4. Knock. Knock. Client example. The server's socket and the client's socket are connected. Next comes the while loop that implements the communication between the client and the server. The server speaks first, so the client must listen first. The client does this by reading from the input stream attached to the socket. If the server does speak, it says . Otherwise, the client displays the text to the standard output and then reads the response from the user, who types into the standard input. After the user types a carriage return, the client sends the text to the server through the output stream attached to the socket. To do this, run the server program using the Java interpreter, just as you would any other Java application. Specify as a command- line argument the port number on which the server program listens. Knock. Knock. Server 4. Next, run the client program. Note that you can run the client on any computer on your network; it does not have to run on the same computer as the server. Specify as command- line arguments the host name and the port number of the computer running the Knock. Knock. Server server program. Knock. Knock. Client knockknockserver. If you are too quick, you might start the client before the server has a chance to initialize itself and begin listening on the port. If this happens, you will see a stack trace from the client. If this happens, just restart the client. If you try to start a second client while the first client is connected to the server, the second client just hangs. The next section, Supporting Multiple Clients, talks about supporting multiple clients. When you successfully get a connection between the client and server, you will see the following text displayed on your screen: Now, you must respond with: The client echoes what you type and sends the text to the server. The server responds with the first line of one of the many Knock Knock jokes in its repertoire. Now your screen should contain this (the text you typed is in bold). Server: Knock! The server responds with the punch line. Now your screen should contain this. Server: Knock! If you type y, the server begins again with ! However, multiple client requests can come into the same port and, consequently, into the same Server. Socket. Client connection requests are queued at the port, so the server must accept the connections sequentially. However, the server can service them simultaneously through the use of threads—one thread per each client connection. The basic flow of logic in such a server is this. Two classes compose our solution. KKMulti. Server and. KKMulti. Server. Thread. KKMulti. Server loops forever, listening for client connection requests on a Server. Socket. When a request comes in, KKMulti. Server accepts the connection, creates a new KKMulti. Server. Thread object to process it, hands it the socket returned from accept, and starts the thread. Then the server goes back to listening for connection requests. The KKMulti. Server. Thread object communicates to the client by reading from and writing to the socket. Run the new Knock Knock server KKMulti. Server and then run several clients in succession.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
January 2017
Categories |