我在一个资料上看到Socket client = serversocket.accept();client使用的端口是系统随即选择的,可是我打印出client的localport,port,发现一个是serversocket的端口,一个另外一端的port号码。俺知道使用Socket client =new Socket(ip,port)建立的client一定是使用的由系统随即选择的端口。
但是Socket client = serversocket.accept();这个client的port我还真迷惑了,如果就是使用serversocket的port,那么岂不服务器的一个端口就要和n个客户端建立连线?
但是Socket client = serversocket.accept();这个client的port我还真迷惑了,如果就是使用serversocket的port,那么岂不服务器的一个端口就要和n个客户端建立连线?
这个有什么问题
像ftp的默认21端口,架设一个ftp服务器只需指定一个端口,但是连接上的可以有很多,
同一个道理吧
服务器端口是指定的。客户端可以通过这个端口和给定的服务器URL。来对其访问和操作。
学习中。不对的地方请指点~~
而client指定的端口,就是指服务器的端口,
服务器可以提供FTP,http等服务,每个服务的端口是不一样.
clietn可以通过FTP协议连上服务,也可以用HTTP协议访问服务器的WEB服务.
系统会随机选取一个端口进行和服务器通信,同时查找ip的服务器的port端口
服务器:ServerSocket serversocket = new ServerSocket(port);
服务器端临听本地port端口,等待连接!!
Socket client = serversocket.accept();
如果有客户连接,就创建一个Socket与客户Socket通信,ServerSocket并不写客户端的Socket直接通信!!
其实在有连接来的时候,服务器会开一个就的端口同时创建一个Socket与客户端通信!!!我有一张图,是描述这个的,是写在纸上的,不好发给你了!!
Socket cs = serversocket.accept();in this statement,what is the local port for cs?Is the same port for the serversocket?
I think so,but a document from IBM say is not.Please see:
"at that point ,accept() return a socket bounded to a randomly assigned port on the server...."
these words come from :http://www.cn-java.com/download/data/book/socket_tutorial.pdf,
you can find it in page 15.
please take a look at that location,then tell me whether you consider it right or wrong!
tks