I have built this TCP Client Server pair that are supposed to communicate with each other by sending messages back and forth 1000 times. The problem that I have at the moment is that when I run TCPClient2.java, I get an error message telling me that it can not locate the host. Please see my code below. Thanks a lot.
Code:
TCPServerThread2.java:
import java.io.*;
import java.net.*;
public class TCPServerThread2 extends Thread{
int n=10;
private Socket s=null; //this is the client socket which is created when the client connects server
private String st=new String();
private char[] arr=new char[n];
public TCPServerThread2(Socket s){
super("TCPServerThread");
this.s=s;
}
public void run(){
//String i, o;
try{
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
BufferedReader br = new BufferedReader(new InputStreamReader(s.
getInputStream()));
st=br.readLine();
for(int i=0; i<n; i++){ //the receiver receives the message, copies into array
arr[i]=st.charAt(i);
}
pw.write(arr); //the receiver sends message back to sender
public class TCPClient2{
public static void main(String[] args) throws IOException{
Socket socket=null;
PrintWriter pw=null;
BufferedReader br=null;
int n=10;
long time1, time2, ETE, sumETE, avgETE, maxETE;
sumETE=0;
char[] ca=new char[n];
double[] maxETEa=new double[n];
Date d=new Date();
try{
socket=new Socket("192.168.0.101", 8777);
pw=new PrintWriter(socket.getOutputStream(), true);
br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
catch(UnknownHostException e){
System.err.println("Can not locate host");
System.exit(1);
}
catch(IOException e){
System.err.println("Could not establish IO with IPName");
System.exit(1);
}
for(int i=0;i<n; i++){ //create the n character string
ca[i]=' ';
}
for(int i=0; i<1000; i++){
time1 = d.getTime(); //the sender records the current time before sending a message
pw.print(ca); //send n character string to server
br.read(ca); //receive n character string from the server
time2 = d.getTime();
ETE = (time2 - time1) / 2; //End to End Delay calculation
maxETEa[i]=(double)ETE;
sumETE=sumETE+ETE;
System.out.println("iteration "+i+": ETE="+ETE);
}
Arrays.sort(maxETEa); //sort the elements of maxETEa array in ascending order
avgETE=sumETE/1000;
System.out.println("The maximum ETE is: "+maxETEa[999]); //the last element contains the maximum ETE
System.out.println("The average ETE is: "+avgETE);
// Sorting algorithm for maxETE
socket.close();
br.close();
pw.close();
}
}
TCPServer2.java:
import java.io.*;
import java.net.*;
public class TCPServer2{
public static void main(String[] args) throws IOException{
ServerSocket ss=null;
try{
ss=new ServerSocket(8777);
}
catch(IOException e){
System.err.println("Could not listen on port 8777");
System.exit(-1);
}
Socket client=null;
while(true){
try {
new TCPServerThread(ss.accept()).start();
} catch (IOException e) {
System.err.println("Could not connect to client on port 8777");
//System.exit(-1);
}