刚学到基于 TCP/IP的 “ServerSocket和Socket之间的通信”问题,我总结一下,大家帮看有什么漏的或者错的。现在假设没有任何客户端和服务器连接。步骤为:
一、服务器先通过 “ServerSocket(int port)”new一个ServerSocket对象。(因为要实现连接,服务器要先等待)。二、调用它的accept()方法,处于blocks 状态,等待客户端 new Socket(String host, int port).三、建立连接,都生成各自的Socket对象。四、客户端发送请求,服务器回复请求···如此循环(进行 I/O 数据切换)。五、关闭 Stream.六、关闭 Socket.希望大家给个意见,或是漏了哪儿了。还有一个问题是 Port 包含多个 Socket (因为服务器要和多个客户端进行交流)。
一、服务器先通过 “ServerSocket(int port)”new一个ServerSocket对象。(因为要实现连接,服务器要先等待)。二、调用它的accept()方法,处于blocks 状态,等待客户端 new Socket(String host, int port).三、建立连接,都生成各自的Socket对象。四、客户端发送请求,服务器回复请求···如此循环(进行 I/O 数据切换)。五、关闭 Stream.六、关闭 Socket.希望大家给个意见,或是漏了哪儿了。还有一个问题是 Port 包含多个 Socket (因为服务器要和多个客户端进行交流)。
A port is a service provided by a machine. That service is identified by a number. There are a lot of standardized port numbers for popular services. Port 22 is for FTP. Port 80 is for HTTP. There are many, many others.A socket is the way a server and a client keep track of requests. For example, LQ.org allows access to port 80 (HTTP) for the purposes of reading these forums. When my computer requests to see the content of the forums, a socket is created, and a "conversation" between the server and my machine occurs. The thing is, I'm not the only person trying to read the forums. So, to keep the server from getting conversations mixed up, each request gets a different socket. As I recall, there can be 64K sockets (or simultaneous conversations if you prefer). The thing to note is this: that's 64K sockets per port, because all these requests are coming into the server on port 80, right?So, ports are a broad reference to a type of service, whereas a socket refers to a specific connection on a specific port.EDIT:
Oh, forgot... ports and sockets are both typically represented as integers. At I fairly certain; both can take a value from 0 to ( 64K - 1 )
[email protected]