Reputation: 3836
All, winter comes, plz keep warm and keep healthy. During the meditation about the work, I got some question about the function of fd dup2 . I create a socket server, and a client. the server send, the client receive data. But Now I want to dup2 the server socket fd to a file df in order to let the client read data directly from a file located in server. I write like
while(socketdf = accept(...)) { dup2(filefd , socketfd); }
However, it doesnot work is this possible? Can you give me any advice on this? Thanx
Upvotes: 2
Views: 2444
Reputation:
dup2()
doesn't work like that -- what you're ending up doing here is closing socketfd
and replacing it with a copy of filefd
.
There is no way to directly plug a socket into a file like what you're trying to do here -- you will need to "pump" data from the file to the socket in your application. The sendfile()
system call will simplify things considerably, though.
Upvotes: 4