473,662 Members | 2,581 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

outputStream.fl ush() not flushing?

9 New Member
Hello,
I'm trying to send a string to a subprocesses' output stream in my java program. I'm writing the data to the stream and then trying to flush the stream.

This, however , isn't actually sending anything to the subprocess (a python program) and the data only actually gets sent to the outputstream when I close the stream. I'm wanting to keep sending data to the subprocess so the writing and flushing is in a loop, but because I have to close the stream for the data to be sent the loop only works on the first iteration.

I found other people having the same problem on the internet but I'm not sure if it's machine specific or down to the code. I want to be able to send data to the subprocesses' output stream without closing the stream so it can be used again in the next iteration (this is what flush is supposed to do).

Thank you
Nico
Nov 2 '06 #1
6 17209
r035198x
13,262 MVP
Hello,
I'm trying to send a string to a subprocesses' output stream in my java program. I'm writing the data to the stream and then trying to flush the stream.

This, however , isn't actually sending anything to the subprocess (a python program) and the data only actually gets sent to the outputstream when I close the stream. I'm wanting to keep sending data to the subprocess so the writing and flushing is in a loop, but because I have to close the stream for the data to be sent the loop only works on the first iteration.

I found other people having the same problem on the internet but I'm not sure if it's machine specific or down to the code. I want to be able to send data to the subprocesses' output stream without closing the stream so it can be used again in the next iteration (this is what flush is supposed to do).

Thank you
Nico
From the docs

Expand|Select|Wrap|Line Numbers
  1. The flush method of OutputStream does nothing. 
Have a look at the streams and pick the one which provides an implementation of flush in its contract
Nov 2 '06 #2
tegdim
9 New Member
From the docs

Expand|Select|Wrap|Line Numbers
  1. The flush method of OutputStream does nothing. 
Have a look at the streams and pick the one which provides an implementation of flush in its contract
Thanks, I gave it a try with a BufferedOutputS tream which should implement the abstract flush() method and it still didn't work. I'll fiddle around with some other ones though and see if they help.
Thanks
Nov 2 '06 #3
r035198x
13,262 MVP
Thanks, I gave it a try with a BufferedOutputS tream which should implement the abstract flush() method and it still didn't work. I'll fiddle around with some other ones though and see if they help.
Thanks
Yuck. According to the contracts, anything extending FilterOutputStr eam should work, if BufferedOutputS tream wouldn't do it, then I suppose the problem is elsewhere.
Nov 2 '06 #4
tegdim
9 New Member
Yuck. According to the contracts, anything extending FilterOutputStr eam should work, if BufferedOutputS tream wouldn't do it, then I suppose the problem is elsewhere.
Thanks for the fast replies, I'm still new to IO coding so no doubt it'll be some little mistake in my code. It's just annoying that it works for the first iteration if i just close the stream after writing to it. Thanks again :)
Nov 2 '06 #5
horace1
1,510 Recognized Expert Top Contributor
I tend to use sockets (TCP virtual circuit or UDP datagrams) or RMI to pass information between process (and they don't have to be on the same machine), see
http://java.sun.com/docs/books/tutorial/networking/sockets/index.html
http://java.sun.com/javase/technologies/core/basic/rmi/index.jsp
Nov 4 '06 #6
horace1
1,510 Recognized Expert Top Contributor
Example using UDP datagrams to send data from a Java program to a Python program

this Python program binds to UDP socket 999, it then wait for datagrams and displays the contents
Expand|Select|Wrap|Line Numbers
  1. from socket import *
  2.  
  3. receiverSocket=socket(AF_INET, SOCK_DGRAM)
  4. receiverSocket.bind(('',999))
  5.  
  6. while True :                   # from_physical
  7.    data, address = receiverSocket.recvfrom(1024)
  8.    if not data : break
  9.    print data                   # to_network
  10. receiverSocket.close()
  11.  
  12.  
this Java program reads a string from the keyboard, creates a datagram and sends it to UDP socket 999 on the same machine, i.e. IP address 127.0.0.1
Expand|Select|Wrap|Line Numbers
  1. // simple client sending a string in a datagram to a server
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. import java.net.*;
  6.  
  7. public class UDPclient
  8. {
  9.  
  10. private final static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  11. public static void main(String args[])
  12. {
  13.    String remoteIPaddress="127.0.0.1";
  14.    int remotePort = 999;   
  15.    System.out.println("enter text to transmit");
  16.    while(true)
  17.        try
  18.           {
  19.            String s = in.readLine();
  20.            byte[] data = s.getBytes();                            // get array with data
  21.            DatagramSocket theSocket = new DatagramSocket();       // create datagram socket
  22.            // build the datagram packet
  23.            DatagramPacket   theOutput = new DatagramPacket(data, data.length, InetAddress.getByName(remoteIPaddress), remotePort);
  24.            System.out.println("sendFrame: sent " + s + " to host " + remoteIPaddress + " socket " + remotePort);
  25.            theSocket.send(theOutput);                             // and send the datagram
  26.           }  // end try
  27.        catch (IOException e) {System.err.println("error " +  e); }
  28. }
  29. }
  30.  
Nov 4 '06 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
13772
by: Zoury | last post by:
Hi folks ! :O) I'm trying to show a PDF in ASP.NET but I can't get to work properly. here's a portion of my code : '** Dim ms As MemoryStream = DirectCast(m_report.FormatEngine.ExportToStream(reqContext), MemoryStream) If (fileName Is Nothing) Then
0
1665
by: ruhunu Gamarala | last post by:
Hi, I get the following exception periodically. does anybody what is the reason for it to throw this exception? I really appriciate if anyone can help me on this. thanks, Chinthaka at org.postgresql.PG_Stream.flush(PG_Stream.java:356) at org.postgresql.core.QueryExecutor.sendQuery(QueryExecutor.java:159) at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:70)
30
45791
by: Jonathan Neill | last post by:
I'm aware that there is no ANSI (or POSIX, or any standard, to my knowledge) way of flushing stdin or any other application-level input buffer, but if anyone knows a hack or a non-portable way to do this, no matter how obscure or arcane, I'd appreciate it.
11
14629
by: Darklight | last post by:
is this correct or incorrect i just read the post below before posting this: In fflush(stdin), what happens to flushed data? this program is taken from sams teach yourself c in 21 days /* LIST13-6.C CLEARING STDIN OF EXTRA CHARACTERS */ /* USING FFLUSH() FUNCTION */ #include<stdio.h> int main(void)
11
11383
by: Random | last post by:
I have tried all the Response methods I can think of (WriteFile, BinaryWrite, OutputStream) to write the byte array of a pdf file to the response. The result looks like it's trying, it comes up as a blank PDF file (without the Adobe toolbar that normally comes up). Here is the code I am using... '...fPDF is my Byte array '...I've already verified it is valid by writing the array to a physical file ("result.pdf") and opening it...
4
5851
by: Mantas Miliukas | last post by:
Hi, I have problem when flushing the generated HTML code to the client. It seems that "Page.Response.Flush()" method doesn't work at all. See my code below: protected override void Render(HtmlTextWriter writer) { for (int i = 0; i < 100; i ++) {
1
4158
by: richardtallent | last post by:
(ASP.NET 2.0. Issue presents both on my copy of IIS on my workstation (XP Pro) and my production server (Win2k3). I don't use any proxy servers, anti-spyware apps, etc. Problem occurs using IE6SP2 and Firefox 1.5.02.) I'm writing large, sever-generated XML files to the browser. This method works: Dim xmltxt As New System.IO.StringWriter
3
1870
by: joelcarrier | last post by:
I'm opening up a subprocess like this where slave.py is a text based app that receives commands and responds with output: r, w, e = popen2.popen3('python slave.py') I need to send slave.py a command and see the output, so I'll do something like: w.write("command here") then i'll try this:
5
1909
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
(a little more info) after flushing the line (instead of closing streamwirter) it takes some x time to flush the line so, when I read from the file I cannot find the last written line. if I close the streamwriter it does find the line always. but , my next write fails "Cannot write to a closed TextWriter"
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8344
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8546
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7367
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5654
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.