Shouldn't a JRuby<=Sockets=>Java Connection be simple?

I know it MUST be simple, but I am just unable to get these two to speak
to each other. Can anyone shed any light on my problem here? In Jruby, I
have a client as:

class SocketsexamplesController < ApplicationController
require ‘socket’
require ‘java’

def example
begin
puts “Creating socket to localhost:2003”
socket = TCPSocket::new(“127.0.0.1”, 2003)
s = new java.String(“you should see this”)
puts “sending s”
socket.send(s,0)
puts “reading response”
results = socket.recv(1000)
print results
rescue
return
end
end

and in Java, my server :

import java.io.;
import java.net.
;

public class Server{
ServerSocket providerSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
String message;

Server(){}

void run()
{
    try{
        //1. creating a server socket
        providerSocket = new ServerSocket(2003, 10/*backlog*/);
        //2. Wait for connection
        System.out.println("Waiting for connection");
        connection = providerSocket.accept();
        System.out.println("Connection received from " + 

connection.getInetAddress().getHostName());
//3. get Input and Output streams
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());
sendMessage(“Connection successful”);
//4. The two parts communicate via the input and output
streams
do{
try{
message = (String)in.readObject();
System.out.println(“client>” + message);
if (message.equals(“bye”))
sendMessage(“bye”);
}
catch(ClassNotFoundException classnot){
System.err.println(“Data received in unknown
format”);
}
}while(!message.equals(“bye”));
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
in.close();
//out.close();
providerSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}

void sendMessage(String msg)
{
    try{
        out.writeObject(msg);
        out.flush();
        System.out.println("server>" + msg);
    }
    catch(IOException ioException){
        ioException.printStackTrace();
    }
}

public static void main(String args[])
{
    Server server = new Server();
    while(true){
        server.run();
    }
}

}

It simply hangs in the server somewhere between the output of
“Connection received from localhost” never getting as far as saying
“Connection successful”.

Any ideas where I have gone wrong on this? Thanks, Janna

Am Mittwoch, 17. Juni 2009 schrieb Mistress Janna Brossard:

I know it MUST be simple, but I am just unable to get these two to
speak to each other. Can anyone shed any light on my problem here?

This is a missunderstanding of how ObjectInputStream works.

I am not sure what you wanted to do, but it is always true that an
ObjectInputStream can only understand/read what an ObjectOutputStream
has written.

As long as you simply want to read the bytes of the sent String from
the InputStream of the socket you could use “in.read(byte[])”, which
reads the bytes into the argument byte-buffer.
Then you can create a String from the bytes.

Hope this helps,
Frank

  s = new java.String("you should see this")

and in Java, my server :
ObjectInputStream in;
System.out.println(“Waiting for connection”);
streams do{
}
catch(IOException ioException){
System.out.println(“server>” + msg);
server.run();
Microsoft brings you a new way to search the web. Try Bingâ„¢ now
Bing
tagline_try bing_1x1


Frank E.
Managing Director

Bancos GmbH
Hohenzollerndamm 150
14199 Berlin

Phone:    +49 30 884591-0
Fax:      +49 30 884591-100
mail to:  [email protected]
Internet: http://www.bancos.com

Managing Directors: Marco Brueders, Frank E.
Register Court: Amtsgericht Berlin-Charlottenburg
Register No.: HRB 81 138
VAT-ID: DE211841472

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Frank,

But how can I know the length of byte[] before I read it? How can I code
that to work, not knowing how long the message is? -Janna

Usually it’s something like

bytestream.write(source.read(source.available))

On Wed, Jun 17, 2009 at 10:19 AM, Mistress Janna Brossard <

Am Mittwoch, 17. Juni 2009 schrieb Dr. Robert D. Silvetz:

Usually it’s something like

bytestream.write(source.read(source.available))

A stream, or better say a socket, is a stream of bytes. The receiver
cannot know anything about the structure of the sent bytes, unless
you implement a protocol.
A protocol alows you to interpret the received bytes in some sence, in
example you could read up to a newline-byte, and tell the received
bytes something like a line.
Then, if you expect to receive such a line, all you have to do is read
bytes as long as you find the newline-byte.

The number of ‘available’ bytes on a stream is usually the number of
bytes which one call to read() (or the like) will receive at least.

This is true in both programming languages, java and ruby, isn’t it ?

Frank

To: [email protected]

Then you can create a String from the bytes.

Frank E.
Managing Director

Bancos GmbH
Hohenzollerndamm 150
14199 Berlin

Phone:    +49 30 884591-0
Fax:      +49 30 884591-100
mail to:  [email protected]
Internet: http://www.bancos.com

Managing Directors: Marco Brueders, Frank E.
Register Court: Amtsgericht Berlin-Charlottenburg
Register No.: HRB 81 138
VAT-ID: DE211841472

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email