Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7179569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:09:59+00:00 2026-05-28T17:09:59+00:00

Client code: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h>

  • 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.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:10:00+00:00Added an answer on May 28, 2026 at 5:10 pm

    You’d obviously need to loop and fork then.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

folks please see following code : #include <gio/gio.h> #include <sys/socket.h> //socket(); #include <netinet/in.h> //sockaddr_in
In my socket code I have the following structure: Server: #include <stdio.h> #include <stdlib.h>
#include<stdlib.h> #include<stdio.h> #include<string.h> //This program is a sorting application that reads a sequence of
folks, here ma code : #include <glib.h> #include <gio/gio.h> // gio channel #include <sys/socket.h>
My client needs the non-compiled code. What's that? My project folder includes: Classes:of course,
Client code is pretty simple: <form action=DDServlet method=post> <input type=text name=customerText> <select id=customer> <option
I have some cross platform DNS client code that I use for doing end
After trying to understand why client code is not rendered in a page (injected
I have a year's experience writing client code but none with server stuff. I
I put a TCP server and client code in PIC controller. Now just wanted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.