Quantcast

Reading from a socket

classic Classic list List threaded Threaded
49 messages Options
12345
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Reading from a socket

citron
Hi,

The code found here is writing to a socket.

s = new Socket("localhost", 8167);
s << "Why don't you call me anymore?\n"
s.close()

How can I read the response from the socket?

Thanks!

Hotmail: Trusted email with Microsoft’s powerful SPAM protection. Sign up now.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Reading from a socket

John Hartnup
On 12 March 2010 09:32, Anders Viklund <[hidden email]> wrote:
> Hi,
>
> The code found here is writing to a socket.
>
> s = new Socket("localhost", 8167);
> s << "Why don't you call me anymore?\n"
> s.close()

s.withStreams { inStream, outStream ->
    def reader = inStream.newReader()
    def name = reader.readLine()
    outStream << "Hello ${name}"
}

...  one of my favourite examples of closures leading to neat idioms.

--
"There is no way to peace; peace is the way"

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Reading from a socket

citron
I am trying to pipe an input stream into the socket, but it never closes.

 server = new ServerSocket(8182)
 while(true) {
     server.accept() { socket ->
         socket.withStreams { input, output ->
         def s = new Socket("localhost", 9090)
         s << input       // < -- program hangs here, why?


Thanks for any ideas how to solve this!



> Date: Fri, 12 Mar 2010 10:10:37 +0000
> From: [hidden email]
> To: [hidden email]
> Subject: Re: [groovy-user] Reading from a socket
>
> On 12 March 2010 09:32, Anders Viklund <[hidden email]> wrote:
> > Hi,
> >
> > The code found here is writing to a socket.
> >
> > s = new Socket("localhost", 8167);
> > s << "Why don't you call me anymore?\n"
> > s.close()
>
> s.withStreams { inStream, outStream ->
> def reader = inStream.newReader()
> def name = reader.readLine()
> outStream << "Hello ${name}"
> }
>
> ... one of my favourite examples of closures leading to neat idioms.
>
> --
> "There is no way to peace; peace is the way"
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
> http://xircles.codehaus.org/manage_email
>
>


Hotmail: Free, trusted and rich email service. Get it now.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Reading from a socket

John Hartnup
On 12 March 2010 20:27, Anders Viklund <[hidden email]> wrote:
> I am trying to pipe an input stream into the socket, but it never closes.
>
>  server = new ServerSocket(8182)
>  while(true) {
>      server.accept() { socket ->
>          socket.withStreams { input, output ->
>          def s = new Socket("localhost", 9090)
>          s << input       // < -- program hangs here, why?

Basically, I don't think the Socket.leftShift() function does what you
want, with an inputStream as an argument.

Instead I think you need a loop that reads from input and writes to s.

--
"There is no way to peace; peace is the way"

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Reading from a socket

citron
well, the data arrives to its target destination, but the stream never closes, thats the problem.

Why shouldnt leftShift work in this case?


> Date: Fri, 12 Mar 2010 21:31:09 +0000
> From: [hidden email]
> To: [hidden email]
> Subject: Re: [groovy-user] Reading from a socket
>
> On 12 March 2010 20:27, Anders Viklund <[hidden email]> wrote:
> > I am trying to pipe an input stream into the socket, but it never closes.
> >
> >  server = new ServerSocket(8182)
> >  while(true) {
> >      server.accept() { socket ->
> >          socket.withStreams { input, output ->
> >          def s = new Socket("localhost", 9090)
> >          s << input       // < -- program hangs here, why?
>
> Basically, I don't think the Socket.leftShift() function does what you
> want, with an inputStream as an argument.
>
> Instead I think you need a loop that reads from input and writes to s.
>
> --
> "There is no way to peace; peace is the way"
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
> http://xircles.codehaus.org/manage_email
>
>


Hotmail: Trusted email with Microsoft’s powerful SPAM protection. Sign up now.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Reading from a socket

John Hartnup
On 12 March 2010 22:08, Anders Viklund <[hidden email]> wrote:
> well, the data arrives to its target destination, but the stream never
> closes, thats the problem.
>
> Why shouldnt leftShift work in this case?

Ah, I was going by the JavaDoc for Socket, which doesn't really
specify what leftShift(Object) does in detail:
"Overloads the left shift operator to provide an append mechanism to
add things to the output stream of a socket"
That doesn't document the fairly specialised case of an InputStream.
Could do better!

Without any documented semantics for leftShift(InputStream), I don't
know what you're trying to do, nor whether it should work.

Does the InputStream eventually signal EOF? It would seem reasonable
for leftShift() to return only when that happens. But again, nothing
*says* that it will. At least not in the JavaDoc.

--
"There is no way to peace; peace is the way"

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Reading from a socket

citron
I am trying to create a http proxy, just like tcpmon.

So far I have managed to get the request through to its destination.
Then it stalls because it never stops reading from the request input stream.


> Date: Sat, 13 Mar 2010 07:33:53 +0000
> From: [hidden email]
> To: [hidden email]
> Subject: Re: [groovy-user] Reading from a socket
>
> On 12 March 2010 22:08, Anders Viklund <[hidden email]> wrote:
> > well, the data arrives to its target destination, but the stream never
> > closes, thats the problem.
> >
> > Why shouldnt leftShift work in this case?
>
> Ah, I was going by the JavaDoc for Socket, which doesn't really
> specify what leftShift(Object) does in detail:
> "Overloads the left shift operator to provide an append mechanism to
> add things to the output stream of a socket"
> That doesn't document the fairly specialised case of an InputStream.
> Could do better!
>
> Without any documented semantics for leftShift(InputStream), I don't
> know what you're trying to do, nor whether it should work.
>
> Does the InputStream eventually signal EOF? It would seem reasonable
> for leftShift() to return only when that happens. But again, nothing
> *says* that it will. At least not in the JavaDoc.
>
> --
> "There is no way to peace; peace is the way"
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
> http://xircles.codehaus.org/manage_email
>
>


Hotmail: Trusted email with powerful SPAM protection. Sign up now.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Reading from a socket

John Hartnup
On 13 March 2010 08:20, Anders Viklund <[hidden email]> wrote:
> I am trying to create a http proxy, just like tcpmon.
>
> So far I have managed to get the request through to its destination.
> Then it stalls because it never stops reading from the request input stream.

And how would it know to stop reading from the inputstream, unless you
call a method that

So, do a loop. It's only using Java stuff; no Groovy tricks, but it's
simple and it works.

byte[] buffer
def len = input.read(buffer)
while(len >=0) {
    output.write(buffer,0,len)
    len = input.read(buffer)
}

Of course you probably also want to do the same to transmit data in
the opposite direction. You could do this in another thread:
   // Wrap the code above in a StreamForwarder class that implements Runnable.
   Thread c2s = new Thread(new StreamForwarder(clientInStream,serverOutStream));
   StreamForwarder s2c = new StreamForwarder(serverInStream,clientOutStream);
   // Run one in a new thread
   2s.start();
   // Run the other in this thread
   s2c.run();

Or you could read up on select() and do it all in one thread.
Potentially better scalability. Arguably less easy for people to
understand, if they later read the code and are not familiar with
select().


--
"There is no way to peace; peace is the way"

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Reading from a socket

citron
Hmm, its still not working.

byte[] buffer
def len = input.read(buffer) // <-- now its stalling here, why?
while(len >=0) {
 output.write(buffer,0,len)
 len = input.read(buffer)
}

> Date: Sat, 13 Mar 2010 08:46:21 +0000
> From: [hidden email]
> To: [hidden email]
> Subject: Re: [groovy-user] Reading from a socket
>
> On 13 March 2010 08:20, Anders Viklund <[hidden email]> wrote:
> > I am trying to create a http proxy, just like tcpmon.
> >
> > So far I have managed to get the request through to its destination.
> > Then it stalls because it never stops reading from the request input stream.
>
> And how would it know to stop reading from the inputstream, unless you
> call a method that
>
> So, do a loop. It's only using Java stuff; no Groovy tricks, but it's
> simple and it works.
>
> byte[] buffer
> def len = input.read(buffer)
> while(len >=0) {
> output.write(buffer,0,len)
> len = input.read(buffer)
> }
>
> Of course you probably also want to do the same to transmit data in
> the opposite direction. You could do this in another thread:
> // Wrap the code above in a StreamForwarder class that implements Runnable.
> Thread c2s = new Thread(new StreamForwarder(clientInStream,serverOutStream));
> StreamForwarder s2c = new StreamForwarder(serverInStream,clientOutStream);
> // Run one in a new thread
> 2s.start();
> // Run the other in this thread
> s2c.run();
>
> Or you could read up on select() and do it all in one thread.
> Potentially better scalability. Arguably less easy for people to
> understand, if they later read the code and are not familiar with
> select().
>
>
> --
> "There is no way to peace; peace is the way"
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
> http://xircles.codehaus.org/manage_email
>
>


Hotmail: Powerful Free email with security by Microsoft. Get it now.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Reading from a socket

John Hartnup
On 13 March 2010 09:38, Anders Viklund <[hidden email]> wrote:

> def len = input.read(buffer) // <-- now its stalling here, why?

http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream.html#read(byte[])

"This method blocks until input data is available, end of file is
detected, or an exception is thrown."

So, it's waiting for the other end to either write to the socket, or close it.

It seems as if by trying to write a proxy, you're running before you
can walk. Go through the Java sockets tutorials.
http://java.sun.com/docs/books/tutorial/networking/sockets/index.html
Write a simple client/server pair and make sure you understand how it works.

Groovy doesn't change anything much, other than give you a few
convenience methods for common cases (such as
ServerSocket.accept(Closure ) and Socket.withStreams(Closure)

--
"There is no way to peace; peace is the way"

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


12345
Loading...