StevenWang
StevenWang

Reputation: 3836

dup2 a socket to a file

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

Answers (1)

user149341
user149341

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

Related Questions