TCP通信-客户端给服务端发送数据 -聊天室版本4

TCP通信-客户端给服务端发送数据 -聊天室版本4

案例:

  实现Client重复向Server发送控制台输入的数据,当Server接受全部数据结束后给出响应信息(全部接受完毕!您已下线)
  分析:方便代码优化,可以提取公共的内容,作为父类,将发送数据和接送数据功能细分化,这样看的更加纯粹明了
  1.定义一个发送数据方法sendMessage(String message)有参数的,receiveMessage()方法有返回值,因为接受到的数据需要
   2.定义方法时注意返回值的判断,是否传参的问题。
   3.构造方法把socket传递过去两个处理流。

代码实现:

ChatProtocol类
public class ChatProtocol {
   private Socket socket;
   private DataOutputStream dos;
   private DataInputStream dis;

   public ChatProtocol(Socket socket) throws IOException {
      this.socket = socket;
      dos = new DataOutputStream(socket.getOutputStream());
      dis = new DataInputStream(socket.getInputStream());
   }

   public ChatProtocol() {
      super();
   }



   //发送数据
   public void sendMessage(String message) throws IOException{
      //发送数据类型
      dos.writeByte(1);
      //发送数据长度
      byte[] bys = message.getBytes();
      dos.writeInt(bys.length);
      //发送数据内容
      dos.write(bys);
      dos.flush();
   }
   //接受数据
   public String receiveMessage() throws IOException{
      //接收数据类型
      byte type = dis.readByte();
      //接收数据长度
      int len = dis.readInt();
      byte[] bys = new byte[len];
      //接收数据内容
      dis.read(bys);
      return new String(bys);
   }


}
Client类
public class Client extends ChatProtocol{


   public Client() {
      super();
      // TODO Auto-generated constructor stub
   }

   public Client(Socket socket) throws IOException {
      super(socket);
   }

   public static void main(String[] args) throws UnknownHostException, IOException {
      //创建Socket对象
      Socket socket = new Socket("localhost", 10087);
      //获取流对象并发送数据
      Scanner sc = new Scanner(System.in);
      Client client = new Client(socket);
      while(true){
          String data = sc.nextLine();
          client.sendMessage(data);
          //判断
          if("886".equals(data)){
             break;
          }
      }

      String message = client.receiveMessage();
      System.out.println(message);

      sc.close();
      socket.close();
   }

}
Server类
public class Server extends ChatProtocol {
   public Server() {
      super();
      // TODO Auto-generated constructor stub
   }

   public Server(Socket socket) throws IOException {
      super(socket);
   }

   public static void main(String[] args) throws IOException {
      //创建ServerSocket
      ServerSocket ss = new ServerSocket(10087);
      //建立连接
      Socket socket = ss.accept();
      Server server = new Server(socket);
      while(true){
          //接收数据
          String message = server.receiveMessage();
          System.out.println(message);
          //886意味着Client要下线
          if("886".equals(message)){
             System.out.println("此客户端下线!");
             break;
          }
      }
      //给出响应数据
      server.sendMessage("接收完毕!您已下线!");
   }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值