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 application.pkgfinal;
import java.net.*;
import java.io.*;
import java.net.Socket;
public class Clientw {
public static void communication() throws IOException {
String serverHostname = new String ("127.0.0.1");
System.out.println ("Attemping to connect to host " +
serverHostname + " on port 1082.");
Socket ClientSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
ClientSocket = new Socket(serverHostname, 1082);
out = new PrintWriter(ClientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(ClientSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " + serverHostname);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "+ "the connection to: " + serverHostname);
System.exit(1);
}
String requeststoragemessage="Requeststorage,"+123+",10M.txt,"+1;
out.println(requeststoragemessage);
System.out.println("echo: " +requeststoragemessage);
String msgrecu=null;
msgrecu=in.readLine();
System.out.println(msgrecu);
String data=null;
if( msgrecu!=null){
String[] part= msgrecu.split("[,]");
System.out.println("premiere partie du message de validation "+part[0].trim().toString());
System.out.println("premiere partie du message de validation "+part[1].trim().toString());
if (part[0].trim().toString().equalsIgnoreCase("Y"))
{
if (part[1].trim().toString().equalsIgnoreCase("10M.txt"))
{
int compteurpacket= 0;
try{
InputStream ips=new FileInputStream("10M.txt");
InputStreamReader ipsr=new InputStreamReader(ips);
BufferedReader br=new BufferedReader(ipsr);
String ligne;
while ((ligne=br.readLine())!=null)
{
String reponsedata= "Data,"+compteurpacket+","+ligne;
out.println(reponsedata);
compteurpacket++;
}
br.close();
out.close();
}
catch (Exception e){
System.out.println(e.toString());
}
}
}
}
out.close();
in.close();
ClientSocket.close();
}
Clientw ()
{
}
} |
Partager