日期:2014-05-20 浏览次数:20798 次
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){
}
}
}
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){
}
}
}