欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java socket 线程池服务器与客户端

程序员文章站 2022-06-06 12:08:08
...

服务器:

import java.io.IOException;
import java.net.ServerSocket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadServer {

    public static void main(String[] args) {
        try {
            ServerSocket server = new ServerSocket(20011);
            ExecutorService executor=Executors.newFixedThreadPool(20);
            // 构造一个线程池
            //ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 4, 3, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3),
                 //   new ThreadPoolExecutor.DiscardOldestPolicy());
            boolean flag = true;
            while (flag) {
                System.out.println("准备接入客户端");
                executor.execute(new Handler(server.accept())); 
                System.out.println("客户端已经连接");
            }
            server.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    
    
}
    


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Arrays;


public class Handler extends Thread {

    private Socket client = null;
    private BufferedReader input = null;
    private OutputStream out = null;
    //获取Socket的输入流,用来接收从服务端发送过来的数据 
    InputStream buf =  null;  
    
    public Handler(Socket client ) {
        this.client = client;
        try {
            input = new BufferedReader(new InputStreamReader(client.getInputStream()));
            out = client.getOutputStream();
            buf = client.getInputStream();
            start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public void run() {
        try {
            try {
                byte[] b = new byte[1024];
                int rc=0;
                int c = 0;
                rc = buf.read(b, c, 1024);
                byte[] byte8583 = Arrays.copyOfRange(b, 0, rc);
                System.out.println("post:"+byteToHexString(byte8583));
                byte[] result = send8583(byte8583, "139.129.223.93", 8500);
                System.out.println("return:"+byteToHexString(result));
                out.write(result,0,result.length);
                out.flush();
            }catch (IOException e)  {
                String error = "IOException";
                System.out.println(error);
            }catch (IllegalArgumentException e) {
            } finally{
                out.close();
                input.close();
                client.close();
            }
        } catch (IOException e) {
            System.out.println("发生了IOException");
        }
    }
    
}

客户端:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.SocketTimeoutException;

public class Client {

    public static void main(String[] args) throws IOException {
        Socket client = new Socket("127.0.0.1", 20011);  
        //客户端请求与本机在20011端口建立TCP连接 
        client.setSoTimeout(10000);             
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); 
        //获取键盘输入        
        PrintStream out = new PrintStream(client.getOutputStream());          
        //获取Socket的输出流,用来发送数据到服务端                  
        BufferedReader buf =  new BufferedReader(new InputStreamReader(client.getInputStream()));  
        //获取Socket的输入流,用来接收从服务端发送过来的数据 
        boolean flag = true;  
        while(flag){  
            System.out.print("输入信息:");  
            String str = input.readLine();  
            out.println(str);  
            //发送数据到服务端   
            if("end".equals(str)){  
                flag = false;  
            }else{  
                try{  
                    String echo = buf.readLine();  
                    System.out.println(echo);  
                }catch(SocketTimeoutException e){  
                    System.out.println("Time out, No response");  
                }  
            }  
        }  
        input.close();  
        if(client != null){  
            //如果构造函数建立起了连接,则关闭套接字,如果没有建立起连接,自然不用关闭  
            System.out.println("客户端关闭");
            client.close(); //只关闭socket,其关联的输入输出流也会被关闭  
        } 
    }
    
    
}