📄 socketopts.java
字号:
/* * Copyright (c) 2003, The Regents of the University of California, through * Lawrence Berkeley National Laboratory (subject to receipt of any required * approvals from the U.S. Dept. of Energy). All rights reserved. */package gov.lbl.dsd.sea.nio.util;import java.net.Socket;import java.net.SocketException;import java.net.SocketOptions;import java.util.HashMap;import java.util.Map;/** * Convenience class to get and set socket options; see {@link SocketOptions} * and {@link Socket}for the corresponding keys and values. * <p> * For TCP performance tuning, see http://dsd.lbl.gov/TCP-tuning/ and in particular * http://dsd.lbl.gov/TCP-tuning/buffers.html for how to increase the max socket * buffer sizes allowed by the operating system (for MacOSX use the FreeBSD * instructions as root user). Further useful information: * http://www.psc.edu/networking/perf_tune.html * * @author whoschek@lbl.gov * @author $Author: hoschek3 $ * @version $Revision: 1.3 $, $Date: 2004/05/31 22:09:11 $ */public class SocketOpts implements SocketOptions, java.io.Serializable { protected Map options; /** * Creates an empty object (with all options yet undefined). */ public SocketOpts() { this.options = new HashMap(); } /** * Constructs and object containing the options of the given socket. */ public SocketOpts(Socket socket) throws SocketException { this(); setOption(SO_KEEPALIVE, new Boolean(socket.getKeepAlive())); setOption(SO_OOBINLINE, new Boolean(socket.getOOBInline())); setOption(SO_REUSEADDR, new Boolean(socket.getReuseAddress())); setOption(TCP_NODELAY, new Boolean(socket.getTcpNoDelay())); setOption(SO_RCVBUF, new Integer(socket.getReceiveBufferSize())); setOption(SO_SNDBUF, new Integer(socket.getSendBufferSize())); setOption(SO_LINGER, new Integer(socket.getSoLinger())); setOption(SO_TIMEOUT, new Integer(socket.getSoTimeout())); setOption(IP_TOS, new Integer(socket.getTrafficClass())); } /** * Sets the given option to the given value. * @see java.net.SocketOptions#setOption(int, java.lang.Object) */ public void setOption(int optID, Object value) { this.options.put(new Integer(optID), value); } /** * Returns the given option * @see java.net.SocketOptions#getOption(int) */ public Object getOption(int optID) { return this.options.get(new Integer(optID)); } /** * Copies the options from this object to the given socket. */ public void copyTo(Socket socket) throws SocketException { if (getOption(SO_KEEPALIVE) != null) socket.setKeepAlive(( (Boolean) getOption(SO_KEEPALIVE)).booleanValue()); if (getOption(SO_OOBINLINE) != null) socket.setOOBInline(( (Boolean) getOption(SO_OOBINLINE)).booleanValue()); if (getOption(SO_REUSEADDR) != null) socket.setReuseAddress(( (Boolean) getOption(SO_REUSEADDR)).booleanValue()); if (getOption(TCP_NODELAY) != null) socket.setTcpNoDelay(( (Boolean) getOption(TCP_NODELAY)).booleanValue()); if (getOption(SO_RCVBUF) != null) socket.setReceiveBufferSize(( (Integer) getOption(SO_RCVBUF)).intValue()); if (getOption(SO_SNDBUF) != null) socket.setSendBufferSize(( (Integer) getOption(SO_SNDBUF)).intValue()); if (getOption(SO_LINGER) != null) socket.setSoLinger(true, ( (Integer) getOption(SO_LINGER)).intValue()); if (getOption(SO_TIMEOUT) != null) socket.setSoTimeout(( (Integer) getOption(SO_TIMEOUT)).intValue()); if (getOption(IP_TOS) != null) socket.setTrafficClass(( (Integer) getOption(IP_TOS)).intValue()); } /** * Returns a detailed string representation of the receiver. */ public String toString() { String s = this.getClass().getName() + " ["; s += "SO_KEEPALIVE=" + getOption(SO_KEEPALIVE); s += ", SO_OOBINLINE=" + getOption(SO_OOBINLINE); s += ", SO_REUSEADDR=" + getOption(SO_REUSEADDR); s += ", TCP_NODELAY=" + getOption(TCP_NODELAY); s += ", SO_RCVBUF=" + getOption(SO_RCVBUF); s += ", SO_SNDBUF=" + getOption(SO_SNDBUF); s += ", SO_LINGER=" + getOption(SO_LINGER); s += ", SO_TIMEOUT=" + getOption(SO_TIMEOUT); s += ", IP_TOS=" + getOption(IP_TOS); s += "]"; return s; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -