1

I'm new to Java programming. I want to write simple Java network protocol. I want to send this string to remote Java host, for example:

230 computer delete reboot exit

The problem that I cannot resolve is how I can extract the strings one by one and put it into array? And the biggest mystery to me is how I do this if I have strings with different length? Thank you for your help!

PS: Can I send directly entire array not just simple strings?

PS 2: What if I have Java server and C client? As far as know only strings can be used to exchange data between both sides?

Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
user1285928
  • 1,328
  • 29
  • 98
  • 147
  • I can't speak about sending to a remote Java host, but one can convert a string to an array with the `.split()` function. `"230 computer delete reboot exit".split("\\s+")` will split your string by whitespace into an array of 5 elements. – Ina Jun 13 '12 at 14:16
  • 1
    you don't know it yet, but this isn't your *biggest* problem ... –  Jun 13 '12 at 14:17
  • 2
    Would you please share your experience? – user1285928 Jun 13 '12 at 14:18
  • You have to decide how you want to serialize your data, if you want to send something more complicated like arrays. Look at http://en.wikipedia.org/wiki/Comparison_of_data_serialization_formats for some alternatives. – Christoffer Hammarström Jun 13 '12 at 14:25

3 Answers3

4
String input = "230 computer delete reboot exit";
String s[] = input.split("\\s+");

// now s[0] = 230 s[1] = computer

Read more on String.split().

And the regex used here \\s+ is equal to any whitespace. Read this for further information.

Community
  • 1
  • 1
Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
4

You can either:

  1. Split the strings using String.split method

    String text = "230 computer delete reboot exit";
    String[] split = text.split("\\s");
    
  2. Using a Pattern and a Matcher

    String text = "230 computer delete reboot exit";
    Pattern p = Pattern.compile("(.*)\\s+(.*)\\s+(.*)\\s+(.*)\\s+(.*)");
    Matcher m = p.matcher(text);
    if (m.matches()) {
        String first = m.group(1);
        String second = m.group(2);
        ....
    
  3. Using a Scanner class

    Scanner s = new Scanner(text);
    int firstNumber = s.nextInt(); // 230
    String secondText = s.next(); // computer
    ....
    

The second and the third choice could be superior if read stuff from a stream.

Or some other method... :)

dacwe
  • 43,066
  • 12
  • 116
  • 140
  • One more question: If I want to connect Java server and C client what is the most elective way to exchange data? Strings? – user1285928 Jun 13 '12 at 14:23
  • Define a binary or textual protocol. In your case you can start with strings just like you defined in your question. – dacwe Jun 13 '12 at 14:24
3

You can send directly an array by using serialization through ObectInputStream and ObjectOutputStream.

But I suggest you to use your own classes to encapsulate messages. Eg

class NetworkMessage {
  int code;
  String param1;
  String param2;
  ..
}

So that it's more object oriented and structured. Sending just strings is not a good design, especially if the protocol grows in complexity.

Regarding splitting the string just use String.split(String regex) method but it's not a good solution.

Jack
  • 131,802
  • 30
  • 241
  • 343