• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...).
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic