-
TCP programming
Hi,
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
pw.close();
br.close();
s.close();
}
catch(IOException e){}
}
}
TCPClient2.java:
import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.*;
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);
}
//new TCPServerThread(client).start();
ss.close();
}
}
}
-
Where is the code to run TCPServerThread2 ?
-
socket=new Socket("192.168.0.101", 8777);
if there on the same machine use "localhost"
also the server thread only echos the message once before closing the socket, so I don't think the 1000 echos will work...
Last edited by Joe Beam; 01-11-2006 at 02:16 PM.
Similar Threads
-
By scott in forum Careers
Replies: 5
Last Post: 01-25-2008, 01:40 PM
-
By Kallahan in forum Java
Replies: 1
Last Post: 01-20-2003, 08:28 AM
-
By Mikael Nyberg in forum Mobile
Replies: 0
Last Post: 06-26-2001, 04:30 AM
-
By Novosoft in forum web.announcements
Replies: 0
Last Post: 02-14-2001, 03:50 AM
-
By Philip King in forum authorevents.appleman
Replies: 1
Last Post: 04-10-2000, 12:27 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|