1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
package tcp;
import java.io.*;
import java.net.*;
public class TopologyUDPClient {
private int clientport;
private String serverhost;
private DatagramSocket socket;
/**
* Constructor: TopologyUDPClient: to make a TopologyUDPClient object
* @param host: <B>The server host name</B>
* @param port: <B>The server port number</B>
*/
public TopologyUDPClient(String host, int port){
clientport = port;
serverhost = host;
}
/**
* Method startclient: to start client operation
*/
public void startclient(){
long thelongtimer=-999;
try{
//Get a datagram socket
socket = new DatagramSocket();
//Start a timer to analyze the connection time
TopologyTimer timeranalyze = new TopologyTimer();
timeranalyze.start();
/////////////////////////////
// File filetoread = new File("md5summer.exe");
// RandomAccessFile lecture = new RandomAccessFile(filetoread,"r");
// byte[] buf = new byte[(int)lecture.length()];
// for(int i = 0 ; i < lecture.length() ; i++) {
// buf[i] = lecture.readByte();
// }
////////////////////////////
//Send request
byte[] buf = new byte[256];//A modifier pour taille fichier
InetAddress address = InetAddress.getByName(serverhost);
String messagetosend="Sending a very lon string to analyze the connection quality" +
"test test test test test test test test test test test test test " +
"test test test test test test test test test test test test test " +
"test test test test test test test test test test test test test " +
"test test test test test test test test test test test test test " +
"test test test test test test test test test test test test test " +
"test test test test test test test test test test test test test " +
"test test test test test test test test test test test test test " +
"test test test test test test test test test test test test test " +
"test test test test test test test test test test test test test ";
buf=messagetosend.getBytes();
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, clientport);
socket.send(packet);
//Get response
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
//Display response
String received = new String(packet.getData());
System.out.println("UDP server answer: " + received.substring(0,7));
//Terminate timer analyze
timeranalyze.end();
long timervalue;
if ((timervalue= timeranalyze.duration())>=0){
thelongtimer=timervalue;
}
System.out.println("Timer: "+ thelongtimer+"ms");
} catch(IOException ioe){
System.out.println("Client UDP error: can't create UDP connection!");
}
finally{
try{
socket.close();
} catch(Exception e){
System.out.println("Client UDP error: can't close connection!");
}
}
}
} |
Partager