JAVA多线程__Socket

JAVA多线程__Socket

#服务器

package com.hp.socket;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Server {

    private Map ListRecode=new HashMap<>();
    /**
     * 多线程模拟socket
     * @param args
     */
    public static void main(String[] args){
        new Server().test();
    }

    /**
     *
     */
    public void test(){
        //
        System.out.println(">>请输入要监听的端口号:");
        Scanner sc = new Scanner(System.in);
        server ser = new server();
        ser.port=sc.nextInt();
        new Thread(ser).start();
    }
    class server implements  Runnable{
        Integer port=null;
        @Override
        public void run() {
            try {
                System.out.println("开启服务器");
                ServerSocket server = new ServerSocket(port);
                accep acc = new accep();
                acc.server=server;
                new Thread(acc).start();
            } catch (IOException e){
                System.out.print(e);
            }
        }
    }

    /**
     *
     */
    class accep implements Runnable{
        ServerSocket server=null;
        @Override
        public void run() {

                while(true) {
                    try {
                    System.out.println("服务器开始监听!..");
                    Socket ser = server.accept();
                    System.out.print(ser.getRemoteSocketAddress()+"客户端请求....");
                    client cli = new client();
                    cli.clien=ser;
                    new Thread(cli).start();
                    } catch (IOException e) {
                        System.out.print(e);
                        continue;

                    }
                    }

        }
    }
    class client implements Runnable{
        Socket clien=null;
        @Override
        public void run() {
           if(clien!=null){
               try {
                   inp in = new inp();
                   in.ip=clien.getInputStream();
                   out ou = new out();
                   ou.op=clien.getOutputStream();
                   new Thread(in).start();
                   new Thread(ou).start();
               } catch (IOException e) {
                   System.out.print(e);
               }

           }
        }
    }
    class inp implements Runnable{
        InputStream ip=null;
        int len=0;
        byte[] b=new byte[1024];
        @Override
        public void run() {
                while(true){
                    try {
                    while((len= ip.read(b))!=-1){
                        System.out.println(new String(b,0,len));
                    }
                    } catch (IOException e) {
                        System.out.print(e);
                        break;
                    }
                    }
        }
    }
    class out implements Runnable{
        OutputStream op=null;
        Scanner sc=new Scanner(System.in);
        String con="";
        @Override
        public void run(){
            if(op!=null){
                while(true){
                    System.out.println("请输入要发送的信息:>>");
                    con=sc.next();
                    try {
                        op.write(con.getBytes());
                    } catch (IOException e) {
                        System.out.print(e);
                        break;
                    }
                }
            }
        }
    }

}

#客户端

package com.hp.socket;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class client {
    private Scanner sc=new Scanner(System.in);
    public  static  void main(String[] args){
        new client().test();
    }
    public void test(){
        System.out.println("请输入Ip地址>>");
        String ip=sc.next();
        System.out.println("请输入端口号>>");
        Integer port=sc.nextInt();
        clie cli = new clie();
        if(!ip.equals("")){
            cli.ip=ip;
        }
        cli.port=port;
        new Thread(cli).start();
    }
    class clie implements  Runnable{
        String ip="";
        Integer port=0;
        @Override
        public void run() {
            try {
                Socket clien = new Socket(ip,port);
                OutputStream oup = clien.getOutputStream();
                InputStream inp = clien.getInputStream();
                inp in = new inp();
                in.ip=inp;
                out ou = new out();
                ou.op=oup;
                new Thread(in).start();
                new Thread(ou).start();
            } catch (IOException e) {
                System.out.print(e);
            }

        }
    }
    class inp implements Runnable{
        InputStream ip=null;
        int len=0;
        byte[] b=new byte[1024];

        @Override
        public void run() {

                while(true){
                    try {

                    while((len= ip.read(b))!=-1){
                        System.out.println(new String(b,0,len));
                    }
                    } catch (IOException e) {
                        System.out.print(e);
                        break;
                    }
                    }



        }
    }
    class out implements Runnable{
        OutputStream op=null;
        Scanner sc=new Scanner(System.in);
        String con="";
        @Override
        public void run(){
            if(op!=null){
                while(true){
                    System.out.println("请输入要发送的信息:>>");
                    con=sc.next();
                    try {
                        op.write(con.getBytes());
                    } catch (IOException e) {
                        System.out.print(e);
                        break;
                    }
                }
            }
        }
    }

}

猜你喜欢

转载自blog.csdn.net/qq_39663113/article/details/85563939