Socket网络编程学习笔记 TCP 客户端与服务端实现

Posted 鮀城小帅

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Socket网络编程学习笔记 TCP 客户端与服务端实现相关的知识,希望对你有一定的参考价值。

客户端实现:

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

/**
 * @ClassName Client
 * @Description TODO
 * @Author wushaopei
 * @Date 2022/2/9 23:34
 * @Version 1.0
 */
public class Client 

    public static void main(String[] args) throws IOException, UnknownHostException 
        Socket socket = new Socket();
        //超时时间
        socket.setSoTimeout(3000);

        //连接本地,端口2000;超时时间3000ms
        socket.connect(new InetSocketAddress(Inet4Address.getLocalHost(),2000),3000);

        System.out.println("已发起服务器连接,并进入后续流程~");
        System.out.println("客户端信息:" + socket.getLocalAddress() + " P:"+socket.getLocalPort());
        System.out.println("服务端信息:" + socket.getInetAddress() + " P:"+socket.getPort());

        try 
            todo(socket);
        catch (Exception e)
            System.out.println("异常关闭");
        
        //释放资源
        socket.close();
        System.out.println("客户端退出");
    

    private static void todo(Socket client) throws IOException 
        // 构建键盘输入流
        InputStream in = System.in;
        BufferedReader input = new BufferedReader(new InputStreamReader(in));

        // 得到Socket输出流,并转换为打印流
        OutputStream outputStream = client.getOutputStream();
        PrintStream socketPrintStream = new PrintStream(outputStream);

        // 得到Socket输入流
        InputStream inputStream = client.getInputStream();
        BufferedReader socketBufferedReader = new BufferedReader(new InputStreamReader(inputStream));

        boolean flag = true;
        do 
            // 键盘读取一行
            String str = input.readLine();
            // 发送到服务器
            socketPrintStream.println(str);

            // 从服务器读取一行
            String echo = socketBufferedReader.readLine();
            if("bye".equalsIgnoreCase(echo))
                flag = false;
            else
                System.out.println(echo);
            
        while (flag);

        //  资源释放
        socketPrintStream.close();
        socketBufferedReader.close();

    

服务端实现:

import javax.sound.midi.Soundbank;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @ClassName Server
 * @Description TODO
 * @Author wushaopei
 * @Date 2022/2/9 23:33
 * @Version 1.0
 */
public class Server 
    public static void main(String[] args) throws IOException 
        ServerSocket serverSocket = new ServerSocket(2000);

        System.out.println("服务器准备就绪");
        System.out.println("服务器信息: " + serverSocket.getInetAddress() + " P: " + serverSocket.getLocalPort());

        //等待客户端连接
        for(;;)
            //得到客户端
            Socket socket = serverSocket.accept();
            // 客户端构建异步线程
            ClientHandler clientHandler = new ClientHandler(socket);
            // 启动线程
            clientHandler.start();
        
    

    /**
     * 客户端消息处理
     */
    private static class ClientHandler extends Thread 
        private Socket socket;
        private boolean flag = true;

        ClientHandler(Socket socket)
            this.socket = socket;
        

        @Override
        public void run()
            super.run();
            System.out.println("新客户端连接:" + socket.getInetAddress() + " P:"+ socket.getPort());
            try 
                // 得到打印流,用于数据输出;服务器回送数据使用
                PrintStream socketOutput = new PrintStream(socket.getOutputStream());
                // 得到输入流,用于接收数据
                BufferedReader socketInput = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                do 
                    // 客户端拿到一条数据
                    String str = socketInput.readLine();
                    if("bye".equalsIgnoreCase(str))
                        flag = false;
                        // 回送
                        socketOutput.println("bye");
                    else 
                        // 打印到屏幕。并回送数据长度
                        System.out.println(str);
                        socketOutput.println("回送:" + str.length());
                    
                while (flag);
            catch (Exception e)
                System.out.println("连接异常断开");
            finally 
                // 连接关闭
                try 
                    socket.close();
                catch (IOException e)
                    e.printStackTrace();
                
            
            System.out.println("客户端已退出:" + socket.getInetAddress() + " P:"+ socket.getPort() );
        
    

测试TCP通信

 

 

以上是关于Socket网络编程学习笔记 TCP 客户端与服务端实现的主要内容,如果未能解决你的问题,请参考以下文章

Python学习笔记__16.2章 TCP编程

计算机网络学习笔记7-TCP编程流程及面试题

初识Socket通信:基于TCP和UDP协议学习网络编程

计算机网络学习笔记9-TCP并发服务器框架

12.5-全栈Java笔记:Java网络编程

Socket网络编程学习笔记 网络编程Socket与TCP/UDP