This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "but I don Watch "but I don New topic
Author

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

Josh Whiton
Greenhorn

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/nettest.pl");
URLConnection connection = url.openConnection();

// set the connection so it can be written to
connection.setDoOutput(true);

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

//If i take this out the perl script never writes to the file!
connection.getInputStream();
...
<The Perl Script>
#!/usr/bin/perl
#
# 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
chop($date);
#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...).


http://home.arcor.de/hirnstrom/bewerbung
 
jQuery in Action, 2nd edition
 
subject: but I don't want to read from the URL!
 
Similar Threads
Executing cgi script from applet
Executing cgi script from applet
Writing to a web server
Executing cgi script from applet
cgi applet communication problems