readLong()
嗨,DataInputStream.
当我通过它输入一个值时,我遇到了一些问题,DataOutputStream.writeLong()
但是当它被发送时,它比它应该的要大得多,我将代码全部放在一个可运行的语句中,以便程序不冻结
这是客户
socket = new Socket(ipAddress, Port);
bos = new BufferedOutputStream(socket.getOutputStream());
dos = new DataOutputStream(bos);
File f = new File("C:/Users/lukeLaptop/Downloads/RemoveWAT22.zip");
long length = f.length();
dos.writeLong(length);
String name = f.getName();
dos.writeUTF(name);
FileInputStream fis = new FileInputStream(f);
bis = new BufferedInputStream(fis);
int theByte = 0;
while ((theByte = bis.read()) != -1) {
bos.write(theByte);
}
bis.close();
dos.close();
服务器端
ServerSocket server = new ServerSocket(8080);
while (true) {
socket = server.accept();
System.out.println("Got a client !");
bis = new BufferedInputStream(socket.getInputStream());
dis = new DataInputStream(bis);
int filesCount = dis.readInt();
long fileLength = dis.readLong();
//String fileName = dis.readUTF();
File f = new File("C:/test/here/test.zip");
FileOutputStream fos = new FileOutputStream(f);
BufferedOutputStream bos = new BufferedOutputStream(fos);
for (int j = 0; j < fileLength; j++) {
bos.write(bis.read());
}
bos.close();
dis.close();
编辑
如果有人可以帮助我编写代码,非常感谢我是套接字新手,我对事情有点困惑,我想要的只是使用 dataoutputstream 的 writeLong 方法发送文件的长度并发送writeUTF 的名称,但我不知道发生了什么以及如何解决它