Hello ,...
what happen if i set a queue amount of available connections (second parameter in listen()) and then create more connection :
If i run this program in the first time nothing wired happens (all goes as planed).
but if run this same program again im unable to connect (get "Connection refused" error) .
the client is telnet
Code:
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int main()
{
int sockfd , newfd; // The file discriptor
int fd[5] ;
struct sockaddr_in my_addr ;
struct sockaddr_in their_addr ;
int i = 0;
int sin_size;
sockfd = socket(PF_INET , SOCK_STREAM , 0);
/**
* 0 will make socket to choose the protocol tru the protocol
*/
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(12345);
my_addr.sin_addr.s_addr = INADDR_ANY;// connect on any ip ?
memset(my_addr.sin_zero, '\0',sizeof my_addr.sin_zero);
bind(sockfd , (struct sockaddr * )&my_addr , sizeof my_addr);//So bind was done ..
listen(sockfd , 2); // I will listn only for two connection but what if i connect more times ?
for ( i = 0 ; i < 5; i++)
{
fd[i] = accept(sockfd , (struct sockaddr *) &their_addr, &sin_size);
send(fd[i] , inet_ntoa(their_addr.sin_addr) , strlen ( inet_ntoa(their_addr.sin_addr) ) , 0); //Just send the connecter his ip.
}
close(fd[0]);
close(fd[1]);
close(fd[2]);
close(fd[3]);
close(fd[4]);
return 0;
return 0;
}