1.首先用Java实现hello/hi网络聊天程序
客户端
public class TCPClient { public static void main(String[] args) throws IOException { // 创建发送端socket对象 Socket s = new Socket("127.0.0.1", 6666); // 键盘录入数据 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // 包装通道内的流 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); // 从服务端获取返回的数据 BufferedReader brServer = new BufferedReader(new InputStreamReader(s.getInputStream())); String line = null; while ((line = br.readLine()) != null) { // 结束标记:quit if ("quit".equals(line)) { break; } // 往通道里写数据 bw.write(line); bw.newLine(); bw.flush(); // 读取服务器传送过来的数据 String lineFromServer = brServer.readLine(); System.out.println(lineFromServer); } // 释放资源 br.close(); s.close(); } }
服务器端
public class TCPServer { public static void main(String[] args) throws IOException { // 创建接收端Socket对象 ServerSocket ss = new ServerSocket(6666); // 监听客户端连接,返回一个对应的Socket对象 Socket s = ss.accept(); // 包装通道内的流 BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); String line = null; while ((line = br.readLine()) != null) { System.out.println(line); // 往客户端发送数据 bw.write("服务器端已收到:"+ line); bw.newLine(); bw.flush(); } //释放资源 s.close(); } }
结果
- 服务端
- 客户端
2.追踪客户端Socket
- 起始:第一层:TCPClient.java。
在Java中只需一行代码,客户端就创建了socket并向服务端发出连接请求(成功的话更是直接完成了与服务端的三次握手)。
// 创建发送端socket对象 Socket s = new Socket("127.0.0.1", 6666);
- 第二层:Socket.class
可以看到Socket的构造方法是直接调用本类的构造方法,因为传入的主机地址host!=null,所以这一层的实现为this(new InetSocketAddress(host, port))。
InetSocketAddress只是一个把主机地址和端口号封装起来的类,不是重点。因此继续跟踪this()
public Socket(String host, int port) throws UnknownHostException, IOException { this(host != null ? new InetSocketAddress(host, port) : new InetSocketAddress(InetAddress.getByName((String)null), port), (SocketAddress)null, true); }
- Socket构造方法:Socket.class
把异常处理的代码去掉,可以看到try语句里的实现顺序:**this.createImpl(stream)->this.bind(localAddr)->this.connect(address)**。
分别继续深入看看三个方法各做了什么
private Socket(SocketAddress address, SocketAddress localAddr, boolean stream) throws IOException { // Socket成员变量的初始化...(省略) try { this.createImpl(stream); if (localAddr != null) { this.bind(localAddr); } this.connect(address); } // 异常处理(省略) }
3. 以this.createImpl(stream)为例的追踪过程
- impl是一个SocketImpl实例。SocketImpl是一个抽象类,是实际实现套接字的所有类的公共超类。 它用于创建客户端和服务器套接字。
protected abstract void create(boolean stream) throws IOException 创建流或数据报套接字。 参数 stream - 如果true ,创建一个流套接字; 否则,创建一个数据报套接字。
从API得知stream如果是true则建立一个TCPsocket,false则UDPSocket
void createImpl(boolean stream) throws SocketException { if (this.impl == null) { this.setImpl(); } try { this.impl.create(stream); this.created = true; } }
继续跟进this.impl.create:AbstractPlainSocketImpl第三层
protected synchronized void create(boolean stream) throws IOException { this.stream = stream; if (!stream) { //略 } else { this.fd = new FileDescriptor(); this.socketCreate(true); SocketCleanable.register(this.fd); }
继续跟进this.socketCreate(true):PlainSocketImpl第四层
native void socketCreate(boolean var1) throws IOException;
查看native方法socketCreate的源码:下载JDK源码包,找到PlainSocketImol.c,第五层 可以看到socetCreate里用C语言做了fd=JVM_Socket(domain, type, 0)
JNIEXPORT void JNICALL Java_java_net_PlainSocketImpl_socketCreate(JNIEnv env, jobject this, jboolean stream) { jobject fdObj, ssObj; int fd; //... int type = (stream ? SOCK_STREAM : SOCK_DGRAM); if ((fd = JVM_Socket(domain, type, 0)) == JVM_IO_ERR) { / note: if you run out of fds, you may not be able to load * the exception class, and get a NoClassDefFoundError * instead. / NET_ThrowNew(env, errno, "can't create socket"); return; } //... / * If this is a server socket then enable SO_REUSEADDR * automatically and set to non blocking. */ ssObj = (env)->GetObjectField(env, this, psi_serverSocketID); if (ssObj != NULL) { int arg = 1; SET_NONBLOCKING(fd); if (JVM_SetSockOpt(fd, SOL_SOCKET, SO_REUSEADDR, (char)&arg, sizeof(arg)) < 0) { NET_ThrowNew(env, errno, "cannot set SO_REUSEADDR"); close(fd); return; } } (*env)->SetIntField(env, fdObj, IO_fd_fdID, fd); }
查看JVM_Socket的实现。在jvm.cpp中找到第六层
可以看到最终最终,用到了os::socket(domain, type, protocol)。从形式上也能看出,这是调用了Linux的socket接口了,再往下就是系统调用级别了。
JVM_LEAF(jint, JVM_Socket(jint domain, jint type, jint protocol)) JVMWrapper("JVM_Socket"); return os::socket(domain, type, protocol); JVM_END
至此,经历了六层的搜索,终于追踪到了Java创建socket的接口的最底层是调用Linux的socket函数。
4. Java中各个socket方法的追踪结果总结
由于其他方法的追踪过程与创建socket的过程相似,这里直接给出各个方法的调用栈。
客户端创建socket
socket(host, port) : TCPClient.java |---this.createImpl(stream) : socket.class |---this.impl.create(stream) : socket.class |---protected synchronized void create(boolean stream) : AbstractPlainSocketImpl.class |---this.socketCreate() : AbstractPlainSocketImpl.class |---native void socketCreate(boolean var1) : PlainSocketImpl.class ------JVM------ |---Java_java_net_PlainSocketImpl_socketCreate : PlainSocketImol.c |---fd=JVM_Socket(domain, type, 0) : PlainSocketImol.c |---os::socket(domain, type, protocol) : jvm.cpp
客户端bind
socket(host,port) : TCPClient.java |---this.bind(localAddr) : socket.Class |---this.getImpl().bind(addr, port): socket.class |---protected synchronized void bind(InetAddress address, int lport) : AbstractPlainSocketImpl.class |---native void socketBind(InetAddress var1, int var2) : PlainSocketImpl.class ------JVM------ |---Java_java_net_PlainSocketImpl_socketBind : PlainSocketImol.c |---NET_Bind(int fd, struct sockaddr *him, int len) : net_util_md.c |---rv = bind(fd, him, len) : net_util_md.c |---return os::bind(fd, him, (socklen_t)len) : jvm.cpp
客户端connect
socket(host,port) : TCPClient.java |---this.connect(address) : Socket.class |---public void connect(SocketAddress endpoint, int timeout) : Socket.class |---protected void connect(SocketAddress address, int timeout) : AbstractPlainSocketImpl.class |---synchronized void doConnect(address, port, timeout): AbstractPlainSocketImpl.class |---native void socketConnect(InetAddress var1, int var2, int var3) : PlainSocketImpl.class ------JVM------ |---Java_java_net_PlainSocketImpl_socketConnect : PlainSocketImol.c |---JVM_Connect(jint fd, struct sockaddr *him, jint len) : PlainSocketImol.c |---os::connect(fd, him, (socklen_t)len) : jvm.cpp
服务端创建ServerSocket
ServerSocket(6666) : TCPServer.java |---public ServerSocket(port, backlog, bindAddr) : ServerSocket.class |---this.bind(new InetSocketAddress(bindAddr, port), backlog) : ServerSocket.class |---this.getImpl().bind(epoint.getAddress(), epoint.getPort()) : ServerSocket.class // bind上面已经跟过了 |---this.getImpl().listen(backlog) : ServerSocket.class |---this.socketListen(count) : AbstractPlainSocketImpl.class |--- native void socketListen(int var1) : PlainSocketImpl.class ------JVM------ |---Java_java_net_PlainSocketImpl_socketListen : PlainSocketImol.c |---JVM_Listen(jint fd, jint count) : PlainSocketImol.c |---os::listen(fd, count) : jvm.cpp
服务器端accept
Socket s = ss.accept() : TCPServer.class |----this.implAccept(s) : ServerSocket.class |---this.getImpl().accept(si) : ServerSocket.class |---protected void accept(SocketImpl s) : AbstractPlainSocketImpl.class |---native void socketAccept(SocketImpl var1) : PlainSocketImpl.class ------JVM------ |---Java_java_net_PlainSocketImpl_socketAccept : PlainSocketImol.c |---newfd = NET_Accept(fd, (struct sockaddr )&him, (jint)&len) : PlainSocketImol.c |---JVM_Accept(jint fd, struct sockaddr *him, jint *len) : jvm.h |---jint result = os::accept(fd, him, &socklen) : jvm.cpp
5. 总结Java Socket与Linux Socket的对应关系
Java的Socket方法底层是调用Linux的Socket。各个方法与Linux socket的对应关系如下:
- 服务端
- 客户端