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 115021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:57:11+00:00 2026-05-11T02:57:11+00:00

The program is supposed to accept telnet connections on port 8888 and then send

  • 0

The program is supposed to accept telnet connections on port 8888 and then send and messages from each telnet client using poll(), send() and recv() but it doesn’t quite work 100%. It seems certain connections can always send messages to anyone and the program works fine but there is always at least one client who cannot send messages. All clients can always receive (poll doesn’t register incoming data).

This code runs on it’s own so if you put it in a file and compile it with gcc -o app filename.c then you can telnet to localhost on port 8888 and see it not working. This code was written for Fedora but shouldn’t have anything non-Linux specific in it. Any help would really be appreciated.

#include <stdio.h> #include <stdlib.h> #include <poll.h> #include <unistd.h> #include <sys/time.h> #include <sys/types.h> #include <string.h> #include <signal.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <errno.h>  #define PORT 8888 #define MAX_CONN 10 #define SECOND 1000 #define TIMEOUT (30 * SECOND)  static int listen_socket();  int main(int argc, char **argv) {     struct pollfd **my_fds;                  //array of pollfd structures for poll()     struct pollfd *curr, *new_conn;          //so I can loop through     int num_fds;                             //count of how many are being used     int i, j;                                //for loops     char buff[255], buff2[255];              //for sending and recieving text     struct sockaddr_in my_addr, their_addr;  // my address information     socklen_t sin_size;     int buff_sz;                             //size of data recieved      printf('App Started\n');      //allocate space for 10      my_fds = (struct pollfd**) malloc(sizeof(struct pollfd*) * MAX_CONN);      //set all the pointers to NULL     for (i = 0; i < MAX_CONN; i++)         *(my_fds + i) = NULL;      //I call listen_socket() which creates a socket to listen to     //this is anchored into my_fds array at element 0.     curr = (struct pollfd*) malloc (sizeof(struct pollfd));     curr->fd = listen_socket();     curr->events = POLLIN;     curr->revents = 0;      *my_fds = curr;      printf('Listening socket fd locked always at position zero in array: %d\n', curr->fd);      //num_fds, the count of items in the array is set to 1     //because the listen socket is already present     num_fds = 1;      //This is the main loop.     //While (true)     //  set all struct pollfd items revents to 0     //  call poll     //  loop through, see if there is data to read     //      read the data     //          loop through all sockets (except the listen_socket()) and send the data.     while (1)     {          //reset all event flag         for (i = 1; i < num_fds; i++)         {             curr = *(my_fds + i);             curr->events = POLLIN | POLLPRI;             printf('%i: fd %i\n', i, curr->fd);             curr->revents = 0;             send(curr->fd, 'Enter some text:\n', 18, 0);         }          //put all this into poll and wait for something magical to happen         printf('calling poll (%d sockets)\n', num_fds);         if (poll(*my_fds, num_fds, TIMEOUT) == -1)         {             perror('poll');             exit(0);         }          printf('poll returned!\n');          //First item is the accepting socket....check it independently of the rest!         curr = *my_fds;         if (curr->revents != 0)         {             printf('We have a new connection.\nAccept goes here...\n');              //Accept the connection             sin_size = sizeof their_addr;             new_conn = (struct pollfd*) malloc(sizeof(struct pollfd));             new_conn->fd = accept(curr->fd, (struct sockaddr *)&their_addr, &sin_size);             new_conn->events = POLLIN;             new_conn->revents = 0;              printf('Connection from %s\n', inet_ntoa(their_addr.sin_addr));             sprintf(buff, 'Your %i\n', num_fds);             send(new_conn->fd, buff, 7, 0);              //Add it to the poll call             *(my_fds + num_fds) = new_conn;             num_fds++;          }         else         {             //skip first one, we know that's the accepting socket (handled above).             for (i = 1; i < num_fds; i++)             {                 curr = *(my_fds + i);                 if (curr->revents != 0)                 {                     buff_sz = recv(curr->fd, &buff, 254, 0);                     buff[buff_sz] = '\0';                     printf('Recieved: %s', buff);                      //send the message to everyone else                     for (j = 1; j < num_fds; j++)                     {                         printf('i = %i, j = %i\n', i, j);                         if (j != i)                         {                             new_conn = *(my_fds + j);                             sprintf(buff2, '%i sent you %i: %s', i, j, buff);                             send(new_conn->fd, buff2, strlen(buff2) + 1, 0);                         }                     }                 }             }         }     }      printf('App Ended\n'); }  static int listen_socket() {     struct sockaddr_in a;     int s;     int yes;      if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {         perror('socket');         return -1;     }     yes = 1;     if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,             (char *) &yes, sizeof(yes)) < 0) {         perror('setsockopt');         close(s);         return -1;     }     memset(&a, 0, sizeof(a));     a.sin_port = htons(PORT);     a.sin_family = AF_INET;     if (bind(s, (struct sockaddr *) &a, sizeof(a)) < 0) {         perror('bind');         close(s);         return -1;     }     printf('Accepting connections on port %d\n', PORT);     listen(s, 10);     return s; } 
  • 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. 2026-05-11T02:57:12+00:00Added an answer on May 11, 2026 at 2:57 am

    Isn’t poll supposed to take an array of structures? From the man page:

       int poll(struct pollfd fds[], nfds_t nfds, int timeout); 

    And you’re passing an array of pointers to structures:

    struct pollfd **my_fds;  

    I think you need to have

    struct pollfd *my_fds = calloc(sizeof(pollfd), MAX_CONN);  

    Then your code has a chance to work.

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

Sidebar

Related Questions

So my program is supposed to recursively go through a directory and then for
I have a small program that's supposed to sample some value from a device
My program is supposed to tally up bottles from 4 rooms. When the user
I am making a simple program which suppose to accept txt file data from
The program is supposed to be a live search using php and javascript... where
This program is supposed to open a txt file, record the answers from two
So, this program is supposed to take stats from Auburn's football season and compute
The program is supposed to receive an input through cin, tokenize it, and then
This program is suppose to accept a number from the user and print that
The program is supposed to take a file, say data.dat, filled with a list

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.