Socket编程 JAVA 单工

来源:互联网 发布:javascript alert 编辑:程序博客网 时间:2024/04/30 03:22


Socket编程 JAVA  单工 

客户端:

/** * 版权所有 (c) 2016,xiaoming有限公司   */package com.xiaoming.test;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;/** * 类说明 *  * <pre> * Modify Information: * Author        Date          Description * ============ =========== ============================ * DELL          2017年1月5日    Create this file * </pre> *  */public class Client {    /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));        String command;        try {            while ((command = br.readLine()) != null) {                try {                    if (command.length() >= 1) {                        new CommandSender("127.0.0.1", 10086).send(command);                    } else {                        System.out.println();                    }                } catch (Exception e) {                    System.out.println(" JobGroupRunner Exception");                }            }        } catch (IOException e) {            e.printStackTrace();        }    }}


发送命令方法:

package com.xiaoming.test;import java.io.OutputStream;import java.net.InetSocketAddress;import java.net.Socket;/** * 类说明 *  * <pre> * Modify Information: * Author       Date        Description * ============ =========== ============================ * xiaoming     2016-9-02   Create this file * </pre> *  */public class CommandSender {    private String host;    private int port;    public CommandSender(String host, int port) {        this.host = host;        this.port = port;    }    public void send(String command) throws Exception {        Socket socket = new Socket();        try {            socket.connect(new InetSocketAddress(host, port), 10000);            socket.setSoTimeout(10000);            OutputStream outputStream = socket.getOutputStream();             outputStream.write(command.getBytes("UTF-8"));             outputStream.flush();            //IoUtil.sendLengthValue(outputStream, command.getBytes(StringUtil.DEFAULT_CHARSET));            socket.shutdownOutput();            outputStream.close();        } catch (Exception e) {            throw e;        } finally {            socket.close();        }    }}


服务端:接收客户端消息:

/** * 版权所有 (c) 2016,xiaoming有限公司   */package com.xiaoming.test;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.ServerSocket;import java.net.Socket;/** * 类说明: *  * <pre> * Modify Information: * Author        Date          Description * ============ =========== ============================ * xiaoming      2016-12-21    Create this file * </pre> *  */public class Server {    public static void main(String[] args) {        try {            // 1、创建一个服务器端Socket,即ServerSocket,指定绑定的端口,并监听此端口            ServerSocket serverSocket = new ServerSocket(10086);// 1024-65535的某个端口            while (true) {                // 2、调用accept()方法开始监听,等待客户端的连接                Socket socket = serverSocket.accept();                // 3、获取输入流,并读取客户端信息                InputStream is = socket.getInputStream();                                InputStreamReader isr = new InputStreamReader(is);                BufferedReader br = new BufferedReader(isr);                String info = null;                String command ="";                while ((info = br.readLine()) != null ) {                    command = info;                    System.out.println("Hello,我是服务器,客户端说:" + info);                }                socket.shutdownInput();// 关闭输入流/*                // 4、获取输出流,响应客户端的请求                OutputStream os = socket.getOutputStream();                PrintWriter pw = new PrintWriter(os);                pw.write("Hello World!");                pw.flush();                // 5、关闭资源                pw.close();                os.close();*/                String stop = "STOP";                if (stop.equalsIgnoreCase(command)) {                    System.out.println("接收到关闭Socket指令");                    br.close();                    isr.close();                    is.close();                    socket.close();                    try {                        serverSocket.close();                        System.out.println( " Server shutdown successfully.");                    } catch (Exception e) {                        System.out.println(e);                    }                    System.exit(0);                    break;                }            }                   } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}







0 0
原创粉丝点击