I have implemented a client-server communication in java. The following are the codes for transferring a set of files :
Server code:
public class TransferServer {
static List<String> filesListInDir = new ArrayList<String>();
static String zipName = "tmp.tar.gz";
public static void main(String[] args) throws IOException, InterruptedException {
// TODO Auto-generated method stub
ServerSocket s1 = new ServerSocket(7104);
System.out.println("Transfer server started");
while (true) {
Socket sckt = s1.accept();
System.out.println("Request received. Please wait..");
zipData();
transferData(sckt);
System.out.println("Data transferred");
}
}
private static void transferData(Socket ts) throws IOException {
FileInputStream fi=new FileInputStream(zipName);
byte b[] = new byte[8000];
fi.read(b, 0, b.length);
OutputStream os = ts.getOutputStream();
os.write(b, 0, b.length);
fi.close();
}
Client code:
public class Fetchmyfile {
static String addr_list="/home/pi/addresslist.txt";
static String zipName = "tmp.tar.gz";
public static void main(String[] args) throws InterruptedException, IOException {
// TODO Auto-generated method stub
trigger();
}
private static void trigger() throws InterruptedException, IOException {
// TODO Auto-generated method stub
String[] hostList = readAddressList(addr_list);
//remove tmp.zip
for (int i = 0; i < hostList.length; i++) {
Runtime r = Runtime.getRuntime();
Process p = null;
try {
p = r.exec("rm /home/pi/combined_data/"+hostList[i]+"/"+zipName);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
p.waitFor();
}
//remove complete
for (int i = 0; i < hostList.length; i++) {
String addr = hostList[i];
TransferClient clientInstance = new TransferClient();
clientInstance.fileCopy(addr, "/home/pi/combined_data/"+addr.trim()+"/tmp.tar.gz");
}
System.out.println("All data has been transferred");
}
private static String[] readAddressList(String addr_list) throws IOException {
FileReader fileReader = new FileReader(addr_list);
BufferedReader bufferedReader = new BufferedReader(fileReader);
List<String> lines = new ArrayList<String>();
String line = null;
while ((line = bufferedReader.readLine()) != null)
{
lines.add(line);
}
bufferedReader.close();
System.out.println("Loaded the host list");
return lines.toArray(new String[lines.size()]);
}
}
public class TransferClient {
public boolean fileCopy(String sensor_node_ip, String filename) throws InterruptedException{
//public static void main(String args[]) throws IOException
{
try {
//String filename = "‎�‎�localfile.zip";
byte b[] = new byte[8000];
Socket s = null;
try {
s = new Socket(sensor_node_ip, 7104);
System.out.println("connection done");
} catch (IOException e) {
System.out.println("Couldn't connect to the server");
return false;
}
InputStream iss = s.getInputStream();
FileOutputStream fr = new FileOutputStream(filename);
iss.read(b, 0, b.length);
fr.write(b, 0, b.length);
fr.close();
s.close();
//unZip(filename);
System.out.println("Tar file recieved from " + sensor_node_ip);
return true;
}
catch (IOException e){
return false;
}
}
}
The problem I am facing is, for a comparatively larger file, the client creates a file of the size of the declared buffer with the expected name. But the file is not readable and being an archived file, cannot be extracted. Whereas, the server actually has the file which is correct by all means. What could be the reason for this. Any suggestions/pointers is highly appreciated.
transferData()method never closes the accepted socket, and neither does anything else. And it's not much of an idea to use the same filename for all the zipping for every client. Under a fault condition that could result in an information leak.