Hi,
I'm a bit of a newbie and in getting this far I have exhausted all the online socket tutorials I could find.
Here's my problem:
I have a server and a client. They connect happily. Once. To make them connect again I have to set them both to use a different port. How do I free up resources from the port the server is listening to? As far as my conceptual understanding goes, I want the server to accept the connection and shift it to a different port, so it can resume listening on the original port (while the client is connected to another) for the next potential connection.
I believe my problem lies somewhere in the following server connection code, but I'm clueless as to where. "man socket" suggests I need the socket() command to work differently somehow, but doesn't make much sense to me.
Note, #define is used to define PORTNUMBER and MAXPENDING.
Code:
int socketfd, clientlength;
struct sockaddr_in serv_addr, cli_addr;
socketfd = socket( PF_INET, SOCK_STREAM, 0);
if (socketfd < 0)
{
printf("\n\tERROR: Unable to open socket");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = PF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(PORTNUMBER);
if (bind(socketfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
printf("\n\tERROR: Unable to bind socket\n");
exit(0);
}
listen(socketfd,MAXPENDING);
clientlength = sizeof(cli_addr);
socketfd = accept(socketfd, (struct sockaddr *) &cli_addr, &clientlength);
if (socketfd < 0)
{
printf("\n\tERROR: Unable to accept connection\n");
exit(0);
}
printf("\n\tConnected.\n");
Any advice would be greatly appreciated.
Thanks.