练习——简单的TcpCS了解基本概念

发布时间 2023-05-04 19:07:19作者: Q1uuuu
package com.net;

import java.io.IOException;
import java.io.OutputStream;
import java.net.*;

//客户端
@SuppressWarnings({"all"})
public class TCPClient_ {
    public static void main(String[] args) {

        Socket socket = null;
        OutputStream os = null;

        try {
            //1.知道服务器的地址
            InetAddress inetAddress = InetAddress.getByName("127.0.0.1" );
            int port = 6987;
            //2.创建一个Socket连接
            socket = new Socket(inetAddress , port);
            //3.发送消息
            os = socket.getOutputStream();
            os.write("真不错,住在山里面真不错!".getBytes());

        } catch (Exception e) {
            e.printStackTrace();
        } finally { //注意先开后关原则
            if (os   != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}


package com.net;

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

//服务端
@SuppressWarnings({"all"})
public class TCPServer_ {
    public static void main(String[] args) {

        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;



        try {
            //1.得有一个端口地址
            serverSocket = new ServerSocket(6987);
            //2.监听是否有连接请求,可以用循环持续监听
            //while (true) {
                socket = serverSocket.accept();
                //3.读取客户端信息
                is = socket.getInputStream();

                //管道流,这样做可以避免中文的字节流在中间断开而产生乱码
                baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len;
                while ((len = is.read(buffer)) != -1) {
                    baos.write(buffer, 0, len);
                }

                System.out.println(baos.toString());


        } catch (IOException e) {
            e.printStackTrace();
        } finally { //注意先开后关原则
            if (baos != null){
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (serverSocket != null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}