zoukankan      html  css  js  c++  java
  • 建立简单的服务器端程序 分类: B1_JAVA 2013-10-08 21:53 503人阅读 评论(0) 收藏

      参考自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.

    版权声明:本文为博主原创文章,未经博主允许不得转载。

    欢迎转载,请注明来自: www.lujinhong.com www.cnblogs.com/lujinhong2
  • 相关阅读:
    ubuntu安装谷歌浏览器
    Andoid之硬件访问服务1(让Andoid应用程序访问c库)
    JNI intArray
    JNI Hello World
    Ubuntu 14.04 Sublime Text3 Java编译运行(最简单的方法)
    Python中sys.argv的用法
    python连接mysql数据库
    python中join()函数讲解
    python中split()函数讲解
    深度学习入门教程
  • 原文地址:https://www.cnblogs.com/lujinhong2/p/4637388.html
Copyright © 2011-2022 走看看