#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Clients - Server ( UDP )


    Hello,

    I have a question: I want to create a n client to one server connection. This is the client-server algorithm:

    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<unistd.h>
    #include<error.h>
    #include<string.h>
    #include<stdlib.h>
    #include<signal.h>

    const int PORT_SERVER=9001;
    const int CLIENT_MAXIM=10;

    extern int errno;
    int ds;
    int dc;
    int nr=0;

    void semna(int nr_semnal)
    { if(nr_semnal == SIGCHLD)
    { wait(NULL);
    nr--;
    return; }
    }

    int e_prim(int i)
    { int k;
    for(k=2; k*k<=i;k++)
    if(i%k==0)
    return 0;
    return 1;
    }

    void client()
    { char buffer[100];
    char aux[100];
    int i,t;
    int numar, k;
    sprintf(aux, "Este clientul numarul: %d\n", nr);
    if(write(dc,aux,strlen(aux)) != strlen(aux))
    { shutdown(dc,2);
    exit(errno); }
    sprintf(aux, "Dati numarul:");
    if(write(dc,aux,strlen(aux)) != strlen(aux))
    { shutdown(dc,1);
    exit(errno); }
    bzero(buffer,100);

    if(read(dc,buffer,100) == 0)
    { shutdown(dc,2);
    exit(errno); }
    numar=atoi(buffer);
    for(k=2;k<numarl;k++)
    if(e_prim(k))
    { sprintf(aux, "Numar prim: %d\n",k);
    if(write(dc,aux,strlen(aux)) != strlen(aux))
    { shutdown(dc,2);
    exit(errno); }
    }
    shutdown(dc,2);
    exit(errno);
    }

    int main()
    { struct sockaddr_in server;
    if(signed(SIGCHLD, semnal) == SIG_ERR)
    { perror("signal()");
    exit(errno); }
    if(signal(SIGPIPE, SIG_IGN) == SIG_ERR)
    { perror("signal()");
    exit(errno); }
    if((ds = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    { perror("socket()");
    return errno; }
    bzero(&server,sizeof(server));
    server.sin_family=AF_INET;
    server.sin_port=htons(PORT_SERVER);
    server.sin_addr.s_addr=htonl(INADDR_ANY);

    if(bind(ds,&server,sizeof(server)) == -1)
    { perror("bind()");
    return errno; }
    if(listen(ds,5) == -1)
    { perror("listen()");
    return errno; }
    printf("Asteptam clientul la portul %d ... \n", PORT_SERVER);
    while(1)
    { dc=accept(ds,NULL,NULL);
    if(nr == CLIENTI_MAXIM)
    { shutdown(dc,2);
    continue; }
    switch(fork())
    { case 0:
    client();
    break;
    case -1:
    perror("fork()");
    break;
    default:
    break; }
    nr++; }
    }


    Enybody help to make the changes?
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,366
    Rep Power
    1870
    Also cross-posted here

    Read this

    Please edit your post and put [code][/code] tags around your code.
    Copy again from your code editor so indentation is preserved.
    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

IMN logo majestic logo threadwatch logo seochat tools logo