服务器发送一条数据如:
java代码
bufferedreader in = new bufferedreader(new inputstreamreader(client.getinputstream()));
printwriter out = new printwriter(client.getoutputstream());
out.println("没有查到该数据。");//向客户端发送
out.flush();
bufferedreader in = new bufferedreader(new inputstreamreader(client.getinputstream()));
printwriter out = new printwriter(client.getoutputstream());
out.println("没有查到该数据。");//向客户端发送
out.flush();
然后客户端接收:
java代码
public bufferedreader recieve(){
//从服务端程序接收数据
inputstreamreader isr=null;
bufferedreader br=null;
try {
isr = new inputstreamreader(server.getinputstream());//这里需要gbk
br = new bufferedreader(isr);
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
return br;
}
///
bufferedreader br=recieve();
while((message = br.readline()) != null){
messages+=message;
}
system.out.println("messages="+messages);
public bufferedreader recieve(){
//从服务端程序接收数据
inputstreamreader isr=null;
bufferedreader br=null;
try {
isr = new inputstreamreader(server.getinputstream());//这里需要gbk
br = new bufferedreader(isr);
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
return br;
}
///
bufferedreader br=recieve();
while((message = br.readline()) != null){
messages+=message;
}
system.out.println("messages="+messages);
你会发现这样写会是乱码;
应该写成isr = new inputstreamreader(server.getinputstream(),"gbk");
这样就正常显示了。
在服务端默认的编码情况下,java的socket接收需要gbk编码,而c#的接收需要utf-8编码