Socket连接,linux客户端向android服务端传输多个文件

客户端代码
 

void sendFiles(int socket, const string& directory) {
	//获取文件数量
	//socket传入socket()函数返回值,directory传入文件夹目录
	try {
		vector<string> fileList;
		DIR* dir = opendir(directory.c_str());
		if (dir) {
			dirent* entry;
			while ((entry = readdir(dir)) != nullptr) {
				if (entry->d_type == DT_REG) {
					fileList.push_back(entry->d_name);
				}
			}
			closedir(dir);
		}
		//文件数量
		int filesCount = fileList.size();
		//发送文件数量
		send(socket, &filesCount, sizeof(filesCount), 0);

		char buffer[BUFFER_SIZE];

		for (const string& filename : fileList) {
			string filePath = directory + "/" + filename;
			FILE* file = fopen(filePath.c_str(), "rb");
			if (file) {

				int fileNameLength = filename.length();
				//发送文件名长度
				send(socket, &fileNameLength, sizeof(fileNameLength), 0);
				//发送文件名
				send(socket, filename.c_str(), fileNameLength, 0);
				cout<<fileNameLength<<endl;
				//cout << "Sending file: " << filename << endl;


				fseek(file, 0, SEEK_END);
				long fileSize = ftell(file);
				rewind(file);
				char fileSizeString[7];
				snprintf(fileSizeString, sizeof(fileSizeString), "%06ld", fileSize);
				//发送文件大小
				send(socket, fileSizeString, 6, 0);
				//发送文件
				while (true) {
					
					int bytesRead = fread(buffer, 1, sizeof(buffer), file);
						   
					if (bytesRead > 0) {
						send(socket, buffer, bytesRead, 0);
										   
					} else {
						break;	
					}
				}

				fclose(file);
				cout << "File sent: " << filename << endl;
			}
		}

		//close(socket);
		//cout<<"close yes"<<endl;
	} catch (exception& e) {
		    cerr << "Exception: " << e.what() << endl;
	}
}

 服务端代码

private static void receiveFiles(Socket servertSocket, File saveDir) {
        try {
            //byte[] buffer = new byte[4096];
            byte[] inputByte = null;
            //int bytesRead;

            InputStream inputStream = serverSocket.getInputStream();

            // 读取文件数量

            byte[] filesCountBytes = new byte[4];

            inputStream.read(filesCountBytes);

            int filesCount = byteArrayToInt(filesCountBytes)/1024/1024/16;


            for (int i = 0; i < filesCount; i++) {
                // 读取文件名长度
                byte[] fileNameLengthBytes = new byte[4];

                inputStream.read(fileNameLengthBytes);
                int fileNameLength = byteArrayToInt(fileNameLengthBytes)/1024/1024/16;

                //读取文件名
                byte[] fileNameBytes = new byte[fileNameLength];
                inputStream.read(fileNameBytes);
                String fileName = new String(fileNameBytes);
                //读取文件大小
                byte[] fileSizeBytes = new byte[6];
                inputStream.read(fileSizeBytes);

                int fileSize =Integer.parseInt(new String(fileSizeBytes));
//                System.out.println(fileSize);


                Log.i("Server", "Receiving file: " + fileName);

                //保存文件
                File file = new File(saveDir, fileName);
                FileOutputStream fos = new FileOutputStream(file);
                BufferedOutputStream bos = new BufferedOutputStream(fos);


                int length=0;
                while (fileSize > 0) {
                    if (new DataInputStream(inputStream) != null) {
                        if(fileSize-1024<0)
                            inputByte = new byte[fileSize];
                        else
                            inputByte = new byte[1024];
                        length = inputStream.read(inputByte, 0, inputByte.length);

                    }
                    if (length == -1) {
                        break;
                    }
                    bos.write(inputByte, 0, length);
                    fileSize -= length;
                    bos.flush();

                }


                bos.close();
                fos.close();

                Log.i("Server", "File saved: " + file.getAbsolutePath());

            }

            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值