Reputation: 7766
I have built a client-server program using C socket programming and its run perfectly on my Ubuntu OS which runs on a VMware . The only problem I have is with the listen API call .
Though i have set the connection limit to 2 , I can open four terminals and connect to the server at the same time .
listen (serverFd, 2); /* Maximum pending connection length */
The client and the server runs on the same computer .
This is the snippet of the code
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h> /* for sockaddr_un struct */
#define DEFAULT_PROTOCOL 0
#define BUFFER_SIZE 1024
/* POSIX renames "Unix domain" as "local IPC."
Not all systems define AF_LOCAL and PF_LOCAL (yet). */
#ifndef AF_LOCAL
#define AF_LOCAL AF_UNIX
#endif
#ifndef PF_LOCAL
#define PF_LOCAL PF_UNIX
#endif
/****************************************************************/
main ()
{
printf("Hello, server is starting...\n");
// initialize data from text file into system
readData();
printf("Country data loaded with total of %i countries available.\n", NoOfRecordsRead);
if(NoOfRecordsRead == 0)
{
printf("No valid data to serve, terminating application...\n");
exit (-1);
}
int serverFd, clientFd, serverLen, clientLen;
struct sockaddr_un serverAddress;/* Server address */
struct sockaddr_un clientAddress; /* Client address */
struct sockaddr* serverSockAddrPtr; /* Ptr to server address */
struct sockaddr* clientSockAddrPtr; /* Ptr to client address */
/* Ignore death-of-child signals to prevent zombies */
signal (SIGCHLD, SIG_IGN);
serverSockAddrPtr = (struct sockaddr*) &serverAddress;
serverLen = sizeof (serverAddress);
clientSockAddrPtr = (struct sockaddr*) &clientAddress;
clientLen = sizeof (clientAddress);
/* Create a socket, bidirectional, default protocol */
serverFd = socket (AF_LOCAL, SOCK_STREAM, DEFAULT_PROTOCOL);
serverAddress.sun_family = AF_LOCAL; /* Set domain type */
strcpy (serverAddress.sun_path, "country"); /* Set name */
unlink ("country"); /* Remove file if it already exists */
bind (serverFd, serverSockAddrPtr, serverLen); /* Create file */
listen (serverFd, 2); /* Maximum pending connection length */
printf("Server started.\n");
while (1) /* Loop forever */
{
/* Accept a client connection */
clientFd = accept (serverFd, clientSockAddrPtr, &clientLen);
if (fork () == 0) /* Create child to send recipe */
{
//do something
}
}
}
Why is this happening
Upvotes: 1
Views: 2072
Reputation: 13065
Though i have set the connection limit to 2
Nope. You've set the listen backlog to 2. Read the documentation on the listen command:
The backlog argument defines the maximum length to which the queue of pending connections for sockfd may grow. If a connection request arrives when the queue is full, the client may receive an error with an indication of ECONNREFUSED or, if the underlying protocol supports retransmission, the request may be ignored so that a later reattempt at connection succeeds.
Your program can decide not to call accept()
if you want to limit the number of simultaneous connections.
Upvotes: 5