一个小demo:Socket 短连接

服务端

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

    /**
     * 服务端端口
     */
    private static final int PORT = 12345;

    /**
     * 数据流编码
     */
    private static final String CHARSET_NAME = "UTF-8";

    public static void main(String[] args) {
        new Server().start();
    }

    /**
     * 功能描述:
     * <服务端启动>
     *
     * @return void
     * @author zhoulipu
     * @date 2019/8/8 17:07
     */
    private void start() {
        try {
            // 服务端开启
            ServerSocket server = new ServerSocket(PORT);
            System.out.println("服务端开启,等待客户端连接中...");
            // 循环监听客户端连接
            while (true) {
                // 等待客户端进行连接
                Socket socket = server.accept();
                // 开启新线程处理消息
                new TaskHandler (socket).start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 消息处理
     */
    class TaskHandler extends Thread {

        private Socket socket;

        private TaskHandler (Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            InputStream in = null;
            OutputStream out = null;
            try {
                // 从socket中获取输入流
                in = socket.getInputStream();
                // 建立缓冲区读取数据
                byte[] bytes = new byte[1024];
                int len;
                StringBuilder msg = new StringBuilder();
                while ((len = in.read(bytes)) != -1) {
                    msg.append(new String(bytes, 0, len, CHARSET_NAME));
                }
                System.out.println("[客户端]:" + msg);
                // 从socket中获取输出流
                out = socket.getOutputStream();
                out.write(("我已接收信息\"" + msg + "\"").getBytes(CHARSET_NAME));
                // 关闭输入输出流和连接
                in.close();
                out.close();
                socket.close();
            } catch (IOException e) {
                e.getStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

}

客户端

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class Client {

    /**
     * 服务端地址
     */
    private static final String HOST = "127.0.0.1";

    /**
     * 服务端端口
     */
    private static final int PORT = 12345;

    /**
     * 数据流编码
     */
    private static final String CHARSET_NAME = "UTF-8";

    public static void main(String args[]) {
        new Client().start();
    }

    /**
     * 功能描述:
     * <客户端启动>
     *
     * @return void
     * @author zhoulipu
     * @date 2019/8/8 17:07
     */
    private void start() {
        Socket socket = null;
        InputStream in = null;
        OutputStream out = null;
        try {
            // 客户端开启,建立连接
            socket = new Socket(HOST, PORT);
            System.out.println("连接服务器成功");
            // 从socket中获取输出流
            out = socket.getOutputStream();
            System.out.print("[客户端]:");
            socket.getOutputStream().write(new Scanner(System.in).nextLine().getBytes(CHARSET_NAME));
            // 发送数据流
            socket.shutdownOutput();
            // 从socket中获取输入流
            in = socket.getInputStream();
            byte[] bytes = new byte[1024];
            int len;
            StringBuilder msg = new StringBuilder();
            while ((len = in.read(bytes)) != -1) {
                msg.append(new String(bytes, 0, len, CHARSET_NAME));
            }
            System.out.println("[服务端]:" + msg);
            // 关闭输入输出流和连接
            in.close();
            out.close();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
}
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值