将文件发送给一组用户时遇到问题。用户可以接收文件是从服务器发送的,但如果小于8kb ,文件将不会被保存。 这是代码:
MulticastSocketServer
List<ArrayList<Mat>>
MulticastSocketClient
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class MulticastSocketServer{
public static void main(String[] args) {
String fileName;
String address = "235.0.0.1";
int port = 2222;
Scanner in = new Scanner(System.in);
System.out.print("Please enter file name : ");
fileName = in.next();
try (DatagramSocket serverSocket = new DatagramSocket()) {
InetAddress addr = InetAddress.getByName(address);
BufferedReader br = new BufferedReader(new FileReader(fileName + ".txt"));
DatagramPacket fn = new DatagramPacket(fileName.getBytes(),fileName.getBytes().length, addr, port);
serverSocket.send(fn);
DatagramPacket msgPacket = null;
String txt = "";
while((txt = br.readLine())!=null){
msgPacket = new DatagramPacket(txt.getBytes(),txt.getBytes().length, addr, port);
serverSocket.send(msgPacket);
System.out.println(txt);
}
}catch (IOException ex) {ex.printStackTrace();}
}
}
}
答案 0 :(得分:2)
你永远不会退出while (true)
循环,因为你没有任何传输流结束的机制,所以你永远不会关闭PrintWriter
,所以它不会刷新它最终缓冲区,所以任何文件&lt; 4096个字符根本不会被刷新,因此长度为零。
然而,你的代码有更糟糕的问题。你假设:
你正在使用UDP。这意味着大多数这些假设都是无效的。