kncoughlin
asked on
Java Sockets CR/LF
I have a Solaris service which, thru tcpmux, calls a shell script which receives text from a VB app (using Winsock). I am rewriting the client side...instead of VB we are using Java. I have established a connection back to the Sun...but when I send text the println is attaching a cr/lf which is screwing up my shell script. Below is java code. Is there a change I can make to keep back and forth communication but use print instead of println (to avoid cr/lf)??
import java.io.*;
import java.net.*;
public class myclient {
public static void main(String[] args)
throws IOException {
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
DataOutputStream os = null;
try{
InetAddress Sun = InetAddress.getByName("mysun");
echoSocket=new Socket(Sun,1);
out = new PrintWriter(echoSocket.getOutputStream(),true);
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
} catch (UnknownHostException e){
System.err.println("Cannot connect to Sun");
System.exit(1);
} catch (IOException e){
System.err.println("Cannot connect to Sun");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while((userInput = stdIn.readLine()) != null){
out.println(userInput);
System.out.println(in.readLine());
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}
You can just call out.print, yes. Did you try it?
ASKER
out.print does not reach server
Did you try
out.print(..);
out.flush();
try out.flush after the write..
ASKER
The out.flush does not work. To get to communicate it has to use the println. I'm wondering if I should change input/output types?
It usually does need it, yes. But what problem exactly is caused by using it?
from docs
public PrintWriter(OutputStream out,
boolean autoFlush)
Create a new PrintWriter from an existing OutputStream. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will convert characters into bytes using the default character encoding.
Parameters:
out - An output stream
autoFlush - A boolean; if true, the println, printf, or format methods will flush the output buffer
given that you are using this constructor. using println = print + flush as CHEJ and I have suggested so it should be behaving same as println sans CR\LF
( @ CHEJ sorry I reposted your suggestion but I didnt see it when I was typing).
public PrintWriter(OutputStream out,
boolean autoFlush)
Create a new PrintWriter from an existing OutputStream. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will convert characters into bytes using the default character encoding.
Parameters:
out - An output stream
autoFlush - A boolean; if true, the println, printf, or format methods will flush the output buffer
given that you are using this constructor. using println = print + flush as CHEJ and I have suggested so it should be behaving same as println sans CR\LF
( @ CHEJ sorry I reposted your suggestion but I didnt see it when I was typing).
ASKER
For example, I'm sending to the unix script an string that lets it know what directory to 'cd' into.
read mypath
cd $mypath
The cr/lf added to the end of the path causes a 'path not found'.
read mypath
cd $mypath
The cr/lf added to the end of the path causes a 'path not found'.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
THANK YOU!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!
:-)
That's really a fault in the receiving app - it shouldn't be too picky about line feeds. CRLF is standard with most protocols
That's really a fault in the receiving app - it shouldn't be too picky about line feeds. CRLF is standard with most protocols