栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA

1、介绍

Socket的本质是编程接口,是对TCP/IP协议的具体实现,各种编程语言均有自己的实现,可以直接给程序员调用使用。本质是建立端口与端口间的网络通信。

在java的实际开发中,很少会自己直接使用Socket编程,一般借助第三方写好的工具类或框架。在阅读第三方框架工具的源码中,可能会存在Socket的操作。

2、流程图解

3、代码演示 服务端
public class DemoServer {
    public static void main(String[] args) throws IOException {
        //创建服务端socket,监听tcp 8080端口。
        ServerSocket serverSocket = new ServerSocket(8080);
        while (true){
            //阻塞等待tcp客户端连接,连接好后会创建返回一个Socket对象。
            Socket clientSocket = serverSocket.accept();
            System.out.println(clientSocket.getInetAddress()+" "+clientSocket.getPort());

            handleSocket(clientSocket);
        }
    }

    //新建线程处理连接的Socket客户端发送的消息
    private static void handleSocket(Socket socket){
        new Thread(()->{
            byte[] bufferBytes = new byte[1024];
            InputStream in = null;
            OutputStream out = null;
            try {
                in = socket.getInputStream();
                out = socket.getOutputStream();
                while (true){
                    //阻塞读取tcp客户端发送的消息
                    int readCount = in.read(bufferBytes);
                    if(readCount!=-1){
                        String str = new String(bufferBytes, 0, readCount);
                        System.out.println("服务端接收到的信息:"+str);

                        //回送消息给客户端
                        out.write(bufferBytes,0,readCount);
                        out.flush();
                    }else {
                        System.out.println("socket客户端异常断开");
                        break;
                    }
                }
            }catch (IOException e){
                e.printStackTrace();
            }finally {
                closeIO(out);
                closeIO(in);
                closeIO(socket);
            }
        }).start();
    }
    
	//关闭流方法
    private static void closeIO(Closeable io){
        if(io!=null){
            try{
                io.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}
客户端
public class DemoClient {

    public static void main(String[] args) throws IOException {
        //创建Socket
        Socket socket = new Socket();
        //Socket连接指定ip、port
        socket.connect(new InetSocketAddress("127.0.0.1", 8080));
        InputStream in = socket.getInputStream();
        OutputStream out = socket.getOutputStream();

        handleMsg(in);

        Scanner scanner = new Scanner(System.in);
        boolean flag = true;
        while (flag){
            String str = scanner.next();
            if("exit".equals(str)){
                flag = false;
            }
            out.write(str.getBytes());
            out.flush();
        }
        closeIO(out);
        closeIO(in);
        closeIO(socket);
    }

    //新建线程处理服务端Socket发送的消息
    public static void handleMsg(InputStream in){
        new Thread(()->{
            try {
                byte[] bufferBytes = new byte[1024];
                while (true){
                    int read = in.read(bufferBytes);
                    if(read!=-1){
                        String str = new String(bufferBytes, 0, read);
                        System.out.println("客户端接收到消息:"+str);
                    }else {
                        break;
                    }
                }

            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                closeIO(in);
            }
        }).start();
    }

    //关闭流方法
    private static void closeIO(Closeable io){
        if(io!=null){
            try{
                io.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/826783.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号