【发布时间】:2021-11-10 22:40:41
【问题描述】:
我已经在 java 中实现了客户端-服务器通信。以下是传输一组文件的代码:
服务器代码:
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();
}
客户端代码:
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;
}
}
}
我面临的问题是,对于一个相对较大的文件,客户端创建一个具有预期名称的声明缓冲区大小的文件。但是该文件不可读并且是存档文件,无法提取。然而,服务器实际上拥有无论如何都是正确的文件。这可能是什么原因。任何建议/指针都非常感谢。
【问题讨论】:
-
所以,无论文件长度如何,您都发送 8000 个字节????
-
... 并且您的
transferData()方法永远不会关闭接受的套接字,也不会关闭其他任何东西。对于每个客户端的所有压缩都使用相同的文件名并不是什么好主意。在可能导致信息泄露的故障条件下。 -
非常感谢@MadProgrammer,user207421。那行得通。我对这个想法有错误的概念。现在对我来说很清楚了。另外,为了更好地理解,我正在阅读基本 I/O。顺便说一句,在 MadProgrammer 编写的代码中,客户端的代码实际上是为我最初编写的服务器服务的,反之亦然。谢谢。
标签: java client-server serversocket