Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1669111
  • 博文数量: 1493
  • 博客积分: 38
  • 博客等级: 民兵
  • 技术积分: 5834
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-19 17:28
文章分类

全部博文(1493)

文章存档

2016年(11)

2015年(38)

2014年(137)

2013年(253)

2012年(1054)

2011年(1)

分类:

2012-07-30 08:41:52

原文地址:tcp编程 作者:zsz198908

客户端编程:




点击(此处)折叠或打开

  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.io.OutputStream;
  4. import java.net.Socket;
  5. import java.util.Scanner;

  6. public class ClientDemo {
  7.   public static void main(String[] args)
  8.     throws IOException {
  9.     ClientDemo client = new ClientDemo();
  10.     client.open();
  11.   }
  12.   private void open() throws IOException {
  13.     //new Socket(host, port) 构造器会发起对host主机上
  14.     // port端口 的连接, 如果连接成功就会创建Socket实例s
  15.     // 如连接失败, 会抛出IOException实例.
  16.     // 服务器必须先启动, 等待连接!
  17.     Socket s = new Socket("localhost", 8000);
  18.     //in 代表服务端到客户端的流
  19.     InputStream in = s.getInputStream();
  20.     //out 代表客户端到服务器的流
  21.     OutputStream out = s.getOutputStream();
  22.     new Reader(out, s).start();
  23.     new Writer(in, s). start();
  24.   }
  25.   //负责读取控制台信息, 发送到服务器端.
  26.   class Reader extends Thread{
  27.     OutputStream out;
  28.     Socket s;
  29.     public Reader(OutputStream out, Socket s) {
  30.       this.out = out;
  31.       this.s = s;
  32.     }
  33. // 负责读取控制台信息, 发送到服务器端.
  34.     public void run() {
  35.       Scanner in = new Scanner(System.in);
  36.       try{
  37.         while(true){
  38.           String str = in.nextLine();//粗面\n
  39.           //System.out.println(str);//粗面
  40.           out.write((str+"\n").getBytes());//一定发送 \n
  41.           out.flush();
  42.           if(str.equalsIgnoreCase("Bye")){
  43.             this.s.close();
  44.             break;
  45.           }
  46.         }
  47.       }catch(Exception e){
  48.         e.printStackTrace();
  49.       }
  50.     }
  51.   }
  52.   //负责将从服务器获取的信息显示到控制台, 后台线程, 自动结束
  53.   class Writer extends Thread{
  54.     InputStream in;
  55.     Socket s;
  56.     public Writer(InputStream in, Socket s) {
  57.       this.in = in;
  58.       this.s = s;
  59.       setDaemon(true);
  60.     }
  61. // 负责将从服务器获取的信息显示到控制台, 后台线程, 自动结束
  62.     public void run() {
  63.       Scanner in = new Scanner(this.in);
  64.       while(true){
  65.         String str = in.nextLine();//没有.\n
  66.         //System.out.println(str);//没有.
  67.         System.out.println(str);
  68.       }
  69.     }
  70.   }
  71. }

服务器端编程:

点击(此处)折叠或打开

  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.io.OutputStream;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6. import java.util.Scanner;

  7. public class ServerDemo {
  8.   public static void main(String[] args)
  9.     throws IOException {
  10.     ServerDemo server = new ServerDemo();
  11.     server.start();
  12.   }
  13.   public void start() throws IOException {
  14.     ServerSocket ss = new ServerSocket(8000);
  15.     while(true){
  16.     //ss.accept() 引起当前线程(main)IO Block, 等到客户端
  17.     //的连接, 如果客户端连接成功就返回Socket实例 s
  18.     //s代表客户端的连接, s中包含流属性: in和out,
  19.     //in代表客户端发送来的信息, out代表向客户端输出的信息
  20.     //开启客户端服务线程实例为客户服务, 主线程等待下个客户连接
  21.       Socket s = ss.accept();
  22.       new Service(s).start();
  23.     }
  24.   }
  25.   class Service extends Thread{
  26.     Socket s;
  27.     public Service(Socket s) {
  28.       this.s = s;
  29.     }
  30.     public void run() {
  31.       try{
  32.         InputStream in = s.getInputStream();
  33.         OutputStream out = s.getOutputStream();
  34.         out.write("欢迎光顾!你点些啥?\n".getBytes());
  35.         out.flush();
  36.         Scanner sc = new Scanner(in);
  37.         while(true){
  38.           String str = sc.nextLine();//粗面\n
  39.           //System.out.println(str);//粗面
  40.           if(str.equals("粗面")){
  41.             out.write("没有.\n".getBytes());
  42.             out.flush();
  43.           } else if(str.equals("包子")){
  44.             out.write("有, 给你!\n".getBytes());
  45.             out.flush();
  46.           } else if(str.equalsIgnoreCase("bye")){
  47.             out.write("Bye!\n".getBytes());
  48.             out.flush();
  49.             this.s.close();
  50.             break;
  51.           }
  52.         }
  53.       }catch(IOException e){
  54.         e.printStackTrace();
  55.       }
  56.     }
  57.   }
  58. }



阅读(385) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~