PrintWriter 仅发送部分字符串 Java
我正在用 Java 做一些套接字工作,并尝试对其发送的数据进行编码。 该部分工作正常,但由于某种原因,它没有通过套接字发送整个编码的字符串。它似乎是在 3 的部分中发送的。
客户端从 MyBufferedReader 类(如下)执行一个简单的 readLine(),服务器像这样发送它:
private void sendFile(File f, String dest){ //The file to be sent and the destination
System.out.println("Sending file started..");
try {
this.out.println("CMD MKFILE " + dest + "/" + f.getName());
//TODO send the file
}catch(Exception e){
e.printStackTrace();
}
}
客户端接收到: CMD MKFILE C:\Users\Lolmewn\Documents\test/dir/n/linebrea
并再次读取 k!.txt
MyBufferedReader 和 MyPrintWriter 类如下所示:
MyBufferedReader :
@Override
public String readLine() throws IOException {
String read = super.readLine();
System.out.println("UNDEC: " + read);
try {
System.out.println("DEC: " + decode(read));
return decode(read);
} catch (Exception e) {
e.printStackTrace();
}
return read;
}
public static String decode(String b) throws Exception {
ByteArrayInputStream bais = new ByteArrayInputStream(b.getBytes("UTF-8"));
InputStream b64is = MimeUtility.decode(bais, "base64");
byte[] tmp = new byte[b.length()];
int n = b64is.read(tmp);
byte[] res = new byte[n];
System.arraycopy(tmp, 0, res, 0, n);
return new String(res);
}
和 MyPrintWriter:
private String hash(String x) {
try {
return encode(x);
} catch (Exception e) {
e.printStackTrace();
}
return x;
}
public static String encode(String b) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream b64os = MimeUtility.encode(baos, "base64");
b64os.write(b.getBytes("UTF-8"));
b64os.close();
return new String(baos.toByteArray());
}
发生了什么事以及如何解决这个问题?
请注意:我正在这些套接字上进行异步工作,这意味着我不能只使用 while(read != null) 语句。这会导致其他不应该存在的数据也存在在那里。
I'm doing some socket work in Java, and am trying to encode the data it is sending.
That part is working correctly, but for some reason it doesn't send the whole encoded String over the socket. It seems to send it in parts of 3.
The client performs a simple readLine() from the MyBufferedReader class (which is below), and the server sends it like this:
private void sendFile(File f, String dest){ //The file to be sent and the destination
System.out.println("Sending file started..");
try {
this.out.println("CMD MKFILE " + dest + "/" + f.getName());
//TODO send the file
}catch(Exception e){
e.printStackTrace();
}
}
The client receives this:CMD MKFILE C:\Users\Lolmewn\Documents\test/dir/n/linebrea
and after another read k!.txt
The MyBufferedReader and MyPrintWriter classes look like this:
MyBufferedReader:
@Override
public String readLine() throws IOException {
String read = super.readLine();
System.out.println("UNDEC: " + read);
try {
System.out.println("DEC: " + decode(read));
return decode(read);
} catch (Exception e) {
e.printStackTrace();
}
return read;
}
public static String decode(String b) throws Exception {
ByteArrayInputStream bais = new ByteArrayInputStream(b.getBytes("UTF-8"));
InputStream b64is = MimeUtility.decode(bais, "base64");
byte[] tmp = new byte[b.length()];
int n = b64is.read(tmp);
byte[] res = new byte[n];
System.arraycopy(tmp, 0, res, 0, n);
return new String(res);
}
and MyPrintWriter:
private String hash(String x) {
try {
return encode(x);
} catch (Exception e) {
e.printStackTrace();
}
return x;
}
public static String encode(String b) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream b64os = MimeUtility.encode(baos, "base64");
b64os.write(b.getBytes("UTF-8"));
b64os.close();
return new String(baos.toByteArray());
}
What's happening and how can I fix this?
Please do note: I am doing async work on these Sockets, meaning I can't just use a while(read != null) statement. That would cause other data not supposed to be there being there too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先撇开这一点:如果您从多个线程/客户端发送消息,那么您应该为每个线程/客户端打开一个套接字(就像accept() 所做的那样),这样来自不同客户端的消息就不能相互交错。
现在我假设每个套接字只有一个客户端(合理):
你必须实现一个简单的协议,通过它你可以区分一条消息和下一条消息,例如,如果它永远不能成为数据的一部分或前缀,则使用 0 字节消息与消息的长度并发送两者;这样您的服务器就可以区分一条消息和下一条消息。
First this aside: If you are sending from multiple threads/clients then you should open one socket for each of them (like accept() does), that way messages from different clients cannot interleave one another.
Now i am assuming only a single client per socket (reasonable):
You have to implement a simple protocol with which you can distinguish one message from the next, e.g. use a 0-byte if that can never be part of the data or prefix the message with the length of the message and send both; that way your server can distinguish one message from the next.