码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA笔记15__TCP服务端、客户端程序 / ECHO程序 /

时间:2014-12-22 07:04:29      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:

/**
 * TCP:传输控制协议,采用三方握手的方式,保证准确的连接操作。
 * UDP:数据报协议,发送数据报,例如:手机短信或者是QQ消息。
 */

/**
 * TCP服务器端程序
 */
public class Main {
    public static void main(String[] args) {
       try {
            ServerSocket s1 = new ServerSocket(57712); //创建一个服务器端socket
            System.out.println("服务器已启动,正在等待连接中....");
            Socket s = s1.accept(); //等待客户端连接,会阻塞
            String info = "Hello World ---\n";
            //构造一个字节输出流
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            bw.write(info);
            bw.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
/**
 * TCP客户端程序
 */
public class ClientDemo {
    public static void main(String[] args){
        try {
            System.out.println("good");
            //创建一个客户端的socket对象(IP/主机名,端口号)
            Socket s1 = new Socket("172.31.150.27",57712);
            System.out.println("与服务器连接成功");
            //构造一个字符输入流
            BufferedReader br = new BufferedReader(new InputStreamReader(s1.getInputStream()));
            String info = br.readLine();
            System.out.println(info);
            br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

 

/**
 * ECHO服务器端程序
 */
public class Main {
    public static void main(String[] args) throws IOException {
       ServerSocket s = new ServerSocket(12347);
       System.out.println("Server has started. is Waiting....");
       boolean flag = true;
       while(flag){
           System.out.println("is waiting for connection....");
           Socket s0 = s.accept();
           System.out.println("客户端IP:"+s0.getInetAddress().getHostAddress()+" 已连接!");
           //获取输入输出流
           BufferedReader br = new BufferedReader(
                   new InputStreamReader(s0.getInputStream()));
           PrintStream ps = new PrintStream(
                   new BufferedOutputStream(s0.getOutputStream()));
           boolean bool = true; //循环接收数据
           while(bool){
               String info = br.readLine();
               if(info==null || "bye".equals(info)){
                   bool = false;
               }else{
                   System.out.println(info);
                   ps.println("echo:"+info);
                   ps.flush();
               }
           }
           ps.close();
           br.close();
       }
    }
}
/**
 * ECHO客户端程序
 */
public class ClientDemo {
    public static void main(String[] args) throws IOException{
        //创建一个客户端的socket对象(IP/主机名,端口号)
        Socket s0 = new Socket("172.31.150.27",12347);
        System.out.println("与服务器连接成功");
        Scanner input = new Scanner(System.in);
        boolean flag = true;
        PrintStream ps = new PrintStream(
                   new BufferedOutputStream(s0.getOutputStream()));
        BufferedReader br = new BufferedReader(
                   new InputStreamReader(s0.getInputStream()));
        while(flag){
            System.out.println("请输入:");
            String info = input.next();
            if("bye".equals(info)){
                flag = false;
            }else{
                ps.println(info);
                ps.flush();
                System.out.println(br.readLine());
            }
        }
        br.close();
        ps.close();
    }
}

 

JAVA笔记15__TCP服务端、客户端程序 / ECHO程序 /

标签:

原文地址:http://www.cnblogs.com/fish7/p/4177412.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!