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

  • SEARCH
  • Home
  • 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 7637481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:57:04+00:00 2026-05-31T07:57:04+00:00

I am trying to write a set of simple client/sever programs that make use

  • 0

I am trying to write a set of simple client/sever programs that make use of the poll() system call. Although my code compiles perfectly, while running the program, the client as well as the server display no output. Furthermore, the client keeps on taking input and never stops.
Here is the server code:

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

int main(int argc, char *argv[])
{
    struct pollfd fdarray[5];
    int sfd, port, nsfd, n, clen, ret, i;
    char str[100];
    struct sockaddr_in sadd, cadd;
    memset(str, 0, sizeof(str));
    if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("Unable to create socke\n");
        exit(1);
    }
    memset(&sadd, 0, sizeof(sadd));
    //port = atoi(argv[1]);
    sadd.sin_port = htons(9796);
    sadd.sin_family = AF_INET;
    sadd.sin_addr.s_addr = htonl(INADDR_LOOPBACK);

    if (bind(sfd, (struct sockaddr *) &sadd, sizeof(sadd)) < 0) {
        perror("Error binding to the socket\n");
        exit(1);
    }
    listen(sfd, 5);
    clen = sizeof(cadd);
    for (i = 0; i < 5; i++) {
        nsfd = accept(sfd, (struct sockaddr *) &cadd, &clen);
        if (nsfd < 0) {
            perror("Error accepting client\n");
            exit(1);
        }
        fdarray[i].fd = nsfd;
        fdarray[i].events = POLLIN;
        fdarray[i].revents = 0;
    }
    ret = poll(fdarray,5,10);
    for( i = 0; i < 5; i++) {
        if (fdarray[i].revents ==POLLIN) {
            n = read(fdarray[i].fd,str,100);
            if (n < 0)
                printf("error reading \n");
            printf("message is : %s \n", str);
            n = write(fdarray[i].fd, "message received...",
                  20);
        }
    }
    return 0;
}

Here is the client code

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

int main(int argc, char* argv[])
{   
    int sfd,i;
    char msg[MAXCOUNT];
    char blanmsg[MAXCOUNT];
    struct sockaddr_in saddr;

    memset(&saddr,0,sizeof(saddr)); 
    sfd = socket(AF_INET,SOCK_STREAM,0);
    saddr.sin_family = AF_INET;
    inet_pton(AF_INET,"127.0.0.1",&saddr.sin_addr);
    saddr.sin_port = htons(9796);

    connect(sfd,(struct sockaddr*) &saddr, sizeof(saddr));
    for(i = 0; i < 5; i++) {
        memset(msg,0,MAXCOUNT);
        memset(blanmsg,0,MAXCOUNT);
        fgets(msg,MAXCOUNT,stdin);
        send(sfd,msg,strlen(msg),0);
        recv(sfd,blanmsg,sizeof(blanmsg),0);
        printf("%s",blanmsg);
        fflush(stdout);
    }
    exit(0);
}

It would be really helpful if you can help me find out what is causing this behavior and how to stop this and run the program correctly.

  • 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-31T07:57:05+00:00Added an answer on May 31, 2026 at 7:57 am

    I don’t know if you realize it but your server basically does nothing until it accepts 5 client connections.

    for (i = 0; i < 5; i++)
    {
        nsfd = accept(sfd, (struct sockaddr *) & cadd, &clen);
    
        if (nsfd < 0)
        {
            perror("Error accepting client\n");
            exit(1);
        }
    
        fdarray[i].fd = nsfd;
        fdarray[i].events = POLLIN;
        fdarray[i].revents = 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm trying to write simple C openssl client and server. Here is client's code:
I'm trying to write an algorithm that will find the set of all vertices
I'm trying to write a shell script that, when run, will set some environment
I'm trying to use openmp to multithread a loop through std::set. When I write
Im trying to write a simple script that slides the next 'slide' div in
I am trying to write a simple if statement in javascript (jquery library) that
I'm trying to write a simple program that will compare the files in separate
Setup I am trying to write a really simple double entry accounting system for
I'm trying to write a simple game in Java that uses Processing to render
I am trying to write a simple console app that POSTs to a page

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.