This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes but I don't want to read from the URL! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "but I don Watch "but I don New topic

but I don't want to read from the URL!

Josh Whiton

Joined: May 08, 2004
Posts: 1
I have written a very simple app to learn about java networking but it's not behaving as expected. The client program should take the string "hello", and open a URLConnection to my cgi script (perl). It should write to connection, the perl script should read the text, include a timestamp and write to a file. However, the perl script only completes when my Java program invokes "conection.getInputStream();" Even if Perl script has nothing to write to the URLConnection. I only want my Java prog to write the word "hello" to the connection. I do not want to read from it. That's it! Here's the important parts of the code.
String stringToSend = URLEncoder.encode("Hello","UTF-8");
URL url = new URL("http://myserver/cgi-bin/");
URLConnection connection = url.openConnection();

// set the connection so it can be written to

PrintWriter out = new PrintWriter(
out.print(stringToSend); //Writes to output stream.

//If i take this out the perl script never writes to the file!
<The Perl Script>
# Reads a string from an incoming URL connection and writes it and a
# timestamp to a file.
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
#Get the current system on the server
$date = `date`;
#Remove the new line character returned by date command
#Writes to a file
$filename = "textfile.txt";
open (FH,">>textfile.txt") || die "Error\n";
printf(FH "%s%s\n",$buffer,$date);
close FH;
exit 0;
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

I don' know the http-specifications, but I'm an experienced browser-user.
If I call a url in the browser, I expect some response - at least a 'page not found' or 'connection refused' response.
I guess if you 'misuse' the http-protocol, you should not complain about it
If I use the ticket-automat of the subway, I cannot make a contribution. I can put in some money, but I have to choose a ticket, or my money is refused. If I don't want a ticket, I still have to buy one, but I can throw it away
You may implement your own internet-protocol, using high (unreserved) port-numbers, but then you will have to implement the corresponding program on the server (which calls the perl...).
I agree. Here's the link:
subject: but I don't want to read from the URL!
jQuery in Action, 3rd edition