What is a Client Tester?

Submitted by: Administrator
Example is a program called ClientTester that runs on a port specified on thecommand-line, shows all data sent by the client, and allows you to send a response to the client by typing it on the command line. For example, you can use this program to see the commands that Netscape Navigator sends to a server.

NOTE: Clients are rarely as forgiving about unexpected server responses as servers are about unexpected client responses. If at all possible, try to run the clients that connect to this program on a Unix system or some other platform that is moderately crash-proof. Don't run them on a Mac or Windows 98, which are less stable.

This program uses two threads: one to handle input from the client and the other to send output from the server. Using two threads allows the program to handle input and output simultaneously: it can be sending a response to the client while receiving a request--or, more to the point, it can send data to the client while waiting for the client to respond. This is convenient because different clients and servers talk in unpredictable ways. With some protocols, the server talks first; with others, the client talks first. Sometimes the server sends a one-line response; often, the response is much larger. Sometimes the client and the server talk at each other simultaneously. Other times, one side of the connection waits for the other to finish before it responds. The program must be flexible enough to handle all these cases. Example shows the code.

Example : A Client Tester

import java.net.*;
import java.io.*;
import com.macfaq.io.SafeBufferedReader;
//


public class ClientTester {

public static void main(String[] args) {

int port;

try {
port = Integer.parseInt(args[0]);
}
catch (Exception e) {
port = 0;
}

try {
ServerSocket server = new ServerSocket
(port, 1);
System.out.println
("Listening for connections on port "
+ server.getLocalPort( ));

while (true) {
Socket connection = server.accept( );
try {
System.out.println
("Connection established with "+ connection);
Thread input = new InputThread
(connection.getInputStream( ));
input.start( );
Thread output
= new OutputThread
(connection.getOutputStream( ));
output.start( );
// wait for output and input to finish
try {
input.join( );
output.join( );
}
catch (InterruptedException e) {
}
}
catch (IOException e) {
System.err.println(e);
}
finally {
try {
if (connection !=
null) connection.close( );
}
catch (IOException e) {}
}
}
}
catch (IOException e) {
e.printStackTrace( );
}

}

}

class InputThread extends Thread {

InputStream in;

public InputThread(InputStream in) {
this.in = in;
}

public void run( ) {

try {
while (true) {
int i = in.read( );
if (i == -1) break;
System.out.write(i);
}
}
catch (SocketException e) {
// output thread closed the socket
}
catch (IOException e) {
System.err.println(e);
}
try {
in.close( );
}
catch (IOException e) {
}

}

}

lass OutputThread extends Thread {

Writer out;

public OutputThread(OutputStream out) {
this.out = new OutputStreamWriter(out);
}

public void run( ) {

String line;
BufferedReader in
= new SafeBufferedReader
(new InputStreamReader(System.in));
try {
while (true) {
line = in.readLine( );
if (line.equals(".")) break;
out.write(line +"
");
out.flush( );
}
}
catch (IOException e) {
}
try {
out.close( );
}
catch (IOException e) {
}

}

}
Submitted by: Administrator

Read Online Java Network programming Job Interview Questions And Answers