views:

385

answers:

3

I'm trying to run my server program but I keep getting this error:

ERROR on binding: Address already in use

  int main(int argc, char *argv[]){

        if (argc < 6){
          printf("usage: stringSearcher <filename> <stringLength>  <searchLength> <nChildren> <nThreads> <blockSize>\n");
          exit(0);
        }

        char* fileName = argv[1];
        int stringLen = atoi(argv[2]);
        int searchLen = atoi(argv[3]);
        nChildren = atoi(argv[4]);
        if (nChildren == 0)
          nChildren = 1;
        int nThreads = atoi(argv[5]);
        int blockSize = atoi(argv[6]);
        int childNo = 0;

    //Opens new socket
      int sockfd;
      sockfd = socket(AF_INET, SOCK_STREAM, 0);

      if (sockfd < 0){
        perror("ERROR opening socket");
        exit(0);
      }



  int  newsockfd, portno, clilen;
  struct sockaddr_in serv_addr, cli_addr;
  int n;

  portno = 5000;
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = INADDR_ANY;
  serv_addr.sin_port = htons(portno);

  int opt = 1;
  setsockopt(1, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

  if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){
    perror("ERROR on binding\n");
   exit(1);
  }
A: 

Have you tried changing portno to a different number or making it a short? I think its a short not an int.

Never mind on the short part.

http://linux.die.net/man/3/htons

Also, is the binding happening in a thread? You cant really do that with out a different port number.

Daniel A. White
+6  A: 

setsockopt(1, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

1st argument must be sockfd and not '1'

maykeye
A: 

an excellent tool for winsock development is TCPView. it makes it simple to see which process is listening on which port, socket status, etc.

geva30