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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:22:55+00:00 2026-05-18T08:22:55+00:00

I’m trying to find a solution to a question I posted earlier about synchronizing

  • 0

I’m trying to find a solution to a question I posted earlier about synchronizing chat messages, and one member pointed me in the direction of the select() function. I read this section under Beej’s Network Guide and tried to write the sample given under windows. It compiles fine but when I go to test it, The program crashes and displays my error message "-Select error"– after inputting the port number into the program. I’m uncertain of how to get this working, please advise.

server.cpp

#include <iostream>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
using namespace std;
const int winsockver = 2;
int PORT;
int fdmax;
char buffer[80];

int main(void){
    //*********************************************
    fd_set master;
    fd_set temp;
    SOCKET newfd;
    struct sockaddr_in connected_client;
    //*********************************************

    WSADATA wsadata;
    if (WSAStartup(MAKEWORD(winsockver,0),&wsadata) == 0 ){
        cout<<"-Winsock Initialized." << endl;
        cout<<"Enter PORT:";
        cin>> PORT;
        //--------------------------------------------------------------------
        struct sockaddr_in server_info;
        server_info.sin_family = AF_INET;
        server_info.sin_port = htons(PORT);
        server_info.sin_addr.s_addr = INADDR_ANY;

        SOCKET serv_sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
        if ( serv_sock != INVALID_SOCKET){
            if ( bind(serv_sock,(sockaddr*)&server_info,sizeof(server_info)) != -1 ){
                char yes = '1';
                if ( setsockopt(serv_sock,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) != SOCKET_ERROR){
                    cout<<"-Binding Successful." << endl;
                }
            }

            if ( listen(serv_sock,5) != -1 ){
                cout<<"-Listening for incoming connections." << endl;
            }

            FD_SET(serv_sock,&master);
            fdmax = serv_sock; // keeping track of the largers sockfd, at this moment its serv_sock
            //#########################################################
            for(;;){
                temp = master; // copying to temp the master set
                if (select(fdmax+1,&temp,NULL,NULL,NULL) == -1 ){
                    cout<<"-select error." << endl;
                }

                //run through existing connections looking for data to read 
                for (int i = 0; i <= fdmax; i++){
                    if (FD_ISSET(i,&temp)){//we have one
                        if (i == serv_sock){//handle new connections
                            int size =sizeof(connected_client);
                            newfd = accept(serv_sock,(sockaddr*)&connected_client,&size);

                            if ( newfd == -1 ){
                                cout<<"-Accepted an invalid socket from newfd." << endl;
                            }else{//accept has returned a valid socket descriptor and we add it to the master
                                FD_SET(newfd,&master);
                                if (newfd > fdmax ){
                                    fdmax = newfd;
                                }
                                char *connected_ip= inet_ntoa(connected_client.sin_addr);   
                                cout<<"-Select server new connection from " << connected_ip << " " <<  endl;
                            }

                        }else{
                            //handle data from a client
                            int bytes_in;
                            bytes_in = recv(i,buffer,sizeof(buffer),0);
                            if ( bytes_in <= 0 ){
                                if (bytes_in == 0 ){
                                    cout<<"-Connected socket " << i << ",disconnected " << endl;
                                }else{
                                    cout<<"-Socket error." << endl;
                                }
                                closesocket(i);
                                FD_CLR(i,&master); //remove from master set.
                            }else{
                                //we get data from a client
                                for (int j=0; j <= fdmax; j++ ){
                                    //send to everyone
                                    if (FD_ISSET(j,&master)){
                                        //except the listener and ourself
                                        if ( (j != serv_sock) && (j != i) ){
                                            if ( send(j,buffer,sizeof(buffer),0) == -1 ){
                                                cout<<"-Sending error" << endl;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            //#########################################################
        }

        //--------------------------------------------------------------------
    }else{
        cout<<"-Unable to Initialize." << endl;
    }

    if ( WSACleanup() != -1 ){
        cout<<"-Cleanup Successful." << endl;
    }
    return 0;
}
  • 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-18T08:22:55+00:00Added an answer on May 18, 2026 at 8:22 am

    Your file descriptor sets are not properly initialized, so they still contain garbage when you call FD_SET() on them.

    You should call FD_ZERO() to initialize them before you start using them:

    FD_ZERO(&master);
    FD_SET(serv_sock, &master);
    fdmax = serv_sock;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
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.