• Java基础之使用多线程处理多客户端请求


     Myserver2.java:

    package socket;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class Myserver2 {
    	public static void main(String[] args) throws IOException{
    		
    		try {
    			 ServerSocket serverSocket = new ServerSocket(8888);
    			 while(true){
    				 Socket socket = serverSocket.accept();
    				 ServerThread serverThread = new ServerThread(socket);
    				 serverThread.start();
    			 }
    
    
    		} catch (Exception e) {
    			// TODO: handle exception
    		}
    	}
    }
    

      ServerThread.java:

    package socket;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.ObjectInputStream;
    import java.io.OutputStream;
    import java.net.Socket;
    
    public class ServerThread extends Thread{
    	
    	Socket socket ;
    	public ServerThread(Socket socket){
    		this.socket = socket;
    	}
    	
    	
    	@Override
    	public void run() {
    		try {
    			InputStream in = socket.getInputStream();
    			BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    	        String info = null;
    	        while((info = reader.readLine())!=null){
    	        	System.out.println("I am server! 接收到的客户端消息:"+info);
    	        }
    	        socket.shutdownInput();
    	        OutputStream out = socket.getOutputStream();
    	        out.write("已收到!!!...".getBytes());
    	        socket.shutdownOutput();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }
    

      Myclient1.java:

    package com.example.demo;
    
    import java.io.*;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class Myclient {
        public static void main(String[] args) {
            try {
                Socket socket = new Socket("localhost",8888);
                System.out.println("客户端1启动");
                OutputStream out = socket.getOutputStream();
                out.write("hello socket!".getBytes());
                socket.shutdownOutput();
                InputStream input = socket.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                String info = null;
                while((info = reader.readLine())!=null){
                    System.out.println("I am client1! 接收到的服务端端消息:"+info);
                }
                socket.shutdownInput();
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
    }
    

      多创建几个client。运行看结果:

  • 相关阅读:
    Mysql大量插入随机数据方法--存储过程
    Linux永久修改系统时间和时区方法
    python反转字符串(简单方法)及简单的文件操作示例
    sql怎么批量替换字段里的字符串的
    varchar和Nvarchar区别
    VS改大小写的快捷键
    SQL中PIVOT 行列转换
    [转]VS中展开和折叠代码
    Bootstrap 标签页(Tab)插件
    C# DataTable 和List之间相互转换的方法
  • 原文地址:https://www.cnblogs.com/jccjcc/p/14481501.html
Copyright © 2020-2023  润新知