I want to receive and send data in ASCII how can I do? (Have the code)

I use my program connect to server (the server is .exe file) after I connect in ms-dos mode I want to send the command from client to server but nothing is happen such as I want to send MOVE 50 it means move 50cm nothing is back(may be because I haven't convert it to ASCII first) How can I change mycode to read and wrtie ASCII code?

publicclass proxy_server{

/** Creates a new instance of proxy_server */

publicstaticvoid main(String args[])throws IOException{

Socket sock=null;

PrintWriter out=null;

BufferedReader in=null;

sock =new Socket("localhost", 3000);

out=new PrintWriter(sock.getOutputStream(),true);

in=new BufferedReader(new InputStreamReader(sock.getInputStream()));

BufferedReader stdIn =new BufferedReader(new InputStreamReader(System.in));

String userInput;

while(( stdIn.readLine()!=null)){

userInput=stdIn.readLine();

out.println(userInput);

System.out.println("echo"+in.readLine());

}

out.close();

in.close();

stdIn.close();

sock.close();

}

}

[1943 byte] By [676206a] at [2007-10-3 11:32:21]
# 1
You are buffering the read and then reading a line using in.readline(). Are you sure that the response actually sends an end-of-line ?If I were doing this I would write in one thread and read in another. I would use minimum of buffering until I had basic communication working.
sabre150a at 2007-7-15 13:59:11 > top of Java-index,Archived Forums,Socket Programming...
# 2

Wrong:

while(( stdIn.readLine()!=null)){

userInput=stdIn.readLine();

}

Write:

while((userInput = stdIn.readLine()) != null && userInput.length() > 0){

// use 'userInput' here

hiwaa at 2007-7-15 13:59:11 > top of Java-index,Archived Forums,Socket Programming...
# 3
> while((userInput = stdIn.readLine()) != null && userInput.length() > 0)Delete the second test. Blank lines aren't a reason to exit the loop. Test for them inside the loop if you like.
ejpa at 2007-7-15 13:59:11 > top of Java-index,Archived Forums,Socket Programming...
# 4

> Blank lines aren't a reason to exit the loop

The stdin input often fails on null check, so we usually should use a blank-line-check as an exit condition.

Try this:

import java.io.*;

public class Stdin{

public static void main(String[] args) throws Exception{

String line = null;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

while ((line = br.readLine()) != null){

System.out.println("READ: " + line);

}

System.out.println("exitted the loop");

}

}

hiwaa at 2007-7-15 13:59:11 > top of Java-index,Archived Forums,Socket Programming...
# 5

> > Blank lines aren't a reason to exit the loop

> The stdin input often fails on null check, so we

> usually should use a blank-line-check as an exit

> condition.

Why? A null and a blank line aren't the same thing. A blank line might be perfectly valid data. Indeed it is in HTTP for example. If browsers behaved like this you wouldn't be able to read this post.

ejpa at 2007-7-15 13:59:11 > top of Java-index,Archived Forums,Socket Programming...
# 6
I was just talking about a primitive stdin input on console.Simple null check on readLine while loop often causes confusion among Java newbies becuase they can't exit the program.
hiwaa at 2007-7-15 13:59:11 > top of Java-index,Archived Forums,Socket Programming...
# 7
> I was just talking about a primitive stdin input on> console.> Simple null check on readLine while loop often causes> confusion among Java newbies becuase they can't exit> the program.This is the Socket Programming forum.
cotton.ma at 2007-7-15 13:59:11 > top of Java-index,Archived Forums,Socket Programming...
# 8

> we usually should use a blank-line-check as an exit condition.

Certainy not 'usually'. See below.

> I was just talking about a primitive stdin input on

> console.

But nobody else was, especially the OP.

> Simple null check on readLine while loop often causes

> confusion among Java newbies becuase they can't exit

> the program.

If and only if you define your operating instructions so that pressing ENTER is the way to exit the program, you would include the blank line check. Not otherwise, and certainly not 'usually'.

Otherwise they can always type Ctrl/Z or Ctrl/D to exit the program. And that's the correct way to clear up the confusion. Terminating the loop on a blank line just adds to the confusion

ejpa at 2007-7-15 13:59:11 > top of Java-index,Archived Forums,Socket Programming...
# 9

I thought it was rather obvious that I was talking about a specific/particular and limited context which OP's stdin code might typically represent. Small example codes for beginners often could use the [Enter] key effectively as an exit from console-input loop. That's so mediocre a topic, I think.

In a broader and more general context, I know top-ranked gurus on the forum like ejp are always right. I always learn a lot from them.

hiwaa at 2007-7-15 13:59:11 > top of Java-index,Archived Forums,Socket Programming...
# 10

im using same sort of code

and i can NEVER get out of that **** while loop

how are u ment to properly check to see if the data stream has ended not shut down but just finished sending its current packet

im sending html sorta packets so cant check for /r/n n stuff

this != null seams to do FUUUUK all sure as *** dont exit the loop

russdxa at 2007-7-15 13:59:11 > top of Java-index,Archived Forums,Socket Programming...