• 建立简单的服务器端程序


      参考自core java


    package com.lujinhong.corejava;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Scanner;
    
    public class EchoServer {
    
    	public static void main(String[] args) {
    		
    		try {
    			ServerSocket serverSocket = new ServerSocket(8019);
    			Socket socket = serverSocket.accept();
    			
    			InputStream is = socket.getInputStream();
    			OutputStream os = socket.getOutputStream();
    			
    			PrintWriter pw = new PrintWriter(os);
    			Scanner sc = new Scanner(is);
    			
    			Boolean flag = false;
    			String line = null;
    			String exitString = "bye";
    			
    			while(!flag && sc.hasNextLine()){
    				pw.println("Hello, type " + exitString + " to exit!");
    				line = sc.nextLine();
    				if(line.trim().equals(exitString)){
    					flag = true;
    				}else{
    					pw.println("Hello, "+line);
    				}
    			}
    			pw.close();
    			sc.close();
    			serverSocket.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    

    使用线程实现多个客户端同时访问:

    package com.lujinhong.corejava;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Scanner;
    
    public class MultiEchoServer {
    
    	public static void main(String[] args) {
    		try {
    			ServerSocket serverSocket = new ServerSocket(8189);
    			while (true) {
    				Socket socket = serverSocket.accept();
    
    				Runnable r = new ThreadedEchoHandler(socket);
    				Thread thread = new Thread(r);
    				thread.start();
    			}
    
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    
    	}
    
    }
    
    class ThreadedEchoHandler implements Runnable {
    
    	private Socket s = null;
    
    	public ThreadedEchoHandler(Socket socket) {
    		s = socket;
    	}
    
    	@Override
    	public void run() {
    		try {
    			InputStream is = s.getInputStream();
    			OutputStream os = s.getOutputStream();
    
    			PrintWriter pw = new PrintWriter(os);
    			Scanner sc = new Scanner(is);
    
    			Boolean flag = false;
    			String line = null;
    			String exitString = "bye";
    
    			while (!flag && sc.hasNextLine()) {
    				pw.println("Hello, type " + exitString + " to exit!");
    				line = sc.nextLine();
    				if (line.trim().equals(exitString)) {
    					flag = true;
    				} else {
    					pw.println("Hello, " + line);
    				}
    			}
    			sc.close();
    			pw.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }


    In this program, we spawn a separate thread for each connection. This approach is not satisfactory for highperformance servers. You can achieve greater server throughput by using features of the java.nio package. See www.ibm.com/developerworks/java/library/j-javaio for more information.

  • 相关阅读:
    POJ 3211 Washing Clothes
    MySQL 优化Limit分页
    signed 与 unsigned 有符号和无符号数
    C/C++ 变量的初始化
    C/C++ 变量的初始化
    返回值的判断
    返回值的判断
    《涅槃经》的研读
    《涅槃经》的研读
    Opencv Surf算子特征提取与最优匹配
  • 原文地址:https://www.cnblogs.com/pangblog/p/3359928.html
Copyright © 2020-2023  润新知