本帖最后由 刘张朋 于 2013-9-3 11:10 编辑
我在练习tcp传输,将客户端输入的字符串由服务端转换成大写返回给客户端时发现:如果把输入法切换到汉字输入法后,读取到的键盘录入的根本就不是自己输入的字符串,而且输了几次后就不能在输入了,这是怎么回事呢?
下面是显示结果:下面的图不知道怎么放大,附件里面有一份
客户端源码:
package transtext;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class TransClient {
/**
* @param args
*/
public static void main(String[] args) {
Socket s = null;
BufferedReader bufin = null;
try {
s = new Socket(InetAddress.getByName("lzp"),10003);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bufin = new BufferedReader(new InputStreamReader(System.in));
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = null;
while((line=bufin.readLine())!=null){
System.out.println(line);//汉字输入法是这里读到的不是输入的字符串
if(line.equals("over"))
break;
out.write(line);
out.newLine();
out.flush();
String serverReturn = in.readLine();
System.out.println(serverReturn);
}
} catch (UnknownHostException e1) {
e1.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(bufin!=null) bufin.close();
if(s!=null) s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
|
|