0

Client code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 

    void error(const char *msg)
    {
            perror(msg);
            exit(0);
    }


    int main(int argc, char *argv[])
    {
        int sockfd, portno, n,choice;
        struct sockaddr_in serv_addr;
        struct hostent *server;

                int buffer;

        if (argc < 3) 
        {
                fprintf(stderr,"usage %s hostname port\n", argv[0]);
                exit(0);
            }

        portno = atoi(argv[2]);
        sockfd = socket(AF_INET, SOCK_STREAM, 0);

        if (sockfd < 0) 
                error("ERROR opening socket");
                server = gethostbyname(argv[1]);

        if (server == NULL) 
        {
                fprintf(stderr,"ERROR, no such host\n");
                exit(0);
            }

            serv_addr.sin_family = AF_INET;
            bcopy((char *)server->h_addr, 
            (char *)&serv_addr.sin_addr.s_addr,server->h_length);
            serv_addr.sin_port = htons(portno);

        if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
                error("ERROR connecting");

        //printf("Please enter positive integer ");

        printf("Enter your choice\n1=Prime number\n2=Fibonacci number\n 3=power of 2\n");
        scanf("%d",&choice);

        if(choice==1){
        printf("Please enter positive integer ");
        scanf("%d", &buffer);
        }

            n = write(sockfd,&buffer,sizeof(buffer));

        if (n < 0) 
            error("ERROR writing to socket");
        char msg[256];
        bzero(msg, 256);

        n=read(sockfd, msg, 255);

        printf("%d %s\n",buffer, msg);

        close(sockfd);
            return 0;
}

Server code:

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

    void error(const char *msg)
    {
            perror(msg);
            exit(1);
    }

    int prime(int num)
    {
        int c;

        for ( c = 2 ; c <= num - 1 ; c++ )
        {
                if ( num%c == 0 )
                {
                    return 0;
                }
        }

        if ( c == num )
            return 1;

        return 0; 

    }


    int main(int argc, char *argv[])
    {
        int sockfd, newsockfd, portno;
            socklen_t clilen;
            int buffer;
            struct sockaddr_in serv_addr, cli_addr;
            int n;
        int i=1;

        if (argc < 2) 
        {
                fprintf(stderr,"ERROR, no port provided\n");
                exit(1);
            }

        sockfd = socket(AF_INET, SOCK_STREAM, 0);

        if (sockfd < 0) 
                error("ERROR opening socket");

            portno = atoi(argv[1]);
            serv_addr.sin_family = AF_INET;
            serv_addr.sin_addr.s_addr = INADDR_ANY;
            serv_addr.sin_port = htons(portno);

        if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
                    error("ERROR on binding");

        listen(sockfd,5);
            clilen = sizeof(cli_addr);
            newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr,&clilen);

            if (newsockfd < 0) 
                error("ERROR on accept");



            n = read(newsockfd,&buffer,sizeof(buffer));

        int result= prime(buffer);

        if (n < 0) error("ERROR reading from socket");


            printf("Client's input is: %d\n",buffer);

        if(result==1)
        {

            n = write(newsockfd,"is Prime Number",18);
                if (n < 0) error("ERROR writing to socket");
        }

        else 
        {
            n = write(newsockfd,"is not Prime Number",18);
                if (n < 0) error("ERROR writing to socket");
        }





            close(newsockfd);
            close(sockfd);
            return 0; 
    }

In this client-server process, server program is terminated after first execution. I want to:

  1. Server will run infinitely
  2. If I keep client program in more than one pc, each client will be able to communicate and server program will not be terminated.
Jesi
  • 205
  • 1
  • 5
  • 9

4 Answers4

2

You'd obviously need to loop and fork then.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
0

An infinite loop around the accept/read/write/close would keep your server running.

As for concurrently accepting multiple connections, you have the choice of creating a new process or thread after accept. Another is making the sockets non-blocking (Google is your friend!) and using polling functions like select or poll.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

See my implementation of this task: https://github.com/H2CO3/TCPHelper

The basic trick is that you'll need to close your accept() into an infinite loop, and fork() or create a new thread after ever accepted conection.

if you don't want to use my helper functions, you'll see some pretty good tutorials here: http://www.linuxhowtos.org/C_C++/socket.htm

0

on server side

   while (1){

        listen(sockfd,5);
            clilen = sizeof(cli_addr);
            newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr,&clilen);

            if (newsockfd < 0) 
                error("ERROR on accept");



            n = read(newsockfd,&buffer,sizeof(buffer));

        int result= prime(buffer);

        if (n < 0) error("ERROR reading from socket");


            printf("Client's input is: %d\n",buffer);

        if(result==1)
        {

            n = write(newsockfd,"is Prime Number",18);
                if (n < 0) error("ERROR writing to socket");
        }

        else 
        {
            n = write(newsockfd,"is not Prime Number",18);
                if (n < 0) error("ERROR writing to socket");
        }


    }

on client side :

    //printf("Please enter positive integer ");
    while (choice != -1){
        printf("Enter your choice\n1=Prime number\n2=Fibonacci number\n 3=power of 2\n");
        fflush(stdin);
        scanf("%d",&choice);

        if(choice==1){
        printf("Please enter positive integer ");
        scanf("%d", &buffer);
        }

        n = write(sockfd,&buffer,sizeof(buffer));

        if (n < 0) 
            error("ERROR writing to socket");
        char msg[256];
        bzero(msg, 256);

        n=read(sockfd, msg, 255);

        printf("%d %s\n",buffer, msg);
    }// end of while (choice != -1){

On the server side, you need create a process and maybe you can implement a process synchronization system. Critical section, locking resources etc.

hkulekci
  • 1,894
  • 15
  • 27