刚学到基于 TCP/IP的 “ServerSocket和Socket之间的通信”问题,我总结一下,大家帮看有什么漏的或者错的。现在假设没有任何客户端和服务器连接。步骤为:
一、服务器先通过 “ServerSocket(int port)”new一个ServerSocket对象。(因为要实现连接,服务器要先等待)。二、调用它的accept()方法,处于blocks 状态,等待客户端 new Socket(String host, int port).三、建立连接,都生成各自的Socket对象。四、客户端发送请求,服务器回复请求···如此循环(进行 I/O 数据切换)。五、关闭 Stream.六、关闭 Socket.希望大家给个意见,或是漏了哪儿了。还有一个问题是 Port 包含多个 Socket (因为服务器要和多个客户端进行交流)。

解决方案 »

  1.   

    一般都是服务器在一个wellknown端口不断监听,进来了客户,服务器会另外开其他的端口和他进行通讯,而不会占用这个wellkonwn端口。楼主要清楚,端口是TCP/IP协议需要用来确定将数据传给哪一个进程用的,所以不存在多个socket共用一个端口。
      

  2.   

    一般都是服务器在一个wellknown端口不断监听,进来了客户,服务器会另外开其他的端口和他进行通讯,而不会占用这个wellkonwn端口。楼主要清楚,端口是TCP/IP协议需要用来确定将数据传给哪一个进程用的,所以不存在多个socket共用一个端口。 用消息广播者对象vector实现同步化
      

  3.   

    socket就是端口不是么,每个客户端和服务器连接的socket都是不一样的,这我知道。
      

  4.   

    我在 google搜的英文都这样解释的:
    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 )
      

  5.   

    大哥,怎么我用积分下不了,好奇怪,能不能发给我哈?
    [email protected]