• Tom猫2.0-Socket+Thread


    • Client
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.Scanner;
    public class TestSockClient {
        public static void main(String[] args) {
            DataInputStream dis = null;
            DataOutputStream dos = null;
            Socket socket = null;
            Scanner scan = null;
            String  sendStr;
            String receiveStr;
            try {
                socket = new Socket("localhost", 8888);
                dis = new DataInputStream(socket.getInputStream());
                dos = new DataOutputStream(socket.getOutputStream());
                scan = new Scanner(System.in);
                do {
                    System.out.print("我:");
                    sendStr = scan.nextLine();
                    dos.writeUTF(sendStr);
                    if ((receiveStr = dis.readUTF()) != null) {
                        System.out.println(receiveStr);
                    }
                } while (!sendStr.equals("88"));
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            finally {
                try {
                    dis.close();
                    dos.close();
                    socket.close();
                    scan.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    • Server
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.EOFException;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.ArrayList;
    import java.util.List;
    
    public class TestSockServer {
        public static void main(String[] args) {
            ServerSocket server = null;
            Socket socket = null;
            List<ClientService> cslist = new ArrayList<>();
            try{
                 //开门营业
                server = new ServerSocket(8888);
                System.out.println("服务器已开启");
                while(true){
                    //顾客来
                    socket = server.accept();
                    //给分配服务员
                    ClientService cs = new TestSockServer().new ClientService(socket);
                    //记录分配情况
                    cslist.add(cs);
                     //开始服务
                    cs.start();
                }
            } catch(IOException e){
                e.printStackTrace();
            }catch (Exception e) {
                e.printStackTrace();
            }
            finally{
                try{
                    socket.close();
                    server.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
        }
         class ClientService extends Thread{
            Socket socket;
            public ClientService(Socket socket) {
                this.socket = socket;
            }
            @Override
            public void run() {
                DataOutputStream dos = null;
                DataInputStream dis = null;
                try{
                    dos = new DataOutputStream(socket.getOutputStream());
                    dis = new DataInputStream(socket.getInputStream());
                    String receiveStr ;
                    while((receiveStr = dis.readUTF())!= null) {
                        System.out.println(socket.getPort()+"客户端发送:"+receiveStr);
                        dos.writeUTF(socket.getPort()+":"+receiveStr);
                    }
                }catch(EOFException e) {
                    System.out.println(socket.getPort()+"客户端结束服务");
                }catch(IOException e){
                    e.printStackTrace();
                }catch (Exception e) {
                    e.printStackTrace();
                }
                finally{
                    try{
                        dos.close();
                        dis.close();
                        socket.close();
                    }catch(IOException e){
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
    • 运行结果


    ljm要加油
  • 相关阅读:
    jmeter实现multipart/form-data类型请求
    jmeter(正则提取器、json提取器)做接口关联
    windows使用ubuntu启动linux服务
    Jmeter+Ant+Jenkins环境搭建
    ubuntu网卡驱动的安装
    Ubuntu 挂载U盘
    Linux下安装jdk8步骤详述(转载)
    springboot项目:项目部署
    springboot项目:Redis缓存使用
    springboot项目:Redis分布式锁的使用(模拟秒杀系统)
  • 原文地址:https://www.cnblogs.com/ljmmm1/p/14296506.html
Copyright © 2020-2023  润新知