Thread: Server and client stuck in 1 clients

  1. #1
    Registered User
    Join Date
    Aug 2019
    Posts
    9

    Server and client stuck in 1 clients

    Hello,

    I'm trying to create a serer and clients. but then i added more that 1 client the server does not know with one client to send back to !

    I think it's on
    send(newSocket, name, strlen(name), 0);



    all what i want to server send back to the client, but not the same client.
    different if there are 2 client

    Server:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<unistd.h>
    #include<sys/socket.h>
    #include<sys/types.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<inttypes.h>
    
    #definePORT4444
    
    intfindIndex(int *array, size_tsize, inttarget)
    {
      int i = 0;
      while ((i < size) && (array != target))
        i++;
    
      return (i < size) ? (i) : (-1);
    }
    
    intmain()
    {
      int countclients = 0;
      constchar *clients[] = { };
      constchar *checkclients[] = { };
    
      int sockfd, ret;
      struct sockaddr_in serverAddr;
    
      int newSocket;
      struct sockaddr_in newAddr;
    
      charname[1024];
      charage[1024];
      charholdname[1024];
      pid_t childpid;
    
      sockfd = socket(AF_INET, SOCK_STREAM, 0);
      if (sockfd < 0) {
        printf("[-]Error in connection. \n");
        exit(1);
      }
      printf("[+]Server Socket is created. \n");
    
      memset(&serverAddr, '\0', sizeof(serverAddr));
      serverAddr.sin_family = AF_INET;
      serverAddr.sin_port = htons(PORT);
      serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    
      ret = bind(sockfd, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
      if (ret < 0) {
        printf("[-]Error in binding");
        exit(1);
      }
      printf("[+]Bind port to %d\n", PORT);
    
      if (listen(sockfd, 10) == 0) {
        printf("Listening....\n");
      } else {
        printf("[-]Error in binding. \n");
      }
    
      while (1) {
        socklen_t newAddrLength = sizeof(newAddr);
        newSocket = accept(sockfd, (struct sockaddr *) &newAddr, &newAddrLength);
        if (newSocket < 0) {
          exit(1);
        }
        // printf("%d", newSocket[0]);
    
        printf("login to server: \n");
    
        printf("Connection accepted from %s:%" PRIu16 "\n",
               inet_ntoa(newAddr.sin_addr), ntohs(newAddr.sin_port));
    
        if ((childpid = fork()) == 0) {
          printf("check childpid: \n");
    
          close(sockfd);
    
          while (1) {
            printf("check childpid inside while: \n");
    
            recv(newSocket, name, 1024, 0);
            recv(newSocket, age, 1024, 0);
            if (strcmp(name, ":exit") == 0) {
              printf("disconnection from %s:%" PRIu16 "\n",
                     inet_ntoa(newAddr.sin_addr), ntohs(newAddr.sin_port));
            } else {
              printf("send and recive code: \n");
    
              charnameinserver[1024];
              charageinserver[1024];
    
              for (int i = 0; i <= countclients; i++) {
                printf("works \n");
    
                charconnectclient[1024];
                sprintf(connectclient, "%s:%d", inet_ntoa(newAddr.sin_addr),
                        ntohs(newAddr.sin_port));
                clients = connectclient;
                charcheckclient[1024];
                sprintf(checkclient, "%s:%d", inet_ntoa(newAddr.sin_addr),
                        ntohs(newAddr.sin_port));
    
                if (strcmp(checkclient, clients) == 0) {
                  checkclients = checkclient;
                  sprintf(nameinserver, "%s Name: %s", checkclients, name);
                  sprintf(ageinserver, "%s Age: %s", checkclients, age);
                } else {
                  countclients++;
                  charconnectclient[1024];
                  sprintf(connectclient, "%s:%d", inet_ntoa(newAddr.sin_addr),
                          ntohs(newAddr.sin_port));
                  clients[i + 1] = connectclient;
                  charcheckclient[1024];
                  sprintf(checkclient, "%s:%d", inet_ntoa(newAddr.sin_addr),
                          ntohs(newAddr.sin_port));
                  sprintf(nameinserver, "%s Name: %s", checkclients[i + 1], name);
                  sprintf(ageinserver, "%s Age: %s", checkclients[i + 1], age);
                }
              }
    
              printf("%s\n", nameinserver);
              printf("%s\n", ageinserver);
              send(newSocket, name, strlen(name), 0);
              send(newSocket, age, strlen(age), 0);
              bzero(name, sizeof(name));
              bzero(age, sizeof(age));
              // break;
            }
          }
        }
      }
      printf("close \n");
      close(newSocket);
    
      return0;
    }
    Client:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<unistd.h>
    #include<sys/socket.h>
    #include<sys/types.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    
    #definePORT4444
    
    intmain()
    {
      int clientSocket, ret;
      struct sockaddr_in serverAddr;
      charname[1024];
      charage[1024];
    
      clientSocket = socket(AF_INET, SOCK_STREAM, 0);
      if (clientSocket < 0) {
        printf("[-]Error in cannection. \n");
        exit(1);
      }
      printf("[+]Client Spcket is created \n");
    
      memset(&serverAddr, '\0', sizeof(serverAddr));
      serverAddr.sin_family = AF_INET;
      serverAddr.sin_port = htons(PORT);
      serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    
      ret =
          connect(clientSocket, (struct sockaddr *) &serverAddr,
                  sizeof(serverAddr));
      if (ret < 0) {
        printf("[-]Error in conncation");
        exit(1);
      }
    
      while (1) {
        printf("Client Name: ");
        scanf("%s", &name[0]);
        printf("Client Age: ");
        scanf("%s", &age[0]);
        send(clientSocket, name, strlen(name), 0);
        send(clientSocket, age, strlen(age), 0);
    
        if (strcmp(name, ":exit") == 0 || strcmp(age, ":exit") == 0) {
          close(clientSocket);
          printf("[-]Disconnected from server");
          exit(1);
        }
    
        if (recv(clientSocket, name, 1024, 0) < 0
            || recv(clientSocket, age, 1024, 0) < 0) {
          printf("[-]Error in receivng data \n");
        } else {
          printf("Server: \t%s\n", name);
          printf("Server: \t%s\n", age);
        }
      }
    
      return0;
    }
    Thanks for helping me
    Last edited by Salem; 08-14-2019 at 09:14 AM. Reason: Removed crayola

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,534
    Perhaps you could repost your code using copy-as-text and/or paste-as-text.

    The ridiculous colour scheme from your IDE and browser combo was unreadable, and I don't feel like doing any more work to patch it up.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-16-2018, 08:30 AM
  2. Replies: 6
    Last Post: 05-07-2011, 08:44 AM
  3. Client/server problem; server either stops receiving data or client stops sending
    By robot-ic in forum Networking/Device Communication
    Replies: 10
    Last Post: 02-16-2009, 11:45 AM
  4. Server - All Clients Send.
    By mill1k in forum Networking/Device Communication
    Replies: 13
    Last Post: 09-30-2004, 12:23 PM
  5. server with multiple clients
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-23-2002, 09:15 PM

Tags for this Thread