zoukankan      html  css  js  c++  java
  • 网络编程server

    下面中一个例子,用到了多线程.

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    class ServerOneJabber extends Thread {
    	private Socket socket;
    	private BufferedReader in;
    	private PrintWriter out;
    
    	public ServerOneJabber(Socket s) throws IOException {
    		socket = s;
    		in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    		// auto flush
    		out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
    				socket.getOutputStream())), true);
    		start();
    	}
    
    	public void run() {
    		try {
    			while (true) {
    				String str = in.readLine();
    				if (str.equals("END"))
    					break;
    				System.out.println("Ecoh:" + str);
    				out.println(str);
    			}
    			System.out.println("closing");
    			;
    		} catch (IOException e) {
    			// TODO: handle exception
    		} finally {
    			try {
    				socket.close();
    			} catch (IOException e2) {
    				// TODO: handle exception
    			}
    		}
    	}
    }
    
    public class MultiJabberServer {
    	static final int PORT = 8080;
    
    	public static void main(String args[]) throws IOException {
    		ServerSocket s = new ServerSocket(PORT);
    		System.out.println("server start");
    		try {
    			while (true) {
    				Socket socket = s.accept();
    				try {
    					new ServerOneJabber(socket);
    				} catch (IOException e) {
    					socket.close();
    				}
    			}
    		} finally {
    			s.close();
    		}
    	}
    
    }
    

      

    然后是客户端

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.security.cert.TrustAnchor;
    
    
    class JabberClientThread extends Thread{
    	private final int PORT =8080 ;
    	
    	private Socket socket ;
    	private BufferedReader in ;
    	private PrintWriter out ;
    	private static int counter =0;
    	private int id =counter++ ;
    	private static int threadcount =0;
    	public static int threadCount (){
    		return threadcount ;
    	}
    	public JabberClientThread (InetAddress addr){
    		System.out.println("making client id:"+ id) ;
    		threadcount ++  ;
    		try {
    			socket = new Socket(addr,PORT ) ;
    		} catch (IOException e) {
    			// TODO: handle exception
    		}
    		try {
    			in = new BufferedReader(
    					new InputStreamReader(socket.getInputStream()));
    			out = new PrintWriter(new BufferedWriter(
    					new OutputStreamWriter(
    							socket.getOutputStream())) , true) ;
    		} catch (Exception e) {
    			try {
    				socket.close() ;
    			} catch (Exception e2) {
    				// TODO: handle exception
    			}
    		}
    	}
    	
    	public void run (){
    		try {
    			for (int i =0; i<25;i++){
    				out.println("client id:"+i);
    				String str = in.readLine() ;
    				System.out.println(str) ;
    			}
    			out.println("END") ;
    		} catch (Exception e) {
    			// TODO: handle exception
    		}finally{
    			try {
    				socket.close() ;
    			} catch (IOException e2) {
    				threadcount --;//end this thread 
    			}
    		}
    	}
    }
    public class MultiJabberClient {	
    	static  final int MAX_THREAD =40;
    	public static void main (String args [])
    	throws IOException, InterruptedException{
    		InetAddress addr = InetAddress.getByName(null);
    		while (true){
    			if (JabberClientThread.threadCount()<MAX_THREAD){
    				new JabberClientThread(addr) ;
    			}
    		}
    	}
    	
    
    }
    

      

  • 相关阅读:
    【LuoguP4156】论战捆竹竿
    各种需要背记的图论知识
    SSD:TensorFlow中的单次多重检测器
    YOLO: 3 步实时目标检测安装运行教程 [你看那条狗,好像一条狗!]
    Tensorflow 基于分层注意网络的文件分类器
    StarSpace是用于高效学习实体向量的通用神经模型
    vrn:基于直接体积回归的单幅图像大姿态三维人脸重建
    TensorFlow官方文档
    Machine Learning From Scratch-从头开始机器学习
    Awesome-Text-Classification:文本分类资源合集
  • 原文地址:https://www.cnblogs.com/chuiyuan/p/4379031.html
Copyright © 2011-2022 走看看