0

I have a big issue with my java client.

Server: perl, with IO::Socket;

$n is the socket

    sub listen{    
 while(1){
  my $n = $_[0]->accept();
  my $thread;
  my $thread2;
  $thread = threads->create('talk', $n);
  $thread2 = threads->create('recu', $n);
 }

When a client send a message to the server

    sub talk{
 my $n = $_[0];
 while(<$n>){
  print $n $_;
 }

In 'talk' the server send the client message back to the client

Client: Java, in a Thread I send a byte array ->

static DataOutputStream os;

...

     public static void handleMsg(byte [] b){
  try {
   os.write(b);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

->

<pre><code>
    byte[] buff = new byte[]{10,4};
ThreadListen.handleMsg(buff);
</code></pre>
I receive in the run() method only the first byte in the array (10)
<pre><code>
    public void run(){
  DataInputStream in = null;
  try{
   in = new DataInputStream(s.getInputStream());
  } catch (IOException e) {
   System.out.println("in or out failed");
   System.exit(-1);
  }

  while(true){
   try{
    byte[] buff = new byte[6];
    int b = in.read(buff, 0, buff.length);
   }catch (IOException e) {
    System.out.println("Read failed");
    System.exit(-1);
   }
  }
 }

If I try to send

byte[] buff = new byte[]{50,4};
ThreadListen.handleMsg(buff);

I receive nothing!!!

Did i missed something, I assume that if I send

byte[] buff = new byte[]{50,4};

I should receive 50,4 :)

Thanks,

0

2 Answers 2

4

Your Perl code is doing <$n> so it's fetching data line-by-line. since 10 is the line-feed character, receiving a {10,4} sequence means it gets an empty line (which it prints back) and then a character 4. Even if it receives that (some debug messages in the Perl code would help), the print back to the socket may not complete if the socket is block-buffered (likely the default) without flushing the socket filehandle.

Sign up to request clarification or add additional context in comments.

5 Comments

Understand - why when I send 10,4 I got 10 but when I send 50,4, I received nothing...
<$n> fetches line-by-line: it won't return anything until it encounters an end-of-line character (10) or the socket is closed by the other side. You need to use read, as mentioned by Hasturkun.
You mean something like this?:sub talkRead{ my $n = $_[0]; my $data; while(1==1){ $n->read($data, 1024); print $n $data; } }
Are you sure it is only a Perl issue? I try with read but I got nothing, I will try again :)
the Java output stream might be buffered as well? DataOutputStream doesn't do buffering, though, so if you put that straight on top of the socket it shouldn't. I tend to use strace (on linux) to see the system calls to read/write etc. to debug exactly what's getting sent through in these situations.
1

You may want to use read explicitly (instead of readline implicitly, which as the name suggests reads up to the end of the line, or EOF) to read from the socket.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.