我正在为大学做一个项目,他们让我从不同的客户机向服务器发送消息。他们问我的一件事是计算从客户机第一次连接到服务器到最后一次的时间。这次我想不出一个方法来计算,这就是我的问题所在,我对sockets有点陌生,也许是一些简单的东西。谢谢你的帮助。
服务器
public class Reportero {
public static void main(String[] args) throws IOException {
ServerSocket servidor = null;
Socket sc = null;
DataInputStream in;
DataOutputStream out;
int contador = 0;
final int PUERTO = 5000;
try {
servidor = new ServerSocket(PUERTO);
System.out.println("Reportero iniciado.");
while(true) {
sc = servidor.accept();
//Cliente conectado
in = new DataInputStream(sc.getInputStream());
out = new DataOutputStream(sc.getOutputStream());
String mensaje = in.readUTF();
System.out.println(mensaje);
out.writeUTF("Hola desde el reportero.");
contador++;
System.out.println("Cantidad de información recibida: " + contador);
sc.close();
//Cliente desconectado
}
}catch (IOException ex) {
Logger.getLogger(Reportero.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
客户
public class Consumidor {
public static void main(String[] args) throws IOException, TimeoutException {
final String HOST = "127.0.0.1";
final int PUERTO = 5000;
try {
DataInputStream in;
DataOutputStream out;
Socket sc = new Socket(HOST, PUERTO);
in = new DataInputStream(sc.getInputStream());
out = new DataOutputStream(sc.getOutputStream());
out.writeUTF(encryptThisString(m));
//Imprimir mensaje que envía el reportero
//System.out.println(in.readUTF());
sc.close();
}catch (SocketException ex) {
Logger.getLogger(Consumidor.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnknownHostException ex) {
Logger.getLogger(Consumidor.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Consumidor.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
这是我的密码,希望你能帮我。
暂无答案!
目前还没有任何答案,快来回答吧!