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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:51:40+00:00 2026-05-14T04:51:40+00:00

This is my chat server : #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h>

  • 0

This is my chat server :

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>

#define LISTEN_Q 20
#define MSG_SIZE 1024

struct userlist {
    int sockfd;
    struct sockaddr addr;

    struct userlist *next;
};

int main(int argc, char *argv[])    {
    // declare.
    int listFD, newFD, fdmax, i, j, bytesrecvd;
    char msg[MSG_SIZE], ipv4[INET_ADDRSTRLEN];
    struct addrinfo hints, *srvrAI;
    struct sockaddr_storage newAddr;
    struct userlist *users, *uptr, *utemp;
    socklen_t newAddrLen;
    fd_set master_set, read_set;

    // clear sets
    FD_ZERO(&master_set);
    FD_ZERO(&read_set);

    // create a user list
    users = (struct userlist *)malloc(sizeof(struct userlist));
    users->sockfd = -1;
    //users->addr = NULL;
    users->next = NULL;

    // clear hints
    memset(&hints, 0, sizeof hints);

    // prep hints
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;

    // get srver info
    if(getaddrinfo("localhost", argv[1], &hints, &srvrAI) != 0) {
        perror("* ERROR | getaddrinfo()\n");
        exit(1);
    }

    // get a socket
    if((listFD = socket(srvrAI->ai_family, srvrAI->ai_socktype, srvrAI->ai_protocol)) == -1)    {
        perror("* ERROR | socket()\n");
        exit(1);    
    }

    // bind socket
    bind(listFD, srvrAI->ai_addr, srvrAI->ai_addrlen);

    // listen on socket
    if(listen(listFD, LISTEN_Q) == -1)  {
        perror("* ERROR | listen()\n");
        exit(1);            
    }

    // add listfd to master_set
    FD_SET(listFD, &master_set);

    // initialize fdmax
    fdmax = listFD;

    while(1)    {
        // equate
        read_set = master_set;

        // run select
        if(select(fdmax+1, &read_set, NULL, NULL, NULL) == -1)  {
            perror("* ERROR | select()\n");
            exit(1);            
        }

        // query all sockets
        for(i = 0; i <= fdmax; i++) {
            if(FD_ISSET(i, &read_set))  { // found active sockfd
                if(i == listFD) { // new connection
                    // accept
                    newAddrLen = sizeof newAddr;
                    if((newFD = accept(listFD, (struct sockaddr *)&newAddr, &newAddrLen)) == -1)    {
                        perror("* ERROR | select()\n");
                        exit(1);            
                    }

                    // resolve ip
                    if(inet_ntop(AF_INET, &(((struct sockaddr_in *)&newAddr)->sin_addr), ipv4, INET_ADDRSTRLEN) == -1)  {
                        perror("* ERROR | inet_ntop()");
                        exit(1);
                    }
                    fprintf(stdout, "* Client Connected | %s\n", ipv4);

                    // add to master list
                    FD_SET(newFD, &master_set);

                    // create new userlist component
                    utemp = (struct userlist*)malloc(sizeof(struct userlist));
                    utemp->next = NULL;
                    utemp->sockfd = newFD;
                    utemp->addr = *((struct sockaddr *)&newAddr);

                    // iterate to last node
                    for(uptr = users; uptr->next != NULL; uptr = uptr->next)    {
                    }

                    // add
                    uptr->next = utemp;

                    // update fdmax
                    if(newFD > fdmax)
                        fdmax = newFD;                  
                }
                else    {   // existing sockfd transmitting data
                    // read
                    if((bytesrecvd = recv(i, msg, MSG_SIZE, 0)) == -1)  {
                        perror("* ERROR | recv()\n");
                        exit(1);
                    }
                    msg[bytesrecvd] = '\0';

                    // find out who sent?
                    for(uptr = users; uptr->next != NULL; uptr = uptr->next)    {
                        if(i == uptr->sockfd)
                            break;
                    }

                    // resolve ip
                    if(inet_ntop(AF_INET, &(((struct sockaddr_in *)&(uptr->addr))->sin_addr), ipv4, INET_ADDRSTRLEN) == -1) {
                        perror("* ERROR | inet_ntop()");
                        exit(1);
                    }

                    // print
                    fprintf(stdout, "%s\n", msg);

                    // send to all
                    for(j = 0; j <= fdmax; j++) {
                        if(FD_ISSET(j, &master_set))    {
                            if(send(j, msg, strlen(msg), 0) == -1)
                                perror("* ERROR | send()");
                        }
                    }
                } // handle read from client
            } // end select result handle
        } // end looping fds
    } // end while

    return 0;
}

This is my client:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>

#define MSG_SIZE 1024

int main(int argc, char *argv[])    {
    // declare.
    int newFD, bytesrecvd, fdmax;
    char msg[MSG_SIZE];
    fd_set master_set, read_set;
    struct addrinfo hints, *srvrAI;

    // clear sets
    FD_ZERO(&master_set);
    FD_ZERO(&read_set);

    // clear hints
    memset(&hints, 0, sizeof hints);

    // prep hints
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;

    // get srver info
    if(getaddrinfo(argv[1], argv[2], &hints, &srvrAI) != 0) {
        perror("* ERROR | getaddrinfo()\n");
        exit(1);
    }

    // get a socket
    if((newFD = socket(srvrAI->ai_family, srvrAI->ai_socktype, srvrAI->ai_protocol)) == -1) {
        perror("* ERROR | socket()\n");
        exit(1);    
    }

    // connect to server
    if(connect(newFD, srvrAI->ai_addr, srvrAI->ai_addrlen) == -1)   {
        perror("* ERROR | connect()\n");
        exit(1);    
    }

    // add to master, and add keyboard
    FD_SET(newFD, &master_set);
    FD_SET(STDIN_FILENO, &master_set);

    // initialize fdmax
    if(newFD > STDIN_FILENO)
        fdmax = newFD;
    else
        fdmax = STDIN_FILENO;

    while(1)    {
        // equate
        read_set = master_set;

        if(select(fdmax+1, &read_set, NULL, NULL, NULL) == -1)  {
            perror("* ERROR | select()");
            exit(1);
        }

        // check server
        if(FD_ISSET(newFD, &read_set))  {
            // read data
            if((bytesrecvd = recv(newFD, msg, MSG_SIZE, 0)) < 0 )   {
                perror("* ERROR | recv()");
                exit(1);
            }
            msg[bytesrecvd] = '\0';

            // print
            fprintf(stdout, "%s\n", msg);
        }

        // check keyboard
        if(FD_ISSET(STDIN_FILENO, &read_set))   {
            // read data from stdin
            if((bytesrecvd = read(STDIN_FILENO, msg, MSG_SIZE)) < 0)    {
                perror("* ERROR | read()");
                exit(1);
            }
            msg[bytesrecvd] = '\0';


            // send
            if((send(newFD, msg, bytesrecvd, 0)) == -1) {
                perror("* ERROR | send()");
                exit(1);
            }
        }
    }

    return 0;
}

The problem is with the part where the server recv()s data from an FD, then tries echoing back to all [send() ]; it just dies, w/o errors, and my client is left looping 🙁

  • 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-14T04:51:40+00:00Added an answer on May 14, 2026 at 4:51 am

    Two issues I see with the server at least:

    1. msg[bytesrecvd] = '\0'; overruns the buffer by one (this can be avoided by doing recv(i, msg, MSG_SIZE - 1, 0) instead;
    2. the “send to all” part tries to write to server socket too.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This my chat mixed with threads to run server and chat at same time.
Can ejabberd chat server be integrated with scalable and fast server technologies like node.js/tornado/socket.io
x1.instance3650.db.xeround.com-nino_db [chatserver.sql.MySqlConnection.rehash()] this is con null [.()] connection to MySQL server failed com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Cannot
Is this chat using long polling or http streaming ? http://go-mono.com/moonlight/chat.aspx
I am creating a chat application. In this chat application, I have to use
i'm building an android application which have a chat. in this chat i each
$('.chat').submit(function() { $.post(, $(this).serialize(), function(response) { $(this).closest('.stake_chat').prepend(???); }); return false; }); Where ??? is
this is kinda overkill for me.. I am using a JTextPane for a chat,
I'm folowing this tutorial to create php/jquery based chat application. In short, this code
I'm implementing a simple chat in .NET using Rx on the basis of this

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.