0

This is a Simple Code that I have Written. Server responds to CONNECTIONS ON PORT 2923, 2924 and 2925.

When I run the programme , the serve only accpets CONNECTION from PORT 2923. Can someone HELP me out. Thanku

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h>

void handle_client(int cliend_fd)
{

  int ret = accept(cliend_fd, NULL, 0);
  if (ret < 0)
  {
    printf("Accept Error\n");
  }

  else
  {
    printf("Client Accepted\n");
    shutdown(ret, 2);
  }
}

int main()
{
  int count = 3;
  int PORT = 2923;

  struct sockaddr_in address;
  int MasterSocket[count];
  int i = 0;
  fd_set readfds;

  int maxfd;
  maxfd = -1;
  int SelectSession;

  struct timespec TimeOut;
  TimeOut.tv_sec = 2;
  TimeOut.tv_nsec = 2;

  for (i = 0; i < count; i++)
  {
    MasterSocket[i] = socket(AF_INET, SOCK_STREAM, 0);
  }

  for (i = 0; i < count; i++)
  {
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr("127.0.0.1");
    address.sin_port = PORT + i;
    if (bind(MasterSocket[i], (struct sockaddr *) &address, sizeof(address))
        < 0)
    {
      perror("Bind\n");
      getchar();
    }

    printf("SockerDesriptor %d---bind %d\n", MasterSocket[i], PORT + i);
  }

  for (i = 0; i < count; i++)
  {
    if (listen(MasterSocket[i], 4) < 0)
    {
      perror("Listen");
      getchar();
      //return 1;
    }
    else
    {
      printf("Listening on Port %d---\n", PORT + i);
    }
  }

  while (1)
  {
    FD_ZERO(&readfds);
    int status;

    for (i = 0; i < count; i++)
    {
      FD_SET(MasterSocket[i], &readfds);
      if (MasterSocket[i] > maxfd)
      {
        maxfd = MasterSocket[i];
      }
      printf("%d Added to FD_SET Descriptor %d \n\n", PORT + i,
          MasterSocket[i]);
    }

    //status = 0;
    printf("############Waiting for Connection\n");
    status = pselect(maxfd + 1, &readfds, NULL, NULL, &TimeOut, NULL );
    if (status < 0)
    {
      perror("Status");
      getchar();
      return 1;
    }

    else if (status == 0)
    {
      printf("TimeOut occured\n");
    }

    else
    {
      //printf("Status %d\n", status);
      SelectSession = -1;
      for (i = 0; i < count; i++)
      {
        //printf("Checking Set %d\n", i);
        if (FD_ISSET(MasterSocket[i], &readfds))
        {
          //printf("Matching Set %d\n", i);
          SelectSession = MasterSocket[i];
          printf("SelectSession %d\n", MasterSocket[i]);
          if (SelectSession == -1)
          {
            //shutdown (MasterSocket[i], 2);
            //continue;
            break;
          }
          else
          {
            printf("In Handle\n");
            handle_client(SelectSession);
            getchar();
          }

        }
        else
        {
          printf("Not in FD_ISSET\n");

        }
      }
    }

    /*for (i=0; i<count; i++)
     {
     shutdown (MasterSocket[i], 2);
     }
     */
  }

  return 0;
}
5
  • 1
    Do you get any errors for bind(), listen(), pselect() and/or accept() during execution of the program? Commented Jan 21, 2014 at 11:44
  • Does the code compile without warning? Commented Jan 21, 2014 at 11:52
  • Here the code compiled cleanly after having done #define _POSIX_C_SOURCE 200112L to pull in the prototype for pselect() and works as expected. Commented Jan 21, 2014 at 11:56
  • What do you use to connect to the server? Commented Jan 21, 2014 at 11:57
  • Used a client program. Connect on ports. Only accepting on 2923 Commented Jan 21, 2014 at 12:03

2 Answers 2

2

This line

 address.sin_port = PORT + i;

should be

 address.sin_port = htons(PORT + i);

to make sure the port is stored in network byte-order.


Also you should close() a socket descriptor after having shut it down, if you do not need it anymore, to free the system resources associatee with it.

Sign up to request clarification or add additional context in comments.

Comments

1

Remove the getchar() from while loop.

After pselect server waiting to get a character, so it is not allowing connect anymore client.

Or you have to enter a character in server after every client connected

And change port number to network byte order using htons

Program:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h>

void handle_client (int cliend_fd)
{
    int ret = accept(cliend_fd, NULL, 0);
    if ( ret < 0 )
        printf("Accept Error\n");
    else {
        printf ("Client Accepted\n");
        shutdown (ret, 2);
    }
    return;
}

int main()
{
    int count = 3, PORT = 8000, opt = 1, i = 0;
    int MasterSocket[count];
    int maxfd = -1, SelectSession;
    fd_set readfds;
    struct sockaddr_in address;

    struct timespec TimeOut;
    TimeOut.tv_sec = 2;
        TimeOut.tv_nsec = 2;

    for(i=0; i<count; i++)
    {
        MasterSocket[i] = socket(AF_INET , SOCK_STREAM , 0);
        printf("socket created : %d\n", MasterSocket[i]);
    }

    for(i=0; i<count; i++)
    {
        address.sin_family = AF_INET;
        address.sin_addr.s_addr = inet_addr("192.168.1.4");
        address.sin_port = htons(PORT+i);
        if( setsockopt(MasterSocket[i], SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)) < 0 ) {    
            perror("setsockopt1");
            return -1;
        }
        if (bind(MasterSocket[i], (struct sockaddr *)&address, sizeof(address)) < 0) {
            perror ("Bind\n");
            return -1;
        }
        printf("SockerDesriptor %d---bind %d\n", MasterSocket[i], PORT+i);
    }

    for(i=0; i<count;i++)
    {
        if (listen(MasterSocket[i], 4) < 0) {
                perror ("Listen\n");
                return -1;
        }
        else
            printf("Listening on Port %d---\n", PORT+i);    
    }

    while(1)
    {   
        FD_ZERO(&readfds);
        int status;
        for (i = 0; i < count; i++)
        {
            FD_SET(MasterSocket[i], &readfds);
            if (MasterSocket[i] > maxfd)
                maxfd = MasterSocket[i];
        }

        status = pselect(maxfd + 1, &readfds, NULL, NULL, &TimeOut, NULL );
        if(status < 0)
            perror("Status");
        /* else if(status == 0)
            printf("TimeOut occured\n"); */
        else if(status > 0) {
            for (i = 0; i < count; i++)
            {
                if (FD_ISSET(MasterSocket[i], &readfds)) {
                    SelectSession = MasterSocket[i];
                    printf("SelectSession %d\n", MasterSocket[i]);
                    handle_client(SelectSession);
                    //getchar();
                }
            }
        }
    }
    return 0;
}       

1 Comment

Working ... thanks for the help \m/. Yea I looked up SO_REUSEADDR and getchar(). I overlooked a small detail. I had called 'pselect' with count +1 other than maxfd+1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.