Send string to IP address

  • Thread starter Dirk Bruere at NeoPax
  • Start date
D

Dirk Bruere at NeoPax

Just trying to quickly hack together a demo prog.
The idea is to create an applet so that when a button is pressed a text
string is sent to a given IP address eg 192.168.0.2:6000 in either UDP
or TCP/IP form

Can anyone give me a quick pointer to some example code for the network
stuff, or where to look? I haven't used Java for a decade (pre-Swing!)
and am more than rusty.

--
Dirk

http://www.transcendence.me.uk/ - Transcendence UK
http://www.theconsensus.org/ - A UK political party
http://www.onetribe.me.uk/wordpress/?cat=5 - Our podcasts on weird stuff
 
D

Dirk Bruere at NeoPax

Peter said:
Surely you meant "in either UDP or TCP form". TCP/IP is the lower-level
protocol on which TCP and UDP are sent.

See Socket (TCP) and DatagramSocket (UDP). The phrase "text string" is
ambiguous without knowing the encoding. You'll want to look at
CharsetEncoder to convert a Java string to bytes you can send. Of
course, you'll need to know what encoding the server is expecting, so
that you can use an appropriate encoder.

Pete

Thanks.
It's just an ascii string, or plain hex byte

--
Dirk

http://www.transcendence.me.uk/ - Transcendence UK
http://www.theconsensus.org/ - A UK political party
http://www.onetribe.me.uk/wordpress/?cat=5 - Our podcasts on weird stuff
 
D

Dirk Bruere at NeoPax

Peter said:
Well, which is it?

"Plain hex" implies something formatted as text, but doesn't answer the
question of encoding.

There's no "just" as far as "an ASCII string" is concerned. ASCII is
every bit as legitimate an encoding as any other, and just as
problematic too.

"Byte" seems to imply that you might be dealing with binary data, but
you're unclear as to how you're dealing with it. Do you want to
transmit the bytes in their original form? Or do you want them
formatted as "plain hex" and transmitted as a string?

Pete

I'll probably hand translate the ascii to hex and code as a byte string

--
Dirk

http://www.transcendence.me.uk/ - Transcendence UK
http://www.theconsensus.org/ - A UK political party
http://www.onetribe.me.uk/wordpress/?cat=5 - Our podcasts on weird stuff
 
D

Dirk Bruere at NeoPax

Matt said:
You say applet, so be aware that an unsigned applet can only connect back to
the server it came from. To send to any machine use an application (or JWS)
or otherwise sign the applet and request permission.

Matt Humphrey http://www.iviz.com/

When you say it can only connect back to the server, do you mean the
actual server program on the machine, or the machine itself?
Can I choose to send it to a different port on the machine?

--
Dirk

http://www.transcendence.me.uk/ - Transcendence UK
http://www.theconsensus.org/ - A UK political party
http://www.onetribe.me.uk/wordpress/?cat=5 - Our podcasts on weird stuff
 
O

Owen Jacobson

I'll probably hand translate the ascii to hex and code as a byte string

Welcome to the future. Characters are not bytes and bytes are not
characters. The sooner you stop conflating them in your head, the
better off you'll be.

IO in general, including network IO, is done in bytes through the
InputStream and OutputStream categories of classes. Strings are done in
characters through the Reader and Writer categories. In order to
translate a character sequence into a byte sequence, you use an
encoding. "ASCII" is one such encoding, as are "ISO-8859-15" and
"UTF-8". There is no such thing as "no encoding"; even a simple
hand-hacked one based on the binary representations of characters in
Java is going to be close to, but subtly incompatible with[0], UTF-16.

Fortunately, Java provides some extremely high-quality tools for
encoding and decoding character data to and from byte data. For simple,
stream-based applications, there are the StreamReader and StreamWriter
classes. A simple example that writes strings to a byte stream might
look like this:

// assume the existance of OutputStream out
Writer writeOut = new OutputStreamWriter (out, "UTF-8");
writeOut.write ("Hello, wörld.");

Normally you'd create the Writer once at the same time as you create
the underlying stream, rather than every time you write some text,
obviously. There's a similar pattern for InputStreamReader.

To tie this back to your original question, you can use this in tandem
with the Socket getInputStream and getOutputStream methods. The socket
ensures that bytes written to the output stream are transmitted to the
peer at < other end; your code is responsible for translating the data
you want to send into bytes, and since you're just trying to move
unstructured strings around, a StreamWriter is a perfect adapter.

You almost certainly want to use UTF-8. For characters whose code
points are between U+0000 and U+007F, UTF-8 and ASCII encodings are
byte-for-byte identical. However, UTF-8 can encode the entire Unicode
space (containing over 65,536 distinct code points), whereas ASCII can
*only* encode the first 127 code points.

Joel on software has a surprisingly good explanation of all of this
that's worth reading:
http://www.joelonsoftware.com/articles/Unicode.html

Cheers,
-o

[0] Probably.
 
A

Andreas Leitgeb

Owen Jacobson said:
You almost certainly want to use UTF-8. For characters whose code
points are between U+0000 and U+007F, UTF-8 and ASCII encodings are
byte-for-byte identical. However, UTF-8 can encode the entire Unicode
space (containing over 65,536 distinct code points), whereas ASCII can
*only* encode the first 127 code points.

I wonder, if there was a "binary" encoding with these properties:
- the operation encode(decode(X))==X for any bytestream X
- It is homomorphic with respect to concatenation
(each byte is treated on its own, no ligatures)
- Each byte gets translated to no more than a char
(the "not less" already follows from the former rules)
- Ideally, it would be compatible with casting byte variables
to char variables, but I'm not sure if this is feasible
for an encoding.

A reason for this could be to use Strings as containers for
binary data, with some conveniences available like picking
substrings, simple concatenating and indexOf().

Of course it shouldn't be used for real natural language text.
Thanks for the link! Very good read!
 
D

Dirk Bruere at NeoPax

Matt said:
When you say it [the applet] can only connect back to the server, do you
mean the actual server program on the machine, or the machine itself?
Can I choose to send it to a different port on the machine?

The same IP address. Different port is ok. The restriction is imposed by
the client JVM's security manager.

Excellent! All I need to do then is point my PDA at the applet on the
host machine, download it, and then use it to send my command strings
back to the same machine on a specific port.

Basically all I am doing is replacing a Microsoft IR remote control with
a WiFi connected PDA for the same functionality with a specific program.

--
Dirk

http://www.transcendence.me.uk/ - Transcendence UK
http://www.theconsensus.org/ - A UK political party
http://www.onetribe.me.uk/wordpress/?cat=5 - Our podcasts on weird stuff
 
E

Eric Sosman

Peter said:
Surely you meant "in either UDP or TCP form". TCP/IP is the lower-level
protocol on which TCP and UDP are sent.

UDP (connectionless) rests atop TCP/IP (virtual connections)?
Well, you learn something new every day ...
 
A

Arne Vajhøj

Peter said:
Surely you meant "in either UDP or TCP form".

If "or" takes precedence over "/" in English, then what he wrote is
correct.
> TCP/IP is the lower-level
protocol on which TCP and UDP are sent.

Unlike this.

TCP and UDP are send over IP not over TCP/IP.

(unless you are using some unusual tunneling over TCP)

Arne
 
O

Owen Jacobson

I wonder, if there was a "binary" encoding with these properties:
- the operation encode(decode(X))==X for any bytestream X
- It is homomorphic with respect to concatenation
(each byte is treated on its own, no ligatures)
- Each byte gets translated to no more than a char
(the "not less" already follows from the former rules)
- Ideally, it would be compatible with casting byte variables
to char variables, but I'm not sure if this is feasible
for an encoding.

A reason for this could be to use Strings as containers for
binary data, with some conveniences available like picking
substrings, simple concatenating and indexOf().

Of course it shouldn't be used for real natural language text.

Thanks for the link! Very good read!

Sure. Any of the ANSI codepages would do - most of them specify 256
characters. If you've ever used MS DOS or QBASIC, you've seen the one
IBM picked, containing lots of line-drawing characters and a handful of
useful accents.

Pick one and run with it.

If you only want to do a value conversion, you can just cast back and
forth, too. Treating the resulting characters as meaningful text is
probably a bad idea, but then, if you're storing byte data in strings,
you're already in bat country.

-o
 
A

Arne Vajhøj

Peter said:
If you're suggesting I should parse the text as "(UDP or TCP) / IP",
I'll agree with the "if/then" statement you've made. I don't see why I
should choose that interpretation though. "TCP/IP" has a very specific
meaning, and typographically it's perfectly natural to interpret the
text as "(UDP) or (TCP/IP)".

The OP can feel free to clarify the order of precedence if he likes;
I'll accept whatever meaning he says he intended. Absent such a
clarification, I won't be choosing your interpretation of the intended
precedence.

But maybe you should not correct other based on your assumptions
about a certain precedence order !?

Arne
 
A

Arne Vajhøj

Peter said:
[...]
I'll accept whatever meaning he says he intended. Absent such a
clarification, I won't be choosing your interpretation of the
intended precedence.

But maybe you should not correct other based on your assumptions
about a certain precedence order !?

I don't see how you come to that conclusion. And I certainly don't.

Inasmuch as I made an assumption at all, it was founded in the natural
way to interpret the text. Your alternative assumption is logically
sound given certain assumptions of your own, but completely ignores the
foundational aspects of human languages and context.

I disagree about that.

It seemed rather obvious to me what he meant.
"If you assume that '+' has a higher precedence than '*', then the
expression 1 + 2 * 3 has the value 9". That statement is logically
true, but in context doesn't mean that one should not assume the actual
value of the expression is really 7. Just because you _can_ redefine
things contrary to normal usage, that doesn't mean you _should_.

Both math and programming languages have very precise definitions
of operator precedence.

You do not make assumptions about that. They are well defined.

English or any other languages on the other hand does not have
such definitions.

So the argument is completely bogus.

Arne
 
R

Roedy Green

Just trying to quickly hack together a demo prog.
The idea is to create an applet so that when a button is pressed a text
string is sent to a given IP address eg 192.168.0.2:6000 in either UDP
or TCP/IP form

Can anyone give me a quick pointer to some example code for the network
stuff, or where to look? I haven't used Java for a decade (pre-Swing!)
and am more than rusty.

For some sample UDP code, see
http://mindprod.com/products1.html#SETCLOCK

for same sample TCP HTTP code see
http://mindprod.com/products1.html#HTTP
http://mindprod.com/products1.html#SUBMITTER
http://mindprod.com/products1.html#VERCHECK
http://mindprod.com/products1.html#BROKENLINKS

also see http://mindprod.com/jgloss/http.html


--
Roedy Green Canadian Mind Products
http://mindprod.com

One path leads to despair and utter hopelessness. The other,
to total extinction. Let us pray we have the wisdom to choose correctly.
~ Woody Allen .
 
M

Mark Space

When I write TCP/IP I mean TCP over IP


That to me was the obvious interpretation. Given that the UDP interface
basically just exposes lower level IP methods, it's a pretty clear
distinction.
 
D

Dirk Bruere at NeoPax

A

Andreas Leitgeb

Sure. Any of the ANSI codepages would do - most of them specify 256
characters.
Even those in range 128-159, which the iso-8859-* sets usually exclude?
If you've ever used MS DOS or QBASIC, you've seen the one
IBM picked, containing lots of line-drawing characters and a handful of
useful accents.

I did, loooong time ago, and even more recently, when converting utf-8
encoded text files for printing on my old needle printer that only knows
a couple of those OEM-charsets.
Anyway, those charsets with line-drawing chars in them do definitely not
fulfil the fourth one of my wishes.

Well, I guess I'll just try it with some ANSI ...

Thanks, anyway.
If you only want to do a value conversion, you can just cast back and
forth, too.

That's what I hoped that some stock-encoding would exactly do for
really *all* byte values.
Treating the resulting characters as meaningful text is
probably a bad idea,

Probably? Quite surely! :)
but then, if you're storing byte data in strings,
you're already in bat country.

Let's say "Right at the borderline of bat country."
 
O

Owen Jacobson

Even those in range 128-159, which the iso-8859-* sets usually exclude?

The relevant ANSI codepages specify glyphs for each byte value. The
characters whose byte values are in 128-255 are not guaranteed to map
to U+0080-U+00FF, though, and often don't.
I did, loooong time ago, and even more recently, when converting utf-8
encoded text files for printing on my old needle printer that only knows
a couple of those OEM-charsets.
Anyway, those charsets with line-drawing chars in them do definitely not
fulfil the fourth one of my wishes.

Yeah, true. The only encoding that's going to be compatible with that
point is the trivial one whose operations are

encode (c) = (byte) c, c in [0, 256)
decode (b) = (char) b
That's what I hoped that some stock-encoding would exactly do for
really *all* byte values.

http://java.sun.com/javase/6/docs/technotes/guides/intl/encoding.doc.html

The various Cp* encodings might do it. Try Cp850, 852, or 1252 (most
likely candidate). CP1252 is the encoding Windows refers to as "ANSI"
(see http://blogs.msdn.com/oldnewthing/archive/2004/05/31/144893.aspx
for details on THAT one), corresponding to the default ANSI codepage
for IBM PCs in north america and (I think) western europe.

Cheers,
-o
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Change character in string 105
Official Java Classes 10
Slightly tricky string problem 18
Delay 2
Can an Applet beep? 4
Sorting a JList 4
ListModel name 10
File over network timeout 3

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top