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
6 17209
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 - 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
From the docs - 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
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.
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 :)
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
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 -
from socket import *
-
-
receiverSocket=socket(AF_INET, SOCK_DGRAM)
-
receiverSocket.bind(('',999))
-
-
while True : # from_physical
-
data, address = receiverSocket.recvfrom(1024)
-
if not data : break
-
print data # to_network
-
receiverSocket.close()
-
-
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 -
// simple client sending a string in a datagram to a server
-
-
import java.io.*;
-
import java.util.*;
-
import java.net.*;
-
-
public class UDPclient
-
{
-
-
private final static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
-
public static void main(String args[])
-
{
-
String remoteIPaddress="127.0.0.1";
-
int remotePort = 999;
-
System.out.println("enter text to transmit");
-
while(true)
-
try
-
{
-
String s = in.readLine();
-
byte[] data = s.getBytes(); // get array with data
-
DatagramSocket theSocket = new DatagramSocket(); // create datagram socket
-
// build the datagram packet
-
DatagramPacket theOutput = new DatagramPacket(data, data.length, InetAddress.getByName(remoteIPaddress), remotePort);
-
System.out.println("sendFrame: sent " + s + " to host " + remoteIPaddress + " socket " + remotePort);
-
theSocket.send(theOutput); // and send the datagram
-
} // end try
-
catch (IOException e) {System.err.println("error " + e); }
-
}
-
}
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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)
|
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.
|
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)
|
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...
| |
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 ++)
{
|
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
|
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:
|
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"
|
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...
|
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,...
| |
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,...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |