[英]Send Data between client and server as a byte array
I am trying to send encrypted data between a client and server.我正在尝试在客户端和服务器之间发送加密数据。 Due to the RSA encryption its in Byte array form.
由于 RSA 加密,它采用字节数组形式。 This means I have had to change the way I send data.
这意味着我不得不改变我发送数据的方式。 I curently cant get it working, Ill leave my method (sendMessage) below which is what handles the sending of the message, If anyone could tell me what I am doing wrong that would be great:)
我目前无法让它工作,我会把我的方法(sendMessage)留在下面,它是处理消息发送的方法,如果有人能告诉我我做错了什么,那就太好了:)
public void sendMessage(byte[] msg){
if(msg.equals("null")){
}
else{
try{
ByteArrayOutputStream f = new ByteArrayOutputStream(CSocket.getOutputStream());
out = new PrintWriter(f);
out.write(msg);
countSend++;
}catch (IOException e){
System.out.println("ERROR");
}
}
}
Sorry should have clarified, essentially CSocket is a socket I have opend and I want to send msg through the socket.抱歉应该澄清一下,本质上 CSocket 是我打开的套接字,我想通过套接字发送 msg。 The issue I have specifically with this code is It says that:
OutputStream can not be converted to int
on the line where I creat the ByteArrayOutputStream
object f
and No suitable method found for write(byte[])
on the line out.write(msg);
我在这段代码中特别遇到的问题是它说:
OutputStream can not be converted to int
on where Icreat the ByteArrayOutputStream
object f
and No suitable method found for write(byte[])
on the line out.write(msg);
Thanks for the clarification!感谢您的澄清! Please see https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayOutputStream.html#write(byte%5B%5D,int,int)
请参阅https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayOutputStream.html#write(byte%5B%5D,int,int)
The write method in ByteArrayOutputStream for byte[] needs two more arguments. byte[] 的 ByteArrayOutputStream 中的 write 方法需要多两个 arguments。 Something like the following might work:
像下面这样的东西可能会起作用:
out.write(msg, 0, msg.length);
Please let me know if this is useful.请让我知道这是否有用。
I think I fixed my issue now.我想我现在解决了我的问题。 Its probibably not the most efficient way of doing it but essentially I encode the byte array in a format that means I wont loose any data.
它可能不是最有效的方法,但基本上我以一种格式对字节数组进行编码,这意味着我不会丢失任何数据。 This means I send it in this encoded format and then on the receving end I just simply decode it.
这意味着我以这种编码格式发送它,然后在接收端我只是简单地解码它。 Works so much better with print writer doing it this way.
以这种方式与印刷作家一起工作得更好。
OutputStream f = CSocket.getOutputStream();
out = new PrintWriter(f);
String encodedmsg = new String(msg, "ISO-8859-1"); // ISO-8859-1 is supposed to give every character a unique representation so I shouldent loose any data during encoding and decoding
out.write(encodedmsg);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.