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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:10:50+00:00 2026-06-17T09:10:50+00:00

I came up with a code with help of some tutorials to connect with

  • 0

I came up with a code with help of some tutorials to connect with clients and accept messages from slients using the select function. Now I want to do is send data to a particular client when the server needed. How to accomplish this??.. Thanks in advance.

Server code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#define PORT "9999"   // port we're listening on

// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) 
{
    return &(((struct sockaddr_in*)sa)->sin_addr);
}

return &(((struct sockaddr_in6*)sa)->sin6_addr);
}

int main(void)
{
struct timeval tv;
tv.tv_sec = 2;
tv.tv_usec = 500000;

fd_set master;    // master file descriptor list
fd_set read_fds;  // temp file descriptor list for select()
fd_set write_fds;
int fdmax;        // maximum file descriptor number

int listener;     // listening socket descriptor
int newfd;        // newly accept()ed socket descriptor
struct sockaddr_storage remoteaddr; // client address
socklen_t addrlen;

char buf[256];    // buffer for client data
int nbytes;

char remoteIP[INET6_ADDRSTRLEN];

int yes=1;        // for setsockopt() SO_REUSEADDR, below
int i, j, rv;

struct addrinfo hints, *ai, *p;

FD_ZERO(&master);    // clear the master and temp sets
FD_ZERO(&read_fds);
FD_ZERO(&write_fds);

// get us a socket and bind it
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if ((rv = getaddrinfo(NULL, PORT, &hints, &ai)) != 0) 
{
    fprintf(stderr, "selectserver: %s\n", gai_strerror(rv));
    exit(1);
}

for(p = ai; p != NULL; p = p->ai_next) 
{
    listener = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
    if (listener < 0) 
    { 
        continue;
    }

    // lose the pesky "address already in use" error message
    setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));

    if (bind(listener, p->ai_addr, p->ai_addrlen) < 0) 
    {
        close(listener);
        continue;
    }

    break;
}

// if we got here, it means we didn't get bound
if (p == NULL) 
{
    fprintf(stderr, "selectserver: failed to bind\n");
    exit(2);
}

freeaddrinfo(ai); // all done with this

// listen
if (listen(listener, 10) == -1) {
    perror("listen");
    exit(3);
}

// add the listener to the master set
FD_SET(listener, &master);

printf("Listener is %d \n" , listener);

// keep track of the biggest file descriptor
fdmax = listener; // so far, it's this one
//accept 3 clients


// main loop
for(;;) {
    read_fds = master; // copy it
    write_fds = master;
    if (select(fdmax+1, &read_fds, &write_fds, NULL, NULL) == -1) 
    {
        perror("select");
        exit(4);
    }

    //WRITE TO CONNECTIONS IF SOME SIGNAL OCCURED
    if(signal occured)
    {
        send data to some client;
    }


    // run through the existing connections looking for data to read
    // ADD NEW CONNECTIONS READ FROM CONNECTIONS
    for(i = 0; i <= fdmax; i++) 
    {

        if (FD_ISSET(i, &read_fds)) 
        { // we got one!!
            // handle new connections
            if (i == listener) 
            {                    

                addrlen = sizeof(remoteaddr);
                newfd = accept(listener, (struct sockaddr *)&remoteaddr, &addrlen);
                //write(listener+1 , "SERVER MESSAGE :: ACCEPTED" ,26);
                if (newfd == -1) 
                {
                    perror("accept");
                } 
                else 
                {
                    FD_SET(newfd, &master); // add to master set
                    if (newfd > fdmax) 
                    {    // keep track of the max
                        fdmax = newfd;
                    }
                    printf("selectserver: new connection from %s on socket %d\n", inet_ntop(remoteaddr.ss_family,
                            get_in_addr((struct sockaddr*)&remoteaddr), remoteIP, INET6_ADDRSTRLEN), newfd);
                } 
            } else 
            {
                // handle data from a client
                if ((nbytes = recv(i, buf, sizeof buf, 0)) <= 0) 
                {
                    // got error or connection closed by client
                    if (nbytes == 0) 
                    {
                        // connection closed
                        printf("selectserver: socket %d hung up\n", i);
                    } else 
                    {
                        perror("recv");
                    }
                    close(i); // bye!
                    FD_CLR(i, &master); // remove from master set
                } else 
                {
                    // we got some data from a client
                    printf("Buffer we got %s ",buf);

                }
            } // END handle data from client
        } // END got new incoming connection
        memset(&buf[0], 0, sizeof(buf));
    } // END looping through file descriptors
} // END for(;;)--and you thought it would never end!


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-06-17T09:10:51+00:00Added an answer on June 17, 2026 at 9:10 am

    You can’t send data to a client using “select()” ;).

    You can send data with a socket “send()”, just as you’re already reading data with “recv()”.

    SUGGESTION:

    Check out Beej’s Guide – an excellent primer on exactly the kind of socket programming you’re interested in:

    • http://www.beej.us/guide/bgnet/output/html/multipage/index.html

    For example:

    char *msg = "Beej was here!";
    int len, bytes_sent;
    .
    .
    .
    len = strlen(msg);
    bytes_sent = send(sockfd, msg, len, 0)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I came across some code that is supposed to help me to update
I'm working with some legacy code and I came across a function which is
I've been learning Objective C lately, and I came across some code for using
I came across following code and don't know what does having from twice mean
Context I came across some code, like this: if( Some_Condition ) throw 0; I
While looking through some old code I came across this gem: MyObject o =
The following problem boggled my mind, so I came here for some help. After
From this post i came to know that there exist some platform improvements for
I was studying some vba code and came across this: If DblBalance <> 0
I am currently reading some C++ source code, and I came across this: double

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.