• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1212
  • Last Modified:

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();
    }
  }

Open in new window

0
kncoughlin
Asked:
kncoughlin
  • 5
  • 4
  • 2
1 Solution
 
CEHJCommented:
You can just call out.print, yes. Did you try it?
0
 
kncoughlinAuthor Commented:
out.print does not reach server
0
 
CEHJCommented:
Did you try


out.print(..);
out.flush();

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
KuldeepchaturvediCommented:
try out.flush after the write..

0
 
kncoughlinAuthor Commented:
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?
0
 
CEHJCommented:
It usually does need it, yes. But what problem exactly is caused by using it?
0
 
KuldeepchaturvediCommented:
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).


0
 
kncoughlinAuthor Commented:
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'.
0
 
CEHJCommented:
Try the following instead. See if it works any better
out.printf("%s\n", userInput);

Open in new window

0
 
kncoughlinAuthor Commented:
THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
0
 
CEHJCommented:
:-)

That's really a fault in the receiving app - it shouldn't be too picky about line feeds. CRLF is standard with most protocols
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now