Socket开发Server端接收到客户端的 中文数据(字符串) 为乱码问题?

netskychw 2004-11-05 04:59:16

数据是从文件中读出来,然后放在一个String 变量中的,在客户端中文可以正常显示,但发到服务器时,读出再显示就变成乱码;我在Server用编码转换处理
(Sting line = new String(line.getBytes("ISO-8859-1"),"GBK")
仍不能正常显示。有知道socket发送字符数据是以什么格式发送的吗?

具体代码如下:
//Server.java

import java.io.*;
import java.net.*;
public class Server {

public final static int PORT = 5000;

public static void main(String[] args) {
ServerSocket checkServer = null;
String line;
BufferedReader is = null;
DataOutputStream os = null;

Socket clientSocket = null;

try{
checkServer = new ServerSocket(PORT);
System.out.println("Server started...");
}catch(IOException e){
System.out.println(e);
}

try{
clientSocket = checkServer.accept();
System.out.println("connection established...");
is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
os = new DataOutputStream(clientSocket.getOutputStream());
}catch(Exception ei){
ei.printStackTrace();
}

try{
line = is.readLine();//circle cause to pause here!

System.out.println(line);

int state = 0;
if(line!=null&&state==0){
os.writeBytes("short send successfully!");
}
}catch(IOException e){

System.out.println(e);
}

try{
os.close();
is.close();
clientSocket.close();
}catch(IOException e){
e.printStackTrace();
}

}
}

//Client.java

import java.net.*;
import java.io.*;
public class Client {
public final static int REPORT_PORT = 5000;

public static void main(String[] args) throws Exception {
Socket sock1 = null,sock2 = null;
BufferedReader is = null;
DataOutputStream os = null;

BufferedReader stdin= new BufferedReader(new InputStreamReader(System.in));
String userInput = null;
String output = null;
//get message
String outPutData = null;
try{
is = new BufferedReader(new FileReader("originData.txt"));
StringBuffer strBuffer = new StringBuffer();
String line;
while((line=is.readLine())!=null){
strBuffer.append(line);
}
outPutData = strBuffer.toString();
System.out.println("[client out]"+outPutData);
}catch(IOException ioe){
System.out.println("IO Error:"+ ioe.getMessage() );
}
try{
sock1 = new Socket("localhost",REPORT_PORT);
is = new BufferedReader(new InputStreamReader(sock1.getInputStream()));
os = new DataOutputStream(sock1.getOutputStream());
}catch(UnknownHostException el){
System.out.println("Unknown Host:"+el);

}catch(IOException el2){
System.out.println("Error IO:"+el2);
}

try{
System.out.println("Please input a keyword:");
//userInput = stdin.readLine();
//userInput = "helloWorld!";
os.writeBytes(outPutData+"\n");
}catch(IOException ex){
System.out.println("error writing to server:"+ex);
}

try{
output = is.readLine();
System.out.println("Got from server:"+output);
}catch(IOException eio){
eio.printStackTrace();
}

//close
try{
is.close();
os.close();
sock1.close();
}catch(IOException e){
System.out.println("Error in closing..."+e);
}
}
}
...全文
608 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
intelserver 2004-11-08
  • 打赏
  • 举报
回复
写错了,:)


os = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
intelserver 2004-11-08
  • 打赏
  • 举报
回复
将输出流DataOutputStream(字节流) os 改为BufferedWriter(字符流)

public class Client
{
BufferedWriter os = null;
..............

os = new DataOutputStream(new OutputStreamWriter(clientSocket.getOutputStream()));
............
os.write(outPutData+"\n");
.............
}
这样应该不是乱码了



mxlmwl 2004-11-08
  • 打赏
  • 举报
回复
去网上搜索URLEncoder,来下载到这个文件就成了。
mxlmwl 2004-11-08
  • 打赏
  • 举报
回复
String b="你好";
String a=URLEncoder.encode(b,"UTF-8");
netskychw 2004-11-08
  • 打赏
  • 举报
回复
alaal(~:寒~:冬:~)
能 给个小例子吗 ?谢了
alaal 2004-11-05
  • 打赏
  • 举报
回复
URLEncoder.encode("aaa", "UTF-8")

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧