Topic: 服务与客户之间通信的问题?(传递中文,出现乱码) |
Print this page |
1.服务与客户之间通信的问题?(传递中文,出现乱码) | Copy to clipboard |
Posted by: nextsun Posted on: 2006-05-31 15:10 就是不能够用传递中文,出现乱码"?????",求大家帮帮我!!! //服务器程序 import java.io.*; import java.net.*; public class tcpserver { public static void main(String[] args) throws IOException { ServerSocket svrsoc=null; Socket soc=null; DataInputStream in=null; PrintStream out=null; InetAddress clientIP=null; String str=null; try { svrsoc=new ServerSocket(5555); System.out.println("Server start...."); soc=svrsoc.accept(); in=new DataInputStream(soc.getInputStream()); out=new PrintStream(soc.getOutputStream()); clientIP=soc.getInetAddress(); System.out.println("Client's IP address:"+clientIP); System.out.println("Port:"+soc.getLocalPort()); System.out.println("Client's Name:"+soc.getInetAddress().getHostName()); out.println("welcome....."); in.readLine(); while (!str.equals("quit")) { System.out.println("Client said:"+str); str=in.readLine(); } System.out.println("Client want to leave"); } catch(Exception e) { System.out.println("error:"+e); } finally { in.close(); out.close(); soc.close(); svrsoc.close(); System.exit(0); } } } //客户机程序 import java.io.*; import java.net.*; public class tcpclient { public static void main(String[] args) throws IOException { Socket soc=null; DataInputStream in=null; PrintStream out=null; DataInputStream sysin=null; String strin=null; String strout=null; try { soc=new Socket("127.0.0.1",5555); System.out.println("Connecting to the Server"); in=new DataInputStream(soc.getInputStream()); out=new PrintStream(soc.getOutputStream()); strin=in.readLine(); System.out.println("Server said:"+strin); sysin=new DataInputStream(System.in); strout=sysin.readLine(); while (!strout.equals("quit")) { out.println(strout); strout=sysin.readLine(); } out.println(strout); } catch(Exception e) { System.out.println("error:"+e); } finally { in.close(); out.close(); sysin.close(); soc.close(); System.exit(0); } } } |
2.Re:服务与客户之间通信的问题? [Re: nextsun] | Copy to clipboard |
Posted by: nextsun Posted on: 2006-05-31 16:45 这个问题困扰了我好久了,真的希望能在这里得到解答!!!! |
3.Re:服务与客户之间通信的问题?(传递中文,出现乱码) [Re: nextsun] | Copy to clipboard |
Posted by: lisliefor Posted on: 2006-06-01 11:11 把你的程序小改动了一下! 传递中文,外面套一个过滤流嘛!中途转ISO8859-1,UTF-8,UTF-16编码。 Tcpserver.java package help; import java.io.*; import java.net.*; public class Tcpserver { public static void main(String[] args) throws IOException { ServerSocket svrsoc = null; Socket soc = null; BufferedReader in = null; PrintStream out = null; boolean Flag = true; try { svrsoc = new ServerSocket(5554); System.out.println("Server start...."); soc = svrsoc.accept(); in = new BufferedReader(new InputStreamReader(soc.getInputStream())); out = new PrintStream(soc.getOutputStream()); while(Flag){ String s = in.readLine(); System.out.println("Server gets Message:"+s); if(s.equals("1")){ out.println("我"); out.flush(); } else if(s.equals("2")){ out.println("是"); out.flush(); } else if(s.equals("3")){ out.println("猪猪"); out.flush(); } else if(s.equals("exit")) Flag = false; } } catch (Exception e) { System.out.println("error:" + e); } finally { in.close(); out.close(); soc.close(); svrsoc.close(); System.exit(0); } } } Tcpclient.java package help; import java.io.*; import java.net.*; public class Tcpclient { public static void main(String[] args) throws IOException { Socket soc = null; BufferedReader in = null; PrintStream out = null; BufferedReader sysin = null; String strout = null; boolean Flag = true; try { soc = new Socket("127.0.0.1", 5554); System.out.println("Connecting to the Server"); in = new BufferedReader(new InputStreamReader(soc.getInputStream())); out = new PrintStream(soc.getOutputStream()); while(Flag){ sysin = new BufferedReader(new InputStreamReader(System.in)); strout = sysin.readLine(); out.println(strout); out.flush(); System.out.println("Client send:"+strout); if(strout.equals("exit")) Flag = false; String s = in.readLine(); System.out.println("Server said:"+s); } } catch (Exception e) { System.out.println("error:" + e); } finally { in.close(); out.close(); sysin.close(); soc.close(); System.exit(0); } } } |
4.Re:服务与客户之间通信的问题?(传递中文,出现乱码) [Re: nextsun] | Copy to clipboard |
Posted by: lisliefor Posted on: 2006-06-01 11:12 结果也粘贴一下: Connecting to the Server 1 //后台输入 Client send:1 Server said:我 2 Client send:2 Server said:是 3 Client send:3 Server said:猪猪 |
5.Re:服务与客户之间通信的问题?(传递中文,出现乱码) [Re: nextsun] | Copy to clipboard |
Posted by: nextsun Posted on: 2006-06-01 12:55 高手就是不一样,一看就知道,真的是帮了我大忙了,谢谢!!! |
6.Re:服务与客户之间通信的问题?(传递中文,出现乱码) [Re: nextsun] | Copy to clipboard |
Posted by: nextsun Posted on: 2006-06-01 12:57 弱弱的问一下 BufferedReader(new InputStreamReader(soc.getInputStream())); 这个过程到底是怎么编码的呢? |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |