I’m creating a chat server which accepts both TCP and UDP connections. Assume for now that the server only allows a single client to connect; there’s nobody to chat with yet.
But how do I do that?
int sock = socket( PF_INET, SOCK_STREAM, 0 );
As I understand it, the essential difference in setup is this–
int sock = socket( PF_INET, SOCK_DGRAM, 0 );
But how do I do both simultaneously? Set up two ports and alternate listening on both for connections?
Have a look at the select() function. It allows ‘watching’ multiple file descriptors. Hint: UDP does not have connections, so you don’t have a ‘listener’ socket. For TCP, you open a listener socket, on which connections can be accepted. You can use select() to watch the ‘listen’ socket.