Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Print a null character

807600Aug 1 2007 — edited Aug 2 2007
Okay, so I open a socket and connect to a networked machine that accepts strings as commands. Commands are terminated by the null character '\0'. I know this works as I've done it in Python with no problems. Right now all I'm trying to do is write an ascii char "0" followed by a null. My machine isn't responding and I'm pretty sure its because Java is ignoring my '\0' and just writing the characters preceding it.
   public void connect() throws IOException{
        System.out.println("Trying to connect...\n");
        //connect to roobt
        Socket outsock = new Socket("13.62.154.243", 15223);
        Socket insock = new Socket("13.62.154.243",15224);
        //setup reading/writing.
        PrintWriter out = new PrintWriter(outsock.getOutputStream());
        BufferedReader in = new BufferedReader(new InputStreamReader(insock.getInputStream()));
        String r = in.readLine();
        System.out.println("Received: " + r + "\n");
        //inform robot that connection is closing
        out.print("0\0");
        //close sockets
        outsock.close();
        insock.close();
    }
Am I wrong? Is there a way to force the null character to be written?

And as an aside, is there a way to read lines terminated by a null as opposed to a newline \n or carriage return \r? I can modify the code on the machine to respond with lines terminated by \n's, but I'd rather not have to redo it all. I wrote the code for my machine to function with a Python socket script, and now I'm attempting to rewrite this into Java.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 30 2007
Added on Aug 1 2007
7 comments
387 views