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;