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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:24:53+00:00 2026-05-26T16:24:53+00:00

I’m having a problem with my server printing what is sent to it. I

  • 0

I’m having a problem with my server printing what is sent to it. I know that the data is being received because it broadcasts it back to all clients.

#include <iostream>
#include <string>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <ncurses.h>
#include <vector>

#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#define MAX_CONNECTIONS 10
#define MAX_SRSIZE 500

using namespace std;

struct bcpackage{
    string * message;
};
struct clientval{
    int fd;
};

vector<int> file_descriptors;
WINDOW * console, * input;

void * handleClient(void * arg);
void * broadcast(void * arg);
void wprintr(WINDOW * win, const char * message);

int main(int argc, char **argv)
{
    int myfd, * status;
    status = new int();
    struct addrinfo myaddrinfo, *res;

/****************SETUP NCURSES UI******************/
    initscr();
    int y, x;
    getmaxyx(stdscr, y, x);
    console = subwin(stdscr,y - 1, x, 0, 0);
    input = subwin(stdscr,1,x,y-1,0);
    wrefresh(console);
    wprintr(input,">");
/**************************************************/
    string port = "25544";
    memset(&myaddrinfo, 0, sizeof(myaddrinfo));
    myaddrinfo.ai_family = AF_UNSPEC;
    myaddrinfo.ai_socktype = SOCK_STREAM;
    myaddrinfo.ai_flags = AI_PASSIVE;//Specifies that your socket will be a passive     socket that waits for connections
    wprintr(console,"Starting Server");
    int aistat = getaddrinfo(NULL, "25544", &myaddrinfo, &res);
    if( aistat == 0){
        wprintr(console,"Host Information Retrieved");
    }
    else{
        //string message = "Error: ";
        //message+=gai_strerror(aistat);
        wprintr(console, gai_strerror(aistat));
        endwin();
        exit(1);
    }
//We now have our address now we create a socket
    myfd = socket(res->ai_family, res->ai_socktype,res->ai_protocol);
    if(myfd==-1){
        wprintr(console, "Socket Creation Failed");
        getch();
        endwin();
        exit(2);
    }
//If all went well, we now have a socket for our server
//we will now use the bind() function to bind our socket
//to our program. I think that is what it does at least.
    *status = bind(myfd, res->ai_addr, res->ai_addrlen);
    //wprintw(console, "Status: %d\n", *status);
    if((*status) < 0){
        wprintr(console, "Bind failed");
        getch();
        endwin();
        exit(3);
    }
    else{
        wprintr(console, "Bind success");
    }
    //Now that we are bound, we need to listen on the socket
    *status = listen(myfd, MAX_CONNECTIONS);
    if(status>=0){
        wprintr(console, "Listening on socket");
    }
    else{
        wprintr(console, "Listen failed");
        getch();
        endwin();
        exit(4);
    }

//Everything is setup now we send the server into a loop that will pass
//each client to a new pthread.
    while(true){

        int *clientfd = new int();
        pthread_t * cliPID = new pthread_t();
        struct sockaddr_in * cliaddr = new struct sockaddr_in();
        socklen_t *clilen = new socklen_t();
        *clilen = sizeof(*cliaddr);
        *clientfd = accept(myfd, (struct sockaddr *)cliaddr, clilen);
        file_descriptors.push_back(*clientfd);
        pthread_create(cliPID, NULL, handleClient, clientfd);

    }
    wprintr(console, "Now Exiting");
    getch();
    endwin();
    return 0;
}

void * handleClient(void * arg){//Reads and writes to the functions
    int filedesc = *((int *)arg);
    char buffer[MAX_SRSIZE];
    char * buf = buffer;
    memset(&buffer, 0, sizeof(buffer));
    while(!read(filedesc, buf, sizeof(buffer))<=0){
        if(strcmp(buf, "")!=0){
            wprintr(console, buffer);//<- I think this is the problem Idk why though.
            broadcast(&buffer);
        }
        memset(&buffer, 0, sizeof(buffer));
    }
    wprintr(console, "Client Exited");
    int fdremove = -1;
    for(int i = 0; i < file_descriptors.size(); i++){
        if(file_descriptors.at(i)==filedesc){
            file_descriptors.erase(file_descriptors.begin()+i);
            wprintr(console, "File Descriptor Removed");
        }
    }
    pthread_exit(NULL);

}

void * broadcast(void * arg){
    char * message = (char *)arg;
    int num_fds = file_descriptors.size(); 
    for(int i = 0; i < num_fds; i++ ){
        write(file_descriptors.at(i), message, MAX_SRSIZE);
    }

}
void wprintr(WINDOW * win, const char * message){
    wprintw(win, message);
    wprintw(win, "\n");
    wrefresh(win);
}

This is written for Linux and needs -lpthread and -lncurses to compile. When you run the server you can telnet to it to test it. I know that data is being received because it is getting broadcasted back to all of the clients however the data received is not being printed on the server’s screen. I think it may be an issue with ncurses but I don’t know. I believe the problem is in my handleClient function.

Thanks in advance.

  • 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-26T16:24:54+00:00Added an answer on May 26, 2026 at 4:24 pm

    telnet sends "\r\n" at the end of every line. If you don’t remove those characters every line you print is instantly overwritten.

    I think it’s bad idea to use ncurses in a server. Usually you want to save a log to a file, which would be very hard if you use ncurses. And, you will run into problems like this, because you cannot tell exactly what your program is outputting.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string

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.