Hi, I'm new to
java, I'm doing a java project now, it has several clients machines and 5 server machines, each client will send same timestamp to server, server will reply their own timestamps to clients, these timestamps are all defined by myself, I've done this step, and now I want to find the maximum timestamps returned from the 5 servers. I defined an array ts to collect the timestamps returned from the servers and an inner class max to compute the maximum value, but when i run this program, after the 1st server returned a timestamp to the client, then the client side shows:java Client
Client is running
1
Timestamp returned from server :4
Exception in
thread "main" java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:323)
at java.io.DataInputStream.readUTF(DataInputStream.java:572)
at java.io.DataInputStream.readUTF(DataInputStream.java:547)
at Client.main(Client.java:50)
then it is jumped out, I'm not sure what's wrong with it, could anyone help me? Many thanks! Here are my Client and Server code.
//Client
import java.io.*;
import java.net.*;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Client
{
public static void main(
String arg[])throws IOException
{
String[] ts;
ts = new String[10];
System.out.println("Client is running");
List<String> ls=new ArrayList<String>();
ls.add("net01.utdallas.edu");
ls.add("net02.utdallas.edu");
ls.add("net03.utdallas.edu");
ls.add("net04.utdallas.edu");
ls.add("net05.utdallas.edu");
for(int i=0;i<ls.size();i++)
{
Socket s=new Socket(ls.get(i),2222); //connect to five servers
//send data to server
OutputStream os=s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
dos.writeUTF("timestamp from client : "+br.readLine());
//read data from server
InputStream is = s.getInputStream();
DataInputStream dis = new DataInputStream(is);
System.out.println(dis.readUTF());
ts[i]=dis.readUTF();
}
// max
//System.out.println(max(ts));
}
public static int max(int[] y){
int maximum = y[0]; // start with the first value
for (int i=1; i<y.length; i++) {
if (y[i] > maximum) {
maximum = y[i]; // new maximum
}
}
return maximum;
}
//Server
import java.net.*;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
public class Server
{
public static void main(String arg[])throws Exception
{
ServerSocket ss=new ServerSocket(2222);
System.out.println("Server is running");
while(true){
server1 w;
w = new server1(ss.accept());
Thread t = new Thread(w);
t.start();
}
}
}
class server1 implements Runnable {
Socket client;
server1(Socket client) {
System.out.println("Waiting for timestamp from client");
this.client = client;
}
public void run(){
//read data from client
try {
InputStream is= client.getInputStream();
DataInputStream dis=new DataInputStream(is);
System.out.println(dis.readUTF());
//send data to client
OutputStream os = client.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
dos.writeUTF("Timestamp returned from server :" +br.readLine());
dis.close();
}
catch(Exception e) {
}
}
}