PHP 实现文件的writeInt writeUTF readInt readUTF

<?php

class file_operation{
var $file;
function file_operation($file){
$this->file = $file;
}

function write_int($value)
{
    $data = pack('N', $value);
    fwrite($this->file, $data);    
}

function write_utf($value)
{
    $this->write_int(strlen($value));
    fwrite($this->file,$value,strlen($value));
}


function read_int()
{
    $data = fread($this->file, 4);       
    $raw = unpack('N', $data);            
    $val = $raw[1] & 0xffffffff;
    return $val;
}


function read_utf()
{
    $l = $this->read_int();             
    return fread($this->file,$l);        
}
}



$f = fopen("/home/gt/1.txt","wb");
$fo = new file_operation($f);
$fo->write_utf("abcdefg");
$fo->write_int(123456789);

$f2 = fopen("/home/gt/1.txt","rb");

$fo2 = new file_operation($f2);

echo $fo2->read_utf();
echo $fo2->read_int();
网络文件共享系统的设计与实现可以分为以下几个步骤: 1. 设计文件共享协议:定义客户端和服务端之间交互的消息格式和规则。 2. 实现服务端:服务端需要监听客户端的请求,并响应请求。当接收到文件上传请求时,服务端需要将文件保存到指定的位置,并返回上传成功的消息。当接收到文件下载请求时,服务端需要读取指定的文件并将文件数据返回给客户端。 3. 实现客户端:客户端需要向服务端发送请求,并处理服务端响应。当上传文件时,客户端需要将文件数据打包成消息发送给服务端,等待服务端的响应。当下载文件时,客户端需要向服务端发送下载请求,并将接收到的文件数据保存到本地。 以下是一个简单的网络文件共享系统的Java实现文件共享协议: ```java public class Protocol { // 消息类型常量 public static final int UPLOAD_REQUEST = 1; // 上传请求 public static final int UPLOAD_RESPONSE = 2; // 上传响应 public static final int DOWNLOAD_REQUEST = 3; // 下载请求 public static final int DOWNLOAD_RESPONSE = 4; // 下载响应 // 消息格式:消息类型 + 文件名 + 文件数据 public static byte[] encode(int type, String fileName, byte[] data) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); DataOutputStream dataOut = new DataOutputStream(out); dataOut.writeInt(type); dataOut.writeUTF(fileName); dataOut.writeInt(data.length); dataOut.write(data); return out.toByteArray(); } // 解析消息 public static Message decode(byte[] data) throws IOException { ByteArrayInputStream in = new ByteArrayInputStream(data); DataInputStream dataIn = new DataInputStream(in); int type = dataIn.readInt(); String fileName = dataIn.readUTF(); int length = dataIn.readInt(); byte[] fileData = new byte[length]; dataIn.readFully(fileData); return new Message(type, fileName, fileData); } // 消息类 public static class Message { public int type; public String fileName; public byte[] data; public Message(int type, String fileName, byte[] data) { this.type = type; this.fileName = fileName; this.data = data; } } } ``` 服务端: ```java public class Server { private ServerSocket serverSocket; public Server(int port) throws IOException { serverSocket = new ServerSocket(port); } public void start() throws IOException { while (true) { Socket socket = serverSocket.accept(); Thread t = new Thread(new ClientHandler(socket)); t.start(); } } private class ClientHandler implements Runnable { private Socket socket; public ClientHandler(Socket socket) { this.socket = socket; } @Override public void run() { try { InputStream in = socket.getInputStream(); OutputStream out = socket.getOutputStream(); byte[] buffer = new byte[1024]; int len = in.read(buffer); Protocol.Message request = Protocol.decode(Arrays.copyOf(buffer, len)); if (request.type == Protocol.UPLOAD_REQUEST) { // 处理上传请求 FileOutputStream fileOut = new FileOutputStream(request.fileName); fileOut.write(request.data); fileOut.close(); Protocol.Message response = new Protocol.Message(Protocol.UPLOAD_RESPONSE, request.fileName, null); out.write(Protocol.encode(response.type, response.fileName, response.data)); } else if (request.type == Protocol.DOWNLOAD_REQUEST) { // 处理下载请求 FileInputStream fileIn = new FileInputStream(request.fileName); byte[] data = new byte[fileIn.available()]; fileIn.read(data); fileIn.close(); Protocol.Message response = new Protocol.Message(Protocol.DOWNLOAD_RESPONSE, request.fileName, data); out.write(Protocol.encode(response.type, response.fileName, response.data)); } socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } ``` 客户端: ```java public class Client { private String serverHost; private int serverPort; public Client(String serverHost, int serverPort) { this.serverHost = serverHost; this.serverPort = serverPort; } public void upload(String fileName) throws IOException { FileInputStream fileIn = new FileInputStream(fileName); byte[] data = new byte[fileIn.available()]; fileIn.read(data); fileIn.close(); Protocol.Message request = new Protocol.Message(Protocol.UPLOAD_REQUEST, fileName, data); Socket socket = new Socket(serverHost, serverPort); OutputStream out = socket.getOutputStream(); out.write(Protocol.encode(request.type, request.fileName, request.data)); byte[] buffer = new byte[1024]; int len = socket.getInputStream().read(buffer); Protocol.Message response = Protocol.decode(Arrays.copyOf(buffer, len)); if (response.type == Protocol.UPLOAD_RESPONSE) { System.out.println("Upload success: " + response.fileName); } else { System.out.println("Upload failed: " + response.fileName); } socket.close(); } public void download(String fileName) throws IOException { Protocol.Message request = new Protocol.Message(Protocol.DOWNLOAD_REQUEST, fileName, null); Socket socket = new Socket(serverHost, serverPort); OutputStream out = socket.getOutputStream(); out.write(Protocol.encode(request.type, request.fileName, request.data)); byte[] buffer = new byte[1024]; int len = socket.getInputStream().read(buffer); Protocol.Message response = Protocol.decode(Arrays.copyOf(buffer, len)); if (response.type == Protocol.DOWNLOAD_RESPONSE) { FileOutputStream fileOut = new FileOutputStream(response.fileName); fileOut.write(response.data); fileOut.close(); System.out.println("Download success: " + response.fileName); } else { System.out.println("Download failed: " + response.fileName); } socket.close(); } } ``` 使用示例: ```java public class Main { public static void main(String[] args) throws IOException { // 启动服务端 Server server = new Server(8000); server.start(); // 创建客户端并上传、下载文件 Client client = new Client("localhost", 8000); client.upload("test.txt"); client.download("test.txt"); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FocusOneThread

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值