简体   繁体   English

在客户端和服务器之间发送数据作为字节数组

[英]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.

相关问题 在客户端和服务器之间将图像作为字节数组发送 - Sending an image as a byte array between client and server 如何将字节数组从服务器(码头服务器)发送到客户端(RPI)? - How to send byte array from server(jetty server) to client(RPI)? 无法将字节数组从服务器发送到客户端,无法从客户端发送到服务器 - not able to send byte array from server to client,able to send from client to server 通过套接字Java服务器将字节数组和字符串发送到C ++客户端 - Send Byte array plus String over socket Java server to c++ client 服务器上的Resteasy客户端字节数组为空 - Resteasy Client Byte Array Null on Server Java创建要发送到服务器的数据包字节数组 - Java Creating a packet byte array to send to server 客户端不向服务器发送数据 - Client does not send data to server 使用java中的http客户端将Byte数组作为文件发送 - Send Byte array as file in using http client in java TCP Client Server 通信 - 输入字节数组在服务器接收时被修改 - TCP Client Server communication - The input byte array is modified as the server receives 在客户端服务器之间发送消息(消息以单个字节开头) - Sending a message between client server where a message begins with a single byte
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM