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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:50:37+00:00 2026-06-13T05:50:37+00:00

I’m learning socket programming and i want when that clients are connected to my

  • 0

I’m learning socket programming and i want when that clients are connected to my server. I can send data to them with their specific address..

e.g

Server —> client 1

╚—> client 2

╚—> client 3

Connection from: 192.168.5.3

Connection from: 192.168.5.10

Connection from: 192.168.5.15

For example say client 1 sends data to me and then client 2 also sends data to me i want to reply back only to client 1 how could i possibly to do that?..

How will i store my client so when i need to send data to them i know which client will i send data to?

Here’s my Code.

#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <errno.h>
#include <string.h>
#include <sys/select.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <poll.h>

#define TRUE             1
#define FALSE            0

typedef struct SERVER_FD{

int sPort;
int serverFD;
int smaxFD; 
int newFD;



}sSD;
pid_t pid, sid;
int cFD, 
    dSize, 
    err, 
    start = 1,
    state,
    DescRead,
    DCSERVER = FALSE;

int fd;
char buf[255];
int nbytes; 


struct sockaddr_in  addr, cli_addr;
unsigned long ip;
char strbuf[256];
socklen_t clilen;
fd_set fdin, fduse;
struct pollfd pfds[2];
int rc;

void process(int ServerFD, int Port, int sMax, int NewSFD);
void cleanUP(int i, int max);
void dlogs(unsigned long ip);

main (int argc, char *argv[])
{
    sSD link;
    sSD *sCon;
    sCon = &link;
    sCon->sPort = 53234;

    fd = open("/tmp/myFIFO", O_RDWR);
        if(fd == -1) {
        printf("Could not open the pipe\n");
    }

fcntl(fd, F_SETFL,
fcntl(fd, F_GETFL) | 
O_NONBLOCK);


   sCon->serverFD = socket(AF_INET, SOCK_STREAM, 0);
   if (sCon->serverFD != -1)
   {
    err = setsockopt(sCon->serverFD, SOL_SOCKET,  SO_REUSEADDR,(char *)&start, sizeof(start));
      if (err != -1)
            {
                err = ioctl(sCon->serverFD, FIONBIO, (char *)&start);

                    if (err != -1){

                    process(sCon->serverFD,sCon->sPort,sCon->smaxFD,sCon->newFD);


                    }
                            else{

                        perror("ioctl() failed");
                            close(sCon->serverFD);
                                exit(EXIT_FAILURE);

                        }
            }

                    else{

              perror("setsockopt() failed");
                close(sCon->serverFD);
                    exit(EXIT_FAILURE);
                        }
   }
       else{
       perror("FAILED CONNECTING TO SOCKET");
            exit(EXIT_FAILURE);
   } 

}

void process(int ServerFD, int Port, int sMax, int NewSFD){



   bzero((char *) &addr, sizeof(addr));

   addr.sin_family      = AF_INET;
   addr.sin_addr.s_addr = 0;
   addr.sin_port        = htons(Port);

   err = bind(ServerFD,(struct sockaddr *)&addr, sizeof(addr));
   if (err < 0)
   {
      perror("bind() failed");
      close(ServerFD);
      exit(EXIT_FAILURE);
   }


   err = listen(ServerFD, 32);
   if (err < 0)
   {
      perror("listen() failed");
      close(ServerFD);
      exit(EXIT_FAILURE);
   }

   clilen = sizeof(cli_addr);


   FD_ZERO(&fdin);
   sMax = ServerFD;
   FD_SET(ServerFD, &fdin);



   do
   {

        fduse = fdin;

      //printf("Waiting on select()...\n");
      err = select(sMax + 1, &fduse, NULL, NULL, NULL);

      if (err < 0)
      {
     perror("  select() failed");
     break;
      }

      DescRead = err;
      for (cFD=0; cFD <= sMax  &&  DescRead > 0; ++cFD)
      {

     if (FD_ISSET(cFD, &fduse))
     {

        DescRead -= 1;


        if (cFD == ServerFD)
        {
           //printf("  Listening socket is readable\n");

           do
           {

              NewSFD = accept(ServerFD,(struct sockaddr *) &cli_addr, &clilen);
              if (NewSFD < 0)
              {
                 if (errno != EWOULDBLOCK)
                 {
                    perror("  accept() failed");
                    DCSERVER = TRUE;
                 }
                 break;
              }
                ip = ntohl(cli_addr.sin_addr.s_addr);
                printf("  Connection from %d.%d.%d.%d\n",
                    (int)(ip>>24)&0xff,
                    (int)(ip>>16)&0xff,
                    (int)(ip>>8)&0xff,
                    (int)(ip>>0)&0xff);
                    dlogs(ip);

              FD_SET(NewSFD, &fdin);
              if (NewSFD > sMax)
                 sMax = NewSFD;

           } while (NewSFD != -1);


        }

        else
        {
           //printf("  Descriptor %d is readable\n", cFD);
          pfds[0].fd = fd;
          pfds[0].events = POLLIN;
          pfds[1].fd = cFD;
          pfds[1].events = POLLIN;
          state = FALSE;


           do
           {
            rc = poll(pfds, 2, -1);

            if (pfds[0].revents & POLLIN)
             {
              while ((nbytes = read(fd, buf, sizeof(buf)-1)) > 0)
               {
                   buf[nbytes] = '\0';
                     printf("%s\n", buf);
                   }


              pfds[0].events = 0;
              pfds[1].events = POLLIN | POLLOUT;

               }

            if (pfds[1].revents & POLLIN)
            {
              err = recv(cFD, strbuf, sizeof(strbuf), 0);
              if (err < 0)
              {
                 if (errno != EWOULDBLOCK)
                 {
                    perror("  recv() failed");
                    state = TRUE;

                 }
                 break;
              }

               if (err == 0)
              {
                 printf("  Connection closed\n");
                 state = TRUE;

                 break;
              }
              dSize = err;
             printf("  %d bytes received\n", dSize);
            }

            if (pfds[1].revents & POLLOUT)
            {
                err = send(cFD, buf, strlen(buf), 0);
              if (err < 0)
              {
                 perror("  send() failed");
                 state = TRUE;

                 break;
              }
                pfds[0].events = POLLIN;
                pfds[1].events = POLLIN;
            }


           } while (TRUE);

           fopen("/sockF.txt","w");
           if (state)
           {

              close(cFD);
              FD_CLR(cFD, &fdin);
              if (cFD == sMax)
              {
                 while (FD_ISSET(sMax, &fdin) == FALSE)
                    sMax -= 1;
              }
           }
        } 
     } 
      } 

   } while (DCSERVER == FALSE);

    cleanUP(cFD, sMax);

}

void cleanUP(int i, int max){

for (i=0; i <= max; ++i)
   {
     if (FD_ISSET(i, &fdin))
     close(i);
   }

}

void dlogs(unsigned long ip){

FILE* pFile = fopen("/sockF.txt", "w+");

fprintf(pFile,"Connection from: %d.%d.%d.%d",
                    (int)(ip>>24)&0xff,
                    (int)(ip>>16)&0xff,
                    (int)(ip>>8)&0xff,
                    (int)(ip>>0)&0xff);

fclose(pFile);

}

Clients can connect to me but i don’t know which of them to send data when both of them are connected… I can send data when only 1 client is connected to my server but how will i send data to another client when 2 of my client is connected at the same time?

Thanks,

  • 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-13T05:50:38+00:00Added an answer on June 13, 2026 at 5:50 am

    First – i would say it is not well written. But since you are learning you need to understand which piece of code is doing what.

    NewSFD = accept(ServerFD,(struct sockaddr *) &cli_addr, &clilen);
    

    NewSFD is the resultant new connected descriptor/socket to only the client which sent you the message. If you do a write/send on this descriptor/socket, it will be received by the sending client.

    char * response = "Connection echoed";
    send(NewSFD, response, strlen(response), 0); // call returns number of bytes written.
    

    If you want to do it at some point latter, you need to save this socket in some struct and access it to write to the specific client.

    cli_addr would actually return the client info such as port and ip, which could be used to create a new socket latter. However, you are using tcp which requires a handshake through connect and accept calls. So using the saved connected socket is the appropriate choice.

    You can use a struct like this

    typedef struct _client_fds{
        int connected_sock; // copy the returned value of accept sock_addr_in cli_addr;
        struct sock_add_in cli_addr // copy the client struct here
    } client_fds;
    

    client_fds Clients[MAX];

    and latter retrieve socket to write from it

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I

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.