我想从服务器向客户端发送200 x 200个数组(每个“行”4个)。 但我必须将其添加到服务器,以便客户端接收所有数据
while(true){
Bw.write("end");
Bw.write("\n");
}
没有它,客户端只接收第一个194 x 200和1 x 32,然后不接收mo数据。
我必须做些什么而不等待while(true)中的异常?
服务器:
ServerSocket ssocket = new ServerSocket(PORT_NUMBER);
Socket socket = ssocket.accept();
BufferedWriter Bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
socket.setTcpNoDelay(true);
//load from XML
SAXBuilder builder = new SAXBuilder();
File xmlFile = new File("res/map.xml");
Document doc = (Document) builder.build(xmlFile);
Element root = doc.getRootElement();
int sz = root.getAttribute("sizeZ").getIntValue();
int sx = root.getAttribute("sizeX").getIntValue();
data = new float[sz][sx];
data2 = new int[sz][sx];
Bw.write(String.valueOf(sz));
Bw.write(" ");
Bw.write(String.valueOf(sx));
Bw.write("\n");
for(Object o : root.getChildren()){
Element e = (Element) o;
int z = e.getAttribute("z").getIntValue();
int x = e.getAttribute("x").getIntValue();
int y = e.getAttribute("y").getIntValue();
int g = (int)e.getAttribute("g").getFloatValue();
data[z][x] = y;
data2[z][x] = g;
Bw.write(String.valueOf(z));
Bw.write(" ");
Bw.write(String.valueOf(x));
Bw.write(" ");
Bw.write(String.valueOf(y));
Bw.write(" ");
Bw.write(String.valueOf(g));
Bw.write("\n");
}
Bw.write("end");
Bw.write("\n");
while(true){
Bw.write("end");
Bw.write("\n");
}
客户端:
socket = new Socket("127.0.0.1", 1080);
System.out.println("úspěšné připojení");
socket.setTcpNoDelay(true);
BufferedReader Br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String[] s;
String st;
st = Br.readLine();
System.out.println(st);
s = st.split(" ");
data = new float[Integer.parseInt(s[0])][Integer.parseInt(s[1])];
data2 = new int[Integer.parseInt(s[0])][Integer.parseInt(s[1])];
st = Br.readLine();
s = st.split(" ");
while(!s[0].equals("end")){
int z = Integer.parseInt(s[0]);
int x = Integer.parseInt(s[1]);
int y = Integer.parseInt(s[2]);
int g = Integer.parseInt(s[3]);
data[z][x] = y;
data2[z][x] = g;
st = Br.readLine();
s = st.split(" ");
}
答案 0 :(得分:0)
您只需在完成(真实)数据写入后调用bw.flush()
。
虽然我引起了你的注意,但这些风格很糟糕:
BufferedReader Br = // should be br
BufferedWriter Bw = // should be bw
并且您的客户端和服务器端代码看起来都可能泄漏文件描述符。