这个是从客户端向服务器断写数据
class Write2Server extends Thread{
OutputStream out;
public Write2Server(OutputStream out){
this.out=out;
}
@Override
public void run() {
while(true){
Scanner scanner=new Scanner(System.in);
String time=new SimpleDateFormat("yyyy-MM-dd hh:mm").format(new Date(System.currentTimeMillis()));
try {
out.write(("client"+"\t"+time+"\n"+scanner.nextLine()).getBytes());
out.write('\n');
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
这个是服务器端的读取
class ReadFromClient extends Thread{
InputStream in;
public ReadFromClient(InputStream in){
this.in=in;
}
public void run() {
int b;
try{
while((b=in.read())!=-1){
System.out.write(b);
}
}catch(Exception e){

}
}
}然后问题就是在客户端输入中文,在服务端就乱码了,小菜一个,望大度赐教

解决方案 »

  1.   

    你先检查下2个.java文件的编码是否相同,再检查下服务端和客户端的系统编码是否相同
      

  2.   

    这样改下试试:
    我没测试
    class ReadFromClient extends Thread{
            InputStream in;
            public ReadFromClient(InputStream in){
                this.in=in;
            }
            public void run() {
                byte[] b1=new byte[32];//缓冲数组
        int b;
                try
        {
                    while((b=in.read(b1))!=-1){
    String s=new String(b1,"gbk");//组成字符串。
                        System.out.write(s);
                    }
                }catch(Exception e){
                     
                }
            }
        }
      

  3.   

    试了,不行,不过我昨天把文件和对应的编码改成GBK以后就行了,代码没变的