I got a descriptor for a TCP socket in the following manner :
int desc = accept(socket_descriptor, &client_address, &len)
Now from this descriptor desc I want to get a file pointer. Can fdopen() be used here ?
The reason I want to get a file pointer is because I am making changes to an existing code that writes data to a local file. Now, I want to extend its functionality so that it can alternatively write to a TCP client. I dont want to rewrite all functions and was thinking of somehow being able to use the existing infrastructure. The existing functions use the file pointer to write to the file. I was wondering if it was possible to make the same function write to a TCP stream without making any changes.
Yes,
fdopen()is exactly what you need. Here is what man page is saying about it:But use it with caution when applying to socket descriptors. High-level I/O functions use buffering, and may send data differently (i.e. flush whenever
\nis found in the stream, insert\r) etc.