-1

I have an assignment for school where i have to make an application working through the network. We can do it in either Java or C ( or both ). We've been given an exact protocol to communicate between computers. Here is an example of a message of this protocol :

[REGIS␣id␣port␣pass+++] 

On this message, id must be exactly on 8 bytes, port on 4 bytes , pass on 2 bytes, REGIS on 5 bytes and +++ on 3 bytes. To read this message, i use an InputStream object and the read() method, so i get a byte[] array.

My question is : can i construct a String object directly from the byte [] array and use a regex to check if the message is correctly constructed ?

Other question is : can i make a String containing my message, and use String.getBytes() to send a byte array [] through the network ?

My concern is about encoding : am i sure it will make 1 byte for every character ? Won't i have a problem if the message is read by a C program ?

Thank you for your help !

StuYYY
  • 97
  • 2
  • 12
  • 3
    "can i construct a String object directly from the byte [] array" - there's literally a constructor for that. Have you tried anything yet? – f1sh May 26 '17 at 13:25
  • Yes i know there is a constructor for that, my question is : can i do it and make sure i still have the correct number of bytes between conversion ? – StuYYY May 26 '17 at 13:27
  • 1
    A regex works on String, which is composed of characters, not bytes. All byte sequences are not valid strings, and how bytes are transformed to characters depends on the chosen charset (aka encoding). Why don't you just read the number of bytes you expect (22)? How could you know that a message is invalid anyway if you read 22 bytes, since you don't have any restriction of what the bytes can be? The only invalid cases I see are: 1. you receive less than 22 bytes and then the socket is closed; 2. You're supposed to receive N messages but the number of received bytes is not a multiple of 22. – JB Nizet May 26 '17 at 13:30
  • Treating bytes as characters without precisely specifying how bytes map to characters (charset) is dodgy at best. As given your "protocol" specification is incomplete. If there is no additional information you may have missed (e.g. a common preface chapter on encoding) there is no canonic way to interpret this specification. – Durandal May 26 '17 at 14:57
  • Are id, port and pass sent in binary form or as text? – Danny_ds May 28 '17 at 20:53

1 Answers1

1

My question is: can I construct a String object directly from the byte [] array and use a regex to check if the message is correctly constructed?

Yes you can, String class has a constructor which takes byte [] and then apply appropriate regex to check whether message is valid

Other question is : can i make a String containing my message, and use String.getBytes() to send a byte array [] through the network ?

Yes you can, If you want to know how to send file byte over network look this

Neeraj Jain
  • 7,643
  • 6
  • 34
  • 62
  • Yes i know how to do these things, i'm not asking about the code, i'm asking if this is a correct way to do it ? But thanks for the answer – StuYYY May 26 '17 at 13:32
  • what happens if you get a 0x10 0x09 0x01 bytes ? Then, direct conversion from byte to string, without any assumption on charset ? This may work or not work depending on the input. – BigMike May 26 '17 at 14:03