Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区 » 实战错误讨论  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:服务与客户之间通信的问题?(传递中文,出现乱码) [Re:nextsun]
lisliefor





发贴: 287
积分: 7
于 2006-06-01 11:11 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
把你的程序小改动了一下!
传递中文,外面套一个过滤流嘛!中途转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);
    }
  }
}




话题树型展开
人气 标题 作者 字数 发贴时间
7639 服务与客户之间通信的问题?(传递中文,出现乱码) nextsun 2713 2006-05-31 15:10
5990 Re:服务与客户之间通信的问题? nextsun 28 2006-05-31 16:45
5962 Re:服务与客户之间通信的问题?(传递中文,出现乱码) lisliefor 2259 2006-06-01 11:11
6009 Re:服务与客户之间通信的问题?(传递中文,出现乱码) lisliefor 147 2006-06-01 11:12
5974 Re:服务与客户之间通信的问题?(传递中文,出现乱码) nextsun 29 2006-06-01 12:55
6550 Re:服务与客户之间通信的问题?(传递中文,出现乱码) nextsun 84 2006-06-01 12:57

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   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