简介
“一切皆Socket!”
话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket。
网络中的进程是通过socket来通信的,那什么是socket呢?
> socket起源于Unix,而Unix/Linux基本哲学之一就是“一切皆文件”,都可以用“打开open –> 读写write/read –> 关闭close”模式来操作。
> 我的理解就是Socket就是该模式的一个实现,socket即是一种特殊的文件,一些socket函数就是对其进行的操作(读/写IO、打开、关闭),这些函数我们在后面进行介绍。
类图
Socketoptions接口
该接口定义了socket编程里的基本方法,该接口被SocketImpl
和DataGramSocketImpl
所实现。
声明成员
修饰符 |
类型 |
成员名 |
作用 |
@Native public static final |
int |
TCP_NODELAY |
0x0001 |
@Native public static final |
int |
SO_BINDADDR |
0x000F |
@Native public static final |
int |
SO_REUSEADDR |
0x04 |
@Native public static final |
int |
SO_REUSEPORT |
0x0E |
@Native public static final |
int |
SO_BROADCAST |
0x0020 |
@Native public static final |
int |
IP_MULTICAST_IF |
0x10 |
@Native public static final |
int |
IP_MULTICAST_IF2 |
0x1f |
@Native public static final |
int |
IP_MULTICAST_LOOP |
0x12 |
@Native public static final |
int |
IP_TOS |
0x3 |
@Native public static final |
int |
SO_LINGER |
0x0080 |
@Native public static final |
int |
SO_TIMEOUT |
0x1006 |
@Native public static final |
int |
SO_SNDBUF |
0x1001 |
@Native public static final |
int |
SO_RCVBUF |
0x1002 |
@Native public static final |
int |
SO_KEEPALIVE |
0x0008 |
@Native public static final |
int |
SO_OOBINLINE |
0x1003 |
private static final |
Set<SocketOption<?>> |
socketOptions |
|
private static final |
Set<ServerSocketOption<?>> |
ServerSocketOptions |
|
提供了两个方法来设置和获取这些键值
1 2 3
| public void setOption(int optID, Object value) throws SocketException;
public Object getOption(int optID) throws SocketException;
|
成员变量及方法
修饰符 |
类型 |
变量名 |
默认值 |
说明 |
default |
Socket |
socket |
null |
实际socket对象 |
default |
ServerSocket |
serverSocket |
null |
实际serverSocket对象 |
protected |
FileDesceriptor |
fd |
|
文件描述器 |
protected |
InetAddress |
address |
|
远端主机地址 |
protected |
int |
port |
|
远端主机端口 |
protected |
int |
localPort |
|
本机地址 |
方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| protected abstract void create(boolean stream) throws IOException;
protected abstract void connect(String host, int port) throws IOException;
protected abstract void connect(InetAddress address, int port) throws IOException;
protected abstract void connect(SocketAddress address, int timeout) throws IOException;
protected abstract void bind(InetAddress host, int port) throws IOException;
protected abstract void listen(int backlog) throws IOException;
protected abstract void accept(SocketImpl s) throws IOException;
protected abstract InputStream getInputStream() throws IOException;
protected abstract OutputStream getOutputStream() throws IOException;
protected abstract int available() throws IOException;
protected abstract void close() throws IOException;
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| protected void shutdownInput() throws IOException { throw new IOException("Method not implemented!"); }
protected void shutdownOutput() throws IOException { throw new IOException("Method not implemented!"); }
protected FileDescriptor getFileDescriptor() { return fd; }
protected InetAddress getInetAddress() { return address; }
protected int getPort() { return port; }
protected boolean supportsUrgentData () { return false; }
protected abstract void sendUrgentData (int data) throws IOException;
protected int getLocalPort() { return localport; }
|
1 2 3 4
| public String toString() { return "Socket[addr=" + getInetAddress() + ",port=" + getPort() + ",localport=" + getLocalPort() + "]"; }
|
socketOptions和ServerSocketServer初始化时机
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| static { socketOptions = Set.of(StandardSocketOptions.SO_KEEPALIVE, StandardSocketOptions.SO_SNDBUF, StandardSocketOptions.SO_RCVBUF, StandardSocketOptions.SO_REUSEADDR, StandardSocketOptions.SO_LINGER, StandardSocketOptions.IP_TOS, StandardSocketOptions.TCP_NODELAY);
serverSocketOptions = Set.of(StandardSocketOptions.SO_RCVBUF, StandardSocketOptions.SO_REUSEADDR, StandardSocketOptions.IP_TOS); } protected Set<SocketOption<?>> supportedOptions() { if (getSocket() != null) { return socketOptions; } else { return serverSocketOptions; } }
|