Reputation: 1753
I am following the socket programming tutorial from this link Socket programming. and trying to develop an application where client will receive Network Time from Server and print the time . Here is how the program works-
Server is waiting for incoming connection and while client try to connect with server , server accept the connection and server sends the time to client. After receiving through socket client print the time.
After every message send to client , server close the connection and wait for next. Client is able to communicate with server for 1020 times. If I try to communicate more than 1020 time from client to server then I got Error- Unable to Create Socket
I am trying to develop the application where client can communicate with server infinite times or atleast 20000 times.
I am suspecting , may be memory is not freed while creating socket in client side.
Sample output :
counter=1017 Sun Jul 6 03:19:48 2014
counter=1018 Sun Jul 6 03:19:48 2014
counter=1019 Sun Jul 6 03:19:48 2014
counter=1020 Sun Jul 6 03:19:48 2014
Error : Could not create socket (WHY ??)
Here is my code
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
int main(int argc, char *argv[])
{
int listenfd = 0, connfd = 0;
struct sockaddr_in serv_addr;
char sendBuff[1025];
time_t ticks;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&serv_addr, '0', sizeof(serv_addr));
memset(sendBuff, '0', sizeof(sendBuff));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
listen(listenfd, 10);
while(1)
{
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
ticks = time(NULL);
snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&ticks));
write(connfd, sendBuff, strlen(sendBuff));
close(connfd);
// sleep(1);
}
}
Client.c
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
int sockfd = 0, n = 0;
char recvBuff[1024];
struct sockaddr_in serv_addr;
if(argc != 2)
{
printf("\n Usage: %s <ip of server> \n",argv[0]);
return 1;
}
int counter=0;
while(counter <2000)
{
memset(recvBuff, '0',sizeof(recvBuff));
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Error : Could not create socket \n");
return 1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(5000);
if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0)
{
printf("\n inet_pton error occured\n");
return 1;
}
if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\n Error : Connect Failed \n");
return 1;
}
while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)
{
recvBuff[n] = 0;
printf("counter=%d ",counter);
fputs(recvBuff, stdout);
/*
if(fputs(recvBuff, stdout) == EOF)
{
printf("\n Error : Fputs error\n");
}
*/
}
counter++;
}
if(n < 0)
{
printf("\n Read error \n");
}
return 0;
}
I am using gcc under Ubuntu. Any help to understand the reason and resolve and run the program for around 20000 will be highly appreciated. Thanks in advance.
Upvotes: 2
Views: 2154
Reputation: 375
I have the same problem.
In my situation, after fopen("/proc/net/route" , "r");
, I forgot to fclose()
.
After fopen("/proc/net/route" , "r");
, I found the socket number increased, so I think this is the same problem.
Upvotes: 0
Reputation: 445
Since you are using the same client for connecting to the server , you dont need to start all over again the socket & connect process , You Can block the client till you get some data from server. Otherwise , you are unnecessary wasting lot of kernel resources and you are closing and opening at a very rapid phase too , which is not a good idea !
Upvotes: 0
Reputation: 6214
You appear to be leaking a socket in your client code. You're creating sockets inside your outer loop, but you never close them. Try adding close(sockfd);
to the end of your outer loop, and to all points where you break or return out of it.
Upvotes: 2
Reputation: 7555
You are not closing your client's socket after using it. You need this line in your client code:
close(sockfd);
After your while loop.
Upvotes: 4