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.
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.