Sending Array of Bytes from Client to Server

Sending Array of Bytes from Client to server?

Just use Stream - no need for a writer here. Basic sending is simple:

stream.Write(data, 0, data.Length);

However you probably need to think about "framing", i.e. how it knows where each sun-message starts and ends. With strings this is often special characters (maybe new line) - but this is rarely possible in raw binary. A common appoach is to proceed the message with the number f bytes to follow, in a pre-defined way (maybe network-byte-order fixed 4 byte unsigned integer, for example).

Reading: again, use the Stream Read method, but understand that you always need t check the return value; just because you say "read at most 20 bytes" doesn't mean you get that many, even if more is coming - you could read 3,3,3,11 bytes for example (unlikely, but you see what I mean). For example, to read exactly 20 bytes:

var buffer = new byte[...];
int count = 20, read, offset = 0;
while(count > 0 && ((read = source.Read(buffer, offset, count)) > 0) {
offset += read;
count -= read;
}
if(count != 0) throw new EndOfStreamException();

How can I send multiple arrays of bytes from client to tcp server sequentially in rust?

I found the solution!

Instead of using stream.read() on the serverside I had to use stream.read_exact().

then it will read until 256 bytes were sent from the client (number of bytes = length of buffer)

This way It cant return the length of the recieved buffer, but I circumvented this problem, by writing the length into the first byte of the buffer (only 256 byte big buffers are possible, tho)

Send Data between client and server as a byte array

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);

Sending randomly generated byte array from client to the server using TCP/IP communication

The first problem I noticed is that in your server code, you've called UTF8.GetString using the entire buffer instead of specifying the number of bytes that you actually received. Change this:

networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
string dataFromClient = System.Text.Encoding.UTF8.GetString(bytesFrom);

Into this:

var receivedCount = networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
string dataFromClient = System.Text.Encoding.UTF8.GetString(bytesFrom, 0, receivedCount);

You will eventually run into another problem, however, as messages sometimes get fragmented and your design doesn't include any method for signaling that the entire message has been received or not. You'll need to put a line break at the end of the each message, or prefix the messages with the total expected size, or something so that the server knows when all the data has been received. You can't expect everything to arrive in one piece.

Java TCP socket program to send and read [x] bytes array. How to loop, sout and sent correctly

The problem is that you did not read the data from the socket, but only cycled through the local array.

You need to change these pieces of code:

1) Server

from:

            byte[]message = new byte[length];
for(int i = 0; i < message.length; i++) {
System.out.println(message[i]); // print array
}

to:

            byte[]message = new byte[length];
for(int i = 0; i < message.length; i++) {
message[i] = inFromClient.readByte();
System.out.println(message[i]); // print array
}

2) client

from:

    byte[] messageFromServer = new byte[length];
for(int i = 0; i < messageFromServer.length; i++) {
System.out.println(messageFromServer[i]);//prints received byte array
}

to:

    byte[] messageFromServer = new byte[length];
for(int i = 0; i < messageFromServer.length; i++) {
messageFromServer[i] = inFromClient.readByte();
System.out.println(messageFromServer[i]);//prints received byte array
}

An example is without specifying controls, the length of the array, ...

Or with your function readFully

1) Server

    int length = inFromClient.readInt(); // read length of incoming message
byte[] message = new byte[length];
if (length > 0) {
inFromClient.readFully(message, 0, message.length); // readFully the message, but how?
}

for (int i = 0; i < message.length; i++) {
System.out.println(message[i]); // print array
}

2) Client

    int length = inFromClient.readInt(); // read length of incoming message
byte[] messageFromServer = new byte[length];
if (length > 0) {
inFromClient.readFully(messageFromServer, 0, messageFromServer.length); // read the message
}

for (int i = 0; i < messageFromServer.length; i++) {
System.out.println(messageFromServer[i]);//prints received byte array
}


Related Topics



Leave a reply



Submit