• 【Java】简易Socket连接实现


    客户端:

    import java.io.*;
    import java.net.Socket;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Scanner;
    /**
     * 客户端
     * */
    public class SocketClient {
        Socket socket;
        String address="127.0.0.1";
        int portNumber=4775;
        PrintWriter printWriter;
        BufferedReader bufferedReader;
        public SocketClient(){
            try {
                socket=new Socket(address,portNumber);
                bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
                printWriter=new PrintWriter(socket.getOutputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public void chat(){
            System.out.println("请输入要发送的信息:");
            Scanner scanner=new Scanner(System.in);
            String str="";
            while(true){
                str=scanner.nextLine();
                    printWriter.println(str);
                    printWriter.flush();
                    System.out.println(getCurTime()+"客户端:"+str);
                    if(str.equals("bye")){
                        break;
                    }
                String server_str= null;
                try {
                    server_str = bufferedReader.readLine();
                    System.out.println(getCurTime()+"服务器:"+server_str);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            scanner.close();
            close();
        }
        public static String getCurTime(){
            Date date=new Date();
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return simpleDateFormat.format(date);
        }
        public void close(){
            try {
                printWriter.close();
                bufferedReader.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            SocketClient socketClient=new SocketClient();
            socketClient.chat();
        }
    }

    服务端:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    /**
     * 服务器端
     * */
    public class SocketServer {
        private int server_port=4775;
        private ServerSocket serverSocket;
        PrintWriter printWriter;
        BufferedReader bufferedReader;
        Socket socket;
        public SocketServer(){
            try {
                serverSocket=new ServerSocket(server_port);
                System.out.println("服务器已启动!");
                socket=serverSocket.accept();
                System.out.println("有用户接入了!");
                printWriter=new PrintWriter(socket.getOutputStream());
                bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
                while (true){
                    String str=bufferedReader.readLine();
                    System.out.println("客户端:"+str);
                    if(str.equals("bye")){
                        break;
                    }
                    printWriter.println(str);
                    printWriter.flush();
                    System.out.println("发送数据给客户端~");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    printWriter.close();
                    bufferedReader.close();
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
            }
        }
    
        public static void main(String[] args) {
            new SocketServer();
        }
    }
  • 相关阅读:
    CentOS7.6下 MariaDB的MHA 集群搭建(一)
    Mariadb10.4 集群压力测试(一)
    Galera 核心参数详解(一)
    Mariadb10.4+ ERROR 1118 (42000): Row size too large (> 8126). Changing some columns to TEXT or BLOB may help. In current row format, BLOB prefix of 0 bytes is stored inline.
    手动打造一个弹窗程序
    IAT HOOK
    进制的本质
    基于数组越界的缓冲区溢出
    函数调用堆栈图-c语言
    算法之二分查找(上)-c语言实现
  • 原文地址:https://www.cnblogs.com/robotpaul/p/12045771.html
Copyright © 2020-2023  润新知