请教TCP/IP高手[问题]

fulpchcl

知名会员
注册
2002-04-04
消息
492
荣誉分数
1
声望点数
128
虽然试早考完,已经在家待业。可这个问题还没弄清楚,在此请教各位。

按照我的理解,如果一个port被一个process用了,别的process就不能用这个
port了。可为什么FTP Server能和几个clients同时get/put file 或 check
the content of a directory?

如果FTP Server是一个process在running(我认为不象,一般是spawn a
process when a connection request is coming),这就是说一个port
可以和几个connection communication?(不象)。

或者对于低于1024的reserved ports 可以例外?

谢谢 :thanks: :thanks: :thanks:
 
最初由 fulpchcl 发布
虽然试早考完,已经在家待业。可这个问题还没弄清楚,在此请教各位。

按照我的理解,如果一个port被一个process用了,别的process就不能用这个
port了。可为什么FTP Server能和几个clients同时get/put file 或 check
the content of a directory?

如果FTP Server是一个process在running(我认为不象,一般是spawn a
process when a connection request is coming),这就是说一个port
可以和几个connection communication?(不象)。

或者对于低于1024的reserved ports 可以例外?

谢谢 :thanks: :thanks: :thanks:

"如果一个port被一个process用了,别的process就不能用这个port了。"
此话大错.

找任何一本关于TCP/IP programming的书,参考Concurrent Server的例子,一看就明白了.
 
你任何一台ftp server上,用netstat看看你就知道了。
 
最初由 chengchy 发布
你任何一台ftp server上,用netstat看看你就知道了。

呵呵,同业,坚守岗位呐,还是在家歇着?
 
最初由 VeryIratedPerson 发布


呵呵,同业,坚守岗位呐,还是在家歇着?

哎,不是同业,对这类东西也只懂皮毛。现在就凭这些皮毛混日子吃饭。以前研究过,TCP/IP上算半路出家吧
 
"如果一个port被一个process用了,别的process就不能用这个port了。"

Actually it is quite right. The reason that the FTP server or any kind
of TCP type concurrent servers can handle multiple processes is that the main processor
will spawn a child thread to handle the connection with a cloned port. Then all
the packets will be handled by the child task and the cloned port. The cloned port
is different from the service port. After the child thread is created, the major thread
will continue to wait on the service port. For UDP based service, there's no connection.
the server can handle multiple requests from different clients with one thread and
one service port.
 
最初由 northernwolf 发布
"如果一个port被一个process用了,别的process就不能用这个port了。"

Actually it is quite right. The reason that the FTP server or any kind
of TCP type concurrent servers can handle multiple processes is ...
Presumably FTP Server is a concurent server, that's why a lots of clients can connect to one FTP server. However data-connection in FTP is different, it is a "client"-like connection -- the server socket is in FTP client side.
Use netstat or TCPview application we can see on FTP Server, there are serveral data-connection ports(20) are opened when get/put FTP commands are excuting(big enough files).
That is the point that confused me.

Thanks
 
先把什么是 process 和什么是 Thread 搞清楚再说。 一个
Port当然只能被一个 Process 占用,但是一个 Process
可以创建多个 Thread 来处理多个用户的请求。现在的
Server 都是这样做的。Thread 的创建方式可以是
Thread per request, Thread per session, or Thread Pool.
你在此引用的 "spawn a process when a connection request
is coming" 实际上只的是一个 Thread, not a Process. 你的
参考书不好。 建议你读一读 Stevens 的Unix 网络编程。
自己动手写个小server程序。一个小时后你就明白了。

最初由 fulpchcl 发布
虽然试早考完,已经在家待业。可这个问题还没弄清楚,在此请教各位。

按照我的理解,如果一个port被一个process用了,别的process就不能用这个
port了。可为什么FTP Server能和几个clients同时get/put file 或 check
the content of a directory?

如果FTP Server是一个process在running(我认为不象,一般是spawn a
process when a connection request is coming),这就是说一个port
可以和几个connection communication?(不象)。

或者对于低于1024的reserved ports 可以例外?

谢谢 :thanks: :thanks: :thanks:
 
Re: Re: 请教TCP/IP高手[问题]

最初由 Nirvana 发布
先把什么是 process 和什么是 Thread 搞清楚再说。 一个
Port当然只能被一个 Process 占用,但是一个 Process
可以创建多个 Thread 来处理多个用户的请求。现在的
Server 都是这样做的。Thread 的创建方式可以是
Thread per request, Thread per session, or Thread Pool.
你在此引用的 "spawn a process when a connection request
is coming" 实际上只的是一个 Thread, not a Process. 你的
参考书不好。 建议你读一读 Stevens 的Unix 网络编程。
自己动手写个小server程序。一个小时后你就明白了。


您这段话里唯一正确的就是提到了Richard Stevens的参考书.

Concurrent server与thread没有关系,早期的UNIX没有thread,同样可以在上面写出concurrent.

请参阅UNIX Network Programming (Richard Stevens) Vol 1, Page 104, section 4.8 "Concurrent server"

我还是辛苦一下,将code sample抄在这里:

listenfd = socket(...);

bind(listenfd, ...);
listen(listenfd, ...);

for (;;) {
connfd = accept(listenfd, ...);

if ((pid = fork()) == 0) {
close(listenfd); /*child process closes listenfd socket */
doit(connfd);
close(connfd);
exit(0);
}

close(connfd); /*parent process closes connected socket*/
}

其实,process都不是直接对port进行操作,而是通过listen / connect返回的reference count, which is maintained by file table entry,就象普通文件的file handler一样.

fork之后,child process得到duplicated file handlers,所以,它首先要close listenfd,好让server能应其他的request...
同理,parent process 需要close connfd,因为child process保留了connfd的copy.

太累了,请参考page 105 ~ 106,特别是106的图,很清楚.

在某一特定时刻,当然只有一个process在使用port,但是在一段时间内,可以有多个process共享同一个port,至于其中的multiplexing是如何实现的,TCP layer不需要考虑,那是更低一个layer的问题.
 
最初由 fulpchcl 发布

或者对于低于1024的reserved ports 可以例外?

谢谢 :thanks: :thanks: :thanks:

reserved ports的用途完全是约定俗成,从更低的layer来看,他们与>1024的PORT没有本质区别.
 
最初由 Nirvana 发布
先把什么是 process 和什么是 Thread 搞清楚再说。 一个
Port当然只能被一个 Process 占用。。。
看来大家还没有把问题的关键看出来,可能是我没说清楚。
A FTP Server is a server, 它是用 port 21 一直等待(accept)
a client's connection request, once it is coming,
a new socket is created with a differnet port(let's say 3100),
then FTP Server either spawn a process or launch a thread
(it does not matter) to handle this particular client's
following FTP requests ON the new port(3100).
And FTP Server still goes back to accept to wait for another
FTP client's request.
My question begins here:
In the new process/thread, according to FTP command from client,
sometimes another connection will be established(for example, put, get a file), this connection(on FTP server machine, called
data-connection in FTP protocol) must use port 20(this is FTP protocol specification). FTP server uses port 20 and FTP client
uses whatever port to establsh a conncction and transfer file.
If several clients want to transfer huge files, what will happend?
If one port(20) only can be used by one process, only one FTP client is able to transfer file, and other clients have to wait.
But in real world, we do not need to wait, several clients
get the files at the same time.
How can we explain this phenomena ?

Thanks,
 
楼楼上的已经说得听清楚的了,强烈推荐:

UNIX Network Programming Vol 1 - Richard Stevens
Page 104, section 4.8 "Concurrent server"
 
well, do not get confused. the port is just a BSD term to simplify the programming model, it never refers to the physical port which can not be concurrent. It by no means that can only be owned by one process.

The model does not deal with thread/process at all. The windows async socket model provides a way that a thread handles multiple ports, listening, and accepting operations.
 
Actually fulpchcl's question is a good one. Here's the answer from "Internetworking with TCP/IP" by Douglas E. Comer.
"A server that uses only one protocol port can accept connections from many clients because TCP uses both endpoints to identify a connection" [End Quote].

This means the cloned port number for the data transfer can be 20 for all clients. The server distinguish the socket pair by the client's port number (data transfer, not the control connection port). This is what you have seen from your machine. Each connection will be handled by a different process/or thread.

The FTP control connection, as you pointed, is still procesing the same way as many other TCP concurent servers.
 
后退
顶部