Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   remote netcat talking to a Java app (http://www.velocityreviews.com/forums/t708363-remote-netcat-talking-to-a-java-app.html)

neuneudr@yahoo.fr 12-11-2009 03:31 PM

remote netcat talking to a Java app
 
Hi all,

is it possible to have a Java app listen on a socket (?)
and be *instantly* notified when a remote shell script
netcats something on that socket?

I have control on the exact number of bytes sent by
netcat. Say the shell script does this:

echo "something" | netcat 10.0.0.39 3423

And I've got a Java app configured to 'listen' on
socket 3423, can I be instantly notified when the
9 letters of "something" have been netcat'ed.

Imagine, out of simplicity (?) that it's always
exactly the same number of bytes that are going
to be sent by netcat and that netcat shall work
in TCP mode.

What should I do on the Java side? Which class
should I use and how can I do so that I don't need
to 'poll' the socket but be instantly notified when
something arrives on the socket? (it has to work
on Java 5).

It is maybe a dumb question but I've got no idea
as to how Java application listen on sockets and
how they're supposed to act when something comes
on the socket.

Any explanation greatly appreciated : )


neuneudr@yahoo.fr 12-11-2009 03:48 PM

Re: remote netcat talking to a Java app
 
I need to precise that the goal is not scalibility/perfs
for thousands nor hundreds of remote shell script that would
be netcatt'ing to the Java app.

There's going to be exactly *one* remote shell script doing
the netcat and the goal is to minimize the delay between
the time the netcat is issued on the remote machine by the
shell script and the time at which my Java apps can call a
callback.

Because the goal is to minimize the delay, polling is out of
question (a 100 ms delay, for example, would already be very
laggy for my needs). Typically the two machines will be on
the same network and a round-trip ping takes less than 1 ms.

I'd like to have that kind of reactivity between a netcat and
the Java app.



Daniel Pitts 12-11-2009 04:26 PM

Re: remote netcat talking to a Java app
 
neuneudr@yahoo.fr wrote:
> Hi all,
>
> is it possible to have a Java app listen on a socket (?)
> and be *instantly* notified when a remote shell script
> netcats something on that socket?
>
> I have control on the exact number of bytes sent by
> netcat. Say the shell script does this:
>
> echo "something" | netcat 10.0.0.39 3423
>
> And I've got a Java app configured to 'listen' on
> socket 3423, can I be instantly notified when the
> 9 letters of "something" have been netcat'ed.
>
> Imagine, out of simplicity (?) that it's always
> exactly the same number of bytes that are going
> to be sent by netcat and that netcat shall work
> in TCP mode.
>
> What should I do on the Java side? Which class
> should I use and how can I do so that I don't need
> to 'poll' the socket but be instantly notified when
> something arrives on the socket? (it has to work
> on Java 5).
>
> It is maybe a dumb question but I've got no idea
> as to how Java application listen on sockets and
> how they're supposed to act when something comes
> on the socket.
>
> Any explanation greatly appreciated : )
>

Read the JavaDocs for ServerSocket, it should answer all your questions.

If you have trouble after that, provide an SSCCE <http://sscce.org>, so
that we may further assist you.

I believe what you are asking is technically possible, but I don't feel
like writing your program for you to explain it.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

neuneudr@yahoo.fr 12-11-2009 05:15 PM

Re: remote netcat talking to a Java app
 
On Dec 11, 4:26 pm, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.net> wrote:
....
> > Any explanation greatly appreciated : )

>
> Read the JavaDocs for ServerSocket, it should answer all your questions.


So blocking I/O would be the way to go?

So I can simply block, in a thread, on DatagramSocket.receive()?

What would be the overhead for the other threads of having a thread
being blocking on I/O?


> I believe what you are asking is technically possible, but I don't feel
> like writing your program for you to explain it.


I'm not asking that... I try to understand what is at work here.



markspace 12-11-2009 05:19 PM

Re: remote netcat talking to a Java app
 
neuneudr@yahoo.fr wrote:

>
> Because the goal is to minimize the delay,



The delay in Java should be exactly the same as the delay that a C
program using a native library would have listening on the same socket.
It's completely dependent on the sending system, the network itself,
and the remote OS.

I agree polling is a bad idea. There's no reason not to just block on
the socket and avoid wasting CPU resources.

In both C and Java, the method call is "accept()" iirc to receive an new
socket connection. Just do that and you'll be fine.

<http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html>

Daniel Pitts 12-11-2009 09:39 PM

Re: remote netcat talking to a Java app
 
neuneudr@yahoo.fr wrote:
> On Dec 11, 4:26 pm, Daniel Pitts
> <newsgroup.spamfil...@virtualinfinity.net> wrote:
> ....
>>> Any explanation greatly appreciated : )

>> Read the JavaDocs for ServerSocket, it should answer all your questions.

>
> So blocking I/O would be the way to go?
>
> So I can simply block, in a thread, on DatagramSocket.receive()?
>
> What would be the overhead for the other threads of having a thread
> being blocking on I/O?
>
>
>> I believe what you are asking is technically possible, but I don't feel
>> like writing your program for you to explain it.

>
> I'm not asking that... I try to understand what is at work here.
>
>

And I'm asking you to try something yourself, and then come back if it
doesn't work as expected. Let us know what you've tried, and we'll be
likely to tell you why it failed and what to change.



--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

neuneudr@yahoo.fr 12-12-2009 12:28 AM

Re: remote netcat talking to a Java app
 
On Dec 11, 9:39 pm, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.net> wrote:
....
> And I'm asking you to try something yourself, and then come back if it
> doesn't work as expected.


Which is mostly pointless when one doesn't know what to expect,
which was *precisely* why I came posting to c.l.j.p...

I came with a very precise question and your answer was
basically "RTF ServerSocket Javadoc".

I was looking for "explanation", which I stated. To grasp the
"bigger picture".

Your RTFA post didn't tell much about blocking I/O or
non-blocking I/O. It didn't tell much about non-blocking
synchronous, nor non-blocking asynchronous.

It didn't tell much about wether ServerSocket would be
a good fit to minimize latency or not.

Nor if polling would be a good way to minimize latency.

Nor if spawning a separate thread doing blocking I/O would
be a good way to proceed and likely or not to affect the
perfs of the rest etc.

I'm sorry but I don't buy neither Lew's grammar-naziness
nor your tone which I found rude in both your posts.

This is Usenet, people ask questions. If it's to answer
"go to http://java.sun.com, I'm not spending time writing
code for you" then you could even spare you the trouble
of answering my posts.

Such answers, honestly, won't be missed.

I don't give a flying f*ck about the fact that you're a
regular here or not and I honestly don't give a flying
f*ck either if all the other regulars come up to tell
"oh but I don't think it was rude".

Daniel Pitts rudely wrote:

"I believe what you are asking is technically possible,
"but I don't feel like writing your program for you to
"explain it.

Seriously, wtf!?

Killfile me, I won't miss your rudeness.


Lew 12-12-2009 01:21 AM

Re: remote netcat talking to a Java app
 
neuneudr@yahoo.fr wrote:
> I'm sorry but I don't buy neither Lew's grammar-naziness
> nor your tone which I found rude in both your posts.


<http://en.wikipedia.org/wiki/Godwin's_Law>

Boy, you really have a bug up your butt, don't you? I hadn't even posted to
this thread until you popped up with my name.

> Such answers, honestly, won't be missed.


Neither will your nonsense.

> I don't give a flying f*ck about the fact that you're a


OOH! You are such a gentleman!

> regular here or not and I honestly don't give a flying
> f*ck either if all the other regulars come up to tell
> "oh but I don't think it was rude".


You are so full of nonsense.

> Daniel Pitts rudely wrote:
>
> "I believe what you are asking is technically possible,
> "but I don't feel like writing your program for you to
> "explain it.
>
> Seriously, wtf!?


Seriously, all he's doing is telling you that a few minutes effort on *your*
part will make the answer clear. Your nastiness in response just tells a lot
about you.

> Killfile me, I won't miss your rudeness.


There's the pot calling the kettle black. You're sure fond of the name
calling and finger-pointing and coy almost-obscenities. Why don't you just
grow up?

Too bad you will miss out on the useful advice given by Daniel and others.

--
Lew

Arne Vajh°j 12-12-2009 02:19 AM

Re: remote netcat talking to a Java app
 
On 11-12-2009 19:28, neuneudr@yahoo.fr wrote:
> On Dec 11, 9:39 pm, Daniel Pitts
> <newsgroup.spamfil...@virtualinfinity.net> wrote:
> ...
>> And I'm asking you to try something yourself, and then come back if it
>> doesn't work as expected.

>
> Which is mostly pointless when one doesn't know what to expect,
> which was *precisely* why I came posting to c.l.j.p...
>
> I came with a very precise question and your answer was
> basically "RTF ServerSocket Javadoc".


Which (assuming the docs does discuss the topic) is an excellent
suggestion.

> I'm sorry but I don't buy neither Lew's grammar-naziness
> nor your tone which I found rude in both your posts.


Since Lew did not participate in this thread there is
really not much point in dragging him into it.

> This is Usenet, people ask questions. If it's to answer
> "go to http://java.sun.com, I'm not spending time writing
> code for you" then you could even spare you the trouble
> of answering my posts.


Why?

He is giving good advice that future programmer could
take notice of.

Since it is the poster that has the problem, then it is
the posters job not the answers job to do the boring work
of providing the starting point of code.

> Such answers, honestly, won't be missed.


Sure they will.

Arrogant posters of questions that are too lazy or too dum
to write code that test and/or explain the problem will
on the other hand not be missed.

Arne


Knute Johnson 12-12-2009 02:27 AM

Re: remote netcat talking to a Java app
 
neuneudr@yahoo.fr wrote:
> Hi all,
>
> is it possible to have a Java app listen on a socket (?)
> and be *instantly* notified when a remote shell script
> netcats something on that socket?


Depends on your definition of instantly but UDP is going to be faster
than making a TCP/IP connection. Will it be really fast? Yes! You
could give some indication of what you think you need in terms of speed.

> I have control on the exact number of bytes sent by
> netcat. Say the shell script does this:
>
> echo "something" | netcat 10.0.0.39 3423
>
> And I've got a Java app configured to 'listen' on
> socket 3423, can I be instantly notified when the
> 9 letters of "something" have been netcat'ed.


Pretty darn fast.

> Imagine, out of simplicity (?) that it's always
> exactly the same number of bytes that are going
> to be sent by netcat and that netcat shall work
> in TCP mode.


OK, then it will be a little slower than UDP but the fixed length record
makes it pretty easy to know when to disconnect.

> What should I do on the Java side? Which class
> should I use and how can I do so that I don't need
> to 'poll' the socket but be instantly notified when
> something arrives on the socket? (it has to work
> on Java 5).


Typically if you don't have to do a lot of connections, you can have a
thread loop through ServerSocket.accept() and then another thread to
process the connection. If you need an example, post back.

> It is maybe a dumb question but I've got no idea
> as to how Java application listen on sockets and
> how they're supposed to act when something comes
> on the socket.
>
> Any explanation greatly appreciated : )


See above.

In terms of raw speed, UDP is going to be much faster since no
connection transaction or confirmation of data arrival is going to have
to take place as with TCP/IP. You could open a DatagramSocket and block
on a read. Then when a packet arrives you can process the data and wait
for the next packet. If the packets are all going to be small messages
or bits of data then I think it will be easier to use UDP but you have
the risk that UDP packets can be lost. That risk is pretty low on a
closed network but if you are sending these messages around the world,
the odd packet might get lost. If 100% packet arrival is not critical
then I would still suggest UDP for speed.

--

Knute Johnson
email s/nospam/knute2009/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access


All times are GMT. The time now is 10:15 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.