zoukankan      html  css  js  c++  java
  • socket编程代码示范 简单飞扬

    //转自网络http://www.zaoxue.com/article/tech-71025.htm

    //实现Client端功能的ClientApp.java原文件:


    import java.io.*;
    import java.net.Socket;

    public class ClientApp {
        public static void main(String args[]) {
            try {
                // 创建通讯并且和主机Rock连接
                Socket socket = new Socket("10.5.7.1", 8018);
                // 打开这个Socket的输入/输出流
                OutputStream os = socket.getOutputStream();
                DataInputStream is = new DataInputStream(socket.getInputStream());

                int c;
                boolean flag = true;

                String responseline;

                while (flag) {
                    // 从标准输入输出接受字符并且写入系统
                    while ((c = System.in.read()) != -1) {
                        os.write((byte) c);
                        if (c == '"n') {
                            os.flush();
                            // 将程序阻塞,直到回答信息被收到后将他们在标准输出上显示出来
                            responseline = is.readLine();
                            System.out.println("Message is:" + responseline);
                        }
                    }
                }
                
                //关闭所有流    
                os.close();
                is.close();
                socket.close();

            } catch (Exception e) {
                System.out.println("Exception :" + e.getMessage());
            }
        }
    }


    //实现Server端功能的ServerApp.java原文件:

    import java.net.*;
    import java.io.*;

    public class ServerApp {
        public static void main(String args[]) {
            try {
                boolean flag = true;
                Socket clientSocket = null;
                String inputLine;
                int c;

                ServerSocket socket = new ServerSocket(8018);
                System.out.println("Server listen on:" + socket.getLocalPort());

                while (flag) {
                    clientSocket = socket.accept();
                    DataInputStream is = new DataInputStream(
                            new BufferedInputStream(clientSocket.getInputStream()));
                    OutputStream os = clientSocket.getOutputStream();

                    while ((inputLine = is.readLine()) != null) {
                        // 当客户端输入stop的时候服务器程序运行终止!
                        if (inputLine.equals("stop")) {
                            flag = false;
                            break;
                        } else {
                            System.out.println(inputLine);

                            while ((c = System.in.read()) != -1) {
                                os.write((byte) c);
                                if (c == '"n') {
                                    os.flush(); // 将信息发送到客户端
                                    break;
                                }
                            }
                        }
                    }
                    is.close();
                    os.close();
                    clientSocket.close();

                }
                socket.close();
            } catch (Exception e) {
                System.out.println("Exception :" + e.getMessage());
            }
        }
    }



  • 相关阅读:
    HBase Cassandra比较
    重新认识HBase,Cassandra列存储——本质是还是行存储,只是可以动态改变列(每行对应的数据字段)数量而已,当心不是parquet
    HBase底层存储原理——我靠,和cassandra本质上没有区别啊!都是kv 列存储,只是一个是p2p另一个是集中式而已!
    Cassandra 数据模型设计,根据你的查询来制定设计——反范式设计本质:空间换时间
    【LeetCode】【Python解决问题的方法】Best Time to Buy and Sell Stock II
    LVM逻辑卷管理命令
    Java引进和应用的包装类
    Android 4.0新组件:GridLayout详细说明
    【剑指offer】打印单列表从尾部到头部
    原因以及如何避免产生僵尸进程
  • 原文地址:https://www.cnblogs.com/jiandanfy/p/1060931.html
Copyright © 2011-2022 走看看