[英]Send Data between client and server as a byte array
我正在嘗試在客戶端和服務器之間發送加密數據。 由於 RSA 加密,它采用字節數組形式。 這意味着我不得不改變我發送數據的方式。 我目前無法讓它工作,我會把我的方法(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");
}
}
}
抱歉應該澄清一下,本質上 CSocket 是我打開的套接字,我想通過套接字發送 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);
byte[] 的 ByteArrayOutputStream 中的 write 方法需要多兩個 arguments。 像下面這樣的東西可能會起作用:
out.write(msg, 0, msg.length);
請讓我知道這是否有用。
我想我現在解決了我的問題。 它可能不是最有效的方法,但基本上我以一種格式對字節數組進行編碼,這意味着我不會丟失任何數據。 這意味着我以這種編碼格式發送它,然后在接收端我只是簡單地解碼它。 以這種方式與印刷作家一起工作得更好。
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.