zoukankan      html  css  js  c++  java
  • ServerSocket的介绍

    导语

    仅仅只有Socket类是不足以编写服务器的。要创建一个Socket,你需要知道希望连接哪个Internet主机。编写服务器程序时,无法预先了解哪个主机会联系你,即使确实知道,你也不清楚那个主机希望何时与你联系。对于服务器端的Socket,java提供了一个ServerSocket类表示服务器Socket。从技术上讲,服务器Socket在服务器上运行,监听入站TCP连接,每个服务器Socket监听服务器机器上的一个特定端口。当有个客户端尝试连接这个端口时,服务器就被唤醒,协商建立客户端和服务器之间的连接,并返回一个常规的Socket对象,表示两台主机之间的Socket。换句话说,服务器等待连接,客户端发起连接,一旦ServerSocket建立了连接,服务器会使用一个常规的Socket对象向客户端发送数据。

    使用ServerSocket

    ServerSocket类包含了服务器端Socket所需的全部内容。其中包括构造ServerSocket对象,指定端口监听连接的方法,配置各个服务器Socket选项的方法,以及其它一些常见的方法。下面是服务器程序的基本生命周期:

    • 使用一个ServerSocket()构造函数在一个特定端口创建一个新的ServerSocket
    • ServerSocket使用其accept()方法监听这个端口的入站连接。accept()会一直阻塞,直到一个客户端尝试建立连接,此时accept()将会返回一个连接客户端和服务器的Socket对象
    • 根据服务器的类型,会调用Socket的getInputSteam()方法或getOutputSteam()方法,或者这两个方法都调用,以获得与客户端通信的输入和输出流
    • 服务器和客户端根据已协商的协议交互
    • 服务器或客户端关闭连接
    • 服务器返回到步骤2,等待下一次连接

    简单的daytime服务器

    public static void main(String[] args) {
    		
    		try (ServerSocket server = new ServerSocket(13)) {
    			while (true) {
    				try (Socket conn = server.accept()) {
    					Writer out = new OutputStreamWriter(conn.getOutputStream());
    					out.write(new Date().toString());
    					out.flush();
    					out.close();
    					conn.close();
    				} catch (IOException e) {}
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    

    多线程服务器

    操作系统会把指向某个特定端口的入站连接请求存储在一个先进先出的队列中。默认地,Java会将这个队列的长度设置为50,但不同的操作系统会有所不同。有些操作系统有一个最大队列长度,例如,在FreeBSD上,默认的最大队列长度为128。在构建ServerSocket时可以指定这个值,但是这个值不能超过操作系统设置的值。当队列中的连接达到最大容量时,主机会拒绝这个端口上额外的连接,直到队列腾出新的位置为止。为了尽快能够处理队列中的连接,可以采用多线程的方式来处理每个socket。

    package com.dy.xidian;
    
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.io.Writer;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Date;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class PooledDaytimeServer {
    	
    	private final static int PORT = 13;
    	
    	public static void main(String[] args) {
    		ExecutorService pool = Executors.newFixedThreadPool(50);
    		try (ServerSocket server = new ServerSocket(PORT)) {
    			while (true) {
    				try {
    					Socket conn = server.accept();
    					pool.execute(new DaytimeTask(conn));
    				} catch (IOException e) {}
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    
    class DaytimeTask implements Runnable {
    	
    	public Socket conn;
    	
    	public DaytimeTask(Socket _conn) {
    		conn = _conn;
    	}
    	
    	@Override
    	public void run() {
    		try {
    			Writer out = new OutputStreamWriter(conn.getOutputStream());
    			out.write(new Date().toString());
    			out.flush();
    			out.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally {
    			if (conn != null)
    				try {
    					conn.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    		}
    	}
    }
    

    从客户端读取数据

    echo协议是最简单的交互式TCP服务之一。客户端打开指向echo服务器端口7的socket,并发送数据。服务器将数据发回。这个过程一直继续,直到客户端关闭连接为止。echo协议很有用,可以测试网络,确保数据没有被有问题的路由器或防火墙所破坏。下面的代码中使用NIO中的技术,如果对NIO不了解请移步

    package com.dy.xidian;
    
    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.net.ServerSocket;
    import java.nio.ByteBuffer;
    import java.nio.channels.SelectionKey;
    import java.nio.channels.Selector;
    import java.nio.channels.ServerSocketChannel;
    import java.nio.channels.SocketChannel;
    import java.util.Iterator;
    import java.util.Set;
    
    public class PooledDaytimeServer {
    	
    	private final static int PORT = 7;
    	
    	public static void main(String[] args) {
    		ServerSocketChannel serverChannel;
    		Selector selector;
    		
    		try {
    			serverChannel = ServerSocketChannel.open();
    			ServerSocket server = serverChannel.socket();
    			InetSocketAddress address = new InetSocketAddress(PORT);
    			server.bind(address);
    			serverChannel.configureBlocking(false);
    			selector = Selector.open();
    			serverChannel.register(selector, SelectionKey.OP_ACCEPT);
    		} catch (IOException e) {
    			e.printStackTrace();
    			return;
    		}
    		
    		while (true) {
    			try {
    				selector.select();
    			} catch (IOException e) {
    				e.printStackTrace();
    				break;
    			}
    			
    			Set<SelectionKey> readyKeys = selector.selectedKeys();
    			Iterator<SelectionKey> iterator = readyKeys.iterator();
    			
    			while (iterator.hasNext()) {
    				SelectionKey key = iterator.next();
    				iterator.remove();
    				try {
    					if (key.isAcceptable()) {
    						ServerSocketChannel server = (ServerSocketChannel) key.channel();
    						SocketChannel client = server.accept();
    						System.out.println("Accepted connection from " + client);
    						client.configureBlocking(false);
    						SelectionKey clientKey = client.register(selector, SelectionKey.OP_WRITE | SelectionKey.OP_READ);
    						ByteBuffer buffer = ByteBuffer.allocate(100);
    						clientKey.attach(buffer);
    					}
    					if (key.isReadable()) {
    						SocketChannel client = (SocketChannel) key.channel();
    						ByteBuffer output = (ByteBuffer) key.attachment();
    						client.read(output);
    					}
    					if (key.isWritable()) {
    						SocketChannel client = (SocketChannel) key.channel();
    						ByteBuffer output = (ByteBuffer) key.attachment();
    						output.flip();
    						client.write(output);
    						output.compact();
    					}
    				} catch (IOException e) {
    					key.cancel();
    					try {
    						key.channel().close();
    					} catch (IOException e1) {}
    				}
    			}
    		}
    	}
    }
    

    关闭服务器Socket

    使用完一个服务器Socket,就应该将其关闭。但不要把关闭ServerSocket与关闭Socket混淆。关闭ServerSocket会释放本地主机的一个端口,允许另一个服务可以绑定这个端口,并且它还会中断该ServerSocket已经接受的目前处于打开状态的所有Socket。关闭ServerSocket基本使用下面的流程:

    ServerSocket server = null;
    try {
    	server = new ServerSocket(port);
    } finally {
    	if (server != null) {
    		try {
    			server.close();
    		} catch (IOException e) {}
    	}
    }
    

    构造服务器Socket

    创建一个Socket监听80端口

    ServerSocket httpd = new ServerSocket(80);
    

    如果主机上有多个IP地址,我们只想监听特定的IP地址上请求(50表示入栈连接的最大个数)

    InetAddress local = InetAddress.getByName("192.168.0.100");
    ServerSocket httpd = new ServerSocket(80, 50, local);
  • 相关阅读:
    python selenium-webdriver 执行js (八)
    python selenium-webdriver 等待时间(七)
    python selenium-webdriver 处理JS弹出对话框(六)
    python selenium-webdriver 元素操作之键盘操作(五)
    python selenium-webdriver 元素操作之鼠标操作(四)
    Jmeter 设置全部变量(十三)
    Jmeter 执行Python代码(十二)
    Jmeter Java Request (十一)
    Jenkins进阶-定时任务(17)
    JMeter 设置间隔时间(十)
  • 原文地址:https://www.cnblogs.com/xidongyu/p/6209432.html
Copyright © 2011-2022 走看看