Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: Java I/O for Networking

Reply
Thread Tools

Re: Java I/O for Networking

 
 
Robert Olofsson
Guest
Posts: n/a
 
      08-22-2003
Erwin ((E-Mail Removed)) wrote:
: When trying to communicate with Web server, and using socket to send and
: receive data, I am confused which method is best to send and receive data
: from the server as there are plenty of methods and classes available to
: choose.

Talking to a web server is done with HTTP so I would suggest that you
start by reading RFC2616 so you know what you need.
Basically HTTP uses 8-bit character set. This means that if you send
data with a writer you will(/may) send unicode to the server and that
may be data that takes several bytes => server may get confused...
This means that I have to suggest that you use byte buffers and write
to the server (that is do _not_ use the print/println methods).

As for the data in the request or response, that can be considered
binary unless you plan to read it (in which case you know how to parse
it already).

If all you want to do is talk to a web server then starting with
java.net.URLConnection may be enough. Other than that you have some
code in the apache/jakarta commons package. And I have written a web
proxy that supports the full HTTP/1.1 including caching that can be
found at http://www.khelekore.org/rabbit/.

/robo
 
Reply With Quote
 
 
 
 
Jon A. Cruz
Guest
Posts: n/a
 
      08-23-2003
Robert Olofsson wrote:

> Basically HTTP uses 8-bit character set.


No.

HTTP itself uses a 7-bit character set for the HTTP specific stuff (headers)

Then it transports 8-bit bytes in the body.

So two things. One is that HTTP itself deals with 7-bit characters only.
The other that a "byte" is not identical to "char". Those are two
different concepts that are sometimes mapped so that they overlap, but
are still different things.


> This means that if you send
> data with a writer you will(/may) send unicode to the server and that
> may be data that takes several bytes => server may get confused...
> This means that I have to suggest that you use byte buffers and write
> to the server (that is do _not_ use the print/println methods).


Better than that.

When sending text types (any MIME type starting with "text/"), just use
an explicit character encoding when creating the OutputStreamWriter and
be sure to name that encoding using the HTTP charset naming in the HTTP
header that gets sent out.

PrintWriter sending = new PrintWriter(new OutputStreamWriter(
connection.getOutputStream(), "UTF8" ));

Do not use println, as it will use a default platform (client computer)
line ending. Use print() and use the appropriate line ending (usually
either "\r\n" or "\n" ).


For binary types (non-text types), do not use a Writer, and use write().


 
Reply With Quote
 
 
 
 
Robert Olofsson
Guest
Posts: n/a
 
      08-24-2003
Jon A. Cruz ((E-Mail Removed)) wrote:
: Robert Olofsson wrote:

: > Basically HTTP uses 8-bit character set.
: No.
: HTTP itself uses a 7-bit character set for the HTTP specific stuff (headers)

check RFC2616 (http://www.ietf.org/rfc/rfc2616.txt) you will find
this (only a few lines, read the whole text for missing pieces):

OCTET = <any 8-bit sequence of data>
TEXT = <any OCTET except CTLs, but including LWS>
quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
qdtext = <any TEXT except <">>
message-header = field-name ":" [ field-value ]
field-name = token
field-value = *( field-content | LWS )
field-content = <the OCTETs making up the field-value
and consisting of either *TEXT or combinations
of token, separators, and quoted-string>


So I would say that HTTP uses an 8-bit character set. All commonly
used headers are in 7-bit ASCII, but extensions may use the
8-bit and are allowed to do that.

: Then it transports 8-bit bytes in the body.

: So two things. One is that HTTP itself deals with 7-bit characters only.
: The other that a "byte" is not identical to "char". Those are two
: different concepts that are sometimes mapped so that they overlap, but
: are still different things.

Correct a byte is the smallest adressable unit and may be 8, 12, 16 or
even 36 bits depending on the hardware in java a byte is 8 bits.

: Do not use println, as it will use a default platform (client computer)
: line ending. Use print() and use the appropriate line ending (usually
: either "\r\n" or "\n" ).

This is correct. Many web servers allow headers with only \n or only
\r since there are lot of broken clients. One should try to be correct
though.

/robo
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Internet networking , group networking,business leads,new business opportunities,like having many sales peoples elie Computer Support 0 08-18-2007 10:12 AM
networking two pc together with wireless networking ? Andy Wireless Networking 4 02-26-2007 06:02 PM
PROBLEMS WITH NETWORKING - NEW TO NETWORKING =?Utf-8?B?QUJTUE9QVVA=?= Wireless Networking 2 03-23-2005 08:31 AM
[Reminder] Online chat with Networking & Devices VP about Networking in Longhorn Eddy Malik [MSFT] Wireless Networking 0 03-22-2005 03:27 AM
networking 2 pc with a networking hub and 1 internet connection barry crowley Computer Support 20 02-27-2004 05:09 AM



Advertisments