views:

72

answers:

1

Is it possible to have N file descriptors be seen to a program as 1 file descriptor such that data received on any of the N file descriptors (ie from N sockets) will be forwarded back to the calling API on the single file descriptor, hiding the fact that it may actually be coming from a different file descriptor? Is it also possible to have writes similarly be abstracted (but going back to the correct Nth file descriptor)?

+2  A: 

Since you tag and use the word socket, I presume that your data is coming over a network path and you actually want to read multiple sources over the same socket.

If you are working with TCP/IP sockets, you already have that scheme with UDP sockets listening on a local port to which multiple sources can send data.

You cannot have such a TCP socket, however the select API is available on all standard implementations to let you open multiple TCP listening sockets, one per source, and then do a select on the whole lot. You will not be able to 'hide' the source here.

If abstraction is what you are after, a better idea would be to write a small application that will manage this multiple communication end-points and talk with your primary application over IPC. You can implement a short-header to address end-points to this small application. Your primary application will then see everything over one communication point.

It will also solve your problem of abstracting the writes nicely.

nik