Learning2code
Learning2code

Reputation: 51

Print out Bytes received from Java to C

I am trying to send bytes from Android phone to my Raspberry Pi. When i sent it over, I received some weird symbols in this form: 5000 + weird symbols. Bytes should only be in numeric form when received right?

This is my code on Android.

socket.getOutputStream().write(String.valueOf("5000").getBytes());

And this is my code for receiving on Raspberry Pi.

char Buffer[1024];
nread = recv(clientSocket,Buffer,1024,0);
printf("Data Received: %s",Buffer);

Clearly, I am very new to sockets. Also, the bytes I am currently sending in java should = the bytes I am receiving on RPI? Will be really grateful if someone can explain it to me !

Upvotes: 1

Views: 213

Answers (4)

Aubin
Aubin

Reputation: 14873

The code socket.getOutputStream().write(String.valueOf("5000").getBytes()); sends 4 bytes, because Java use 16 bits chars ONLY for non ASCII character as specified in Java Language Specification 3.10.5.

To be sure, you have to print the value of nread in C program.

The code nread = recv(clientSocket,Buffer,1024,0); receives 4 characters and DOESN'T put the zero to terminate the string, so printf display the contents of the (non initailized) buffer, I suggest memset( Buffer, 0, sizeof( Buffer ))

Code suggested:

if( nread < 0 ) {
   perror("Read error");
   return;
}
Buffer[nread] = '\0';

To encode and decode messages and streams I usually use java.nio.ByteBuffer

To encode and send ASCII 7 java.lang.String:

import java.nio.ByteBuffer;

public final class SerializerHelper {

   public static void putString( String s, ByteBuffer target ) {
      final byte[] bytes = s.getBytes();
      target.putInt( bytes.length );
      target.put( bytes );
   }

   public static void putBoolean( boolean value, ByteBuffer target ) {
      target.put((byte)( value ? 1 : 0 ));
   }

   public static boolean getBoolean( ByteBuffer source ) {
      return source.get() != 0;
   }

   public static String getString( ByteBuffer source ) {
      final int len = source.getInt();
      final byte[] bytes = new byte[len];
      source.get( bytes );
      return new String( bytes );
   }
}

In C:

uint32_t len       = strlen( s );
uint32_t lenForNet = htonl( len );
char * p = buffer;
memmove( p, &lenForNet, sizeof( lenForNet ));
p += sizeof( lenForNet );
memmove( p, s, len );
send( sckt, buffer, len + sizeof( LenForNet ), 0 );

Upvotes: 1

Deb S
Deb S

Reputation: 544

Receiving from socket

char recv_data;
 while ((bytes_received = recv(connected,&recv_data,1,0)) > 0){
        printf("\nrecv= %c\n", recv_data);
        }

Sending from Java

 String datatosend = "5000";
   char[] strArray;
   strArray = datatosend.toCharArray();

   while (true) {
   for( int index = 0; index < strArray.length; index++){
       out.write(strArray[index]);
   }
out.flush();

Upvotes: 0

Learning2code
Learning2code

Reputation: 51

I solved the problem. After searching and looking around stackoverflow, I followed the answer from this link. Here

I did:

Buffer[nread] = '\0';
printf("%s",Buffer);

Upvotes: 0

Gabe Sechan
Gabe Sechan

Reputation: 93708

Strings in Java are 16 bits. If you want to send a string to C, convert to utf-8 first. (Or use a 16 bit string library, but generally you'd convert to utf8).

Upvotes: 0

Related Questions