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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:18:26+00:00 2026-06-13T17:18:26+00:00

I wanna make a chat room for 4 guys in UDP. Here’s the code:

  • 0

I wanna make a chat room for 4 guys in UDP. Here’s the code:

<code>
#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<arpa/inet.h>
#include<unistd.h>
#define PORT 9999
#define SIZE 1024
int c;
int i=0;
int k=0;
char name[4][20];
char ip[4][16];
FILE * txt;
struct sockaddr_in seraddr,cliaddr[3],getcliaddr[3];

void gettxt()
{
    txt=fopen("ip.txt","r");
    for(k=0;k<4;k++)
    {
        c=fgetc(txt);
        while(c!=' ')
        {
            name[k][i]=(char)c;
            i++;
            c=fgetc(txt);
        }
        name[k][i]='\0';
        i=0;
        c=fgetc(txt);
        while(c<'1'||c>'3')
            c=fgetc(txt);
        while(c!='\n')
        {
            ip[k][i]=c;
            c=fgetc(txt);
            i++;
        }
        ip[k][i]='\0';
        i=0;
    }
    for(k=0;k<4;k++)
        printf("<%s>%s\n",name[k],ip[k]);
}

int compare(struct sockaddr_in whichcli)
{
    int w=1;
    for(w=1;w<4;w++)
    {
        if(whichcli.sin_addr.s_addr==cliaddr[w].sin_addr.s_addr)
            break;
    }
    return w;
}

int main()
{
    int com;//compare return value (just the "k")
    int qq;
    int ret;
    int ser;
    int maxsock;
    char bufrecv[SIZE];
    char bufsend[SIZE];
    socklen_t clilen=sizeof(cliaddr[1]);//the same wa ~~I guess

    gettxt();

    fd_set readfds;

    seraddr.sin_family=AF_INET;
    seraddr.sin_addr.s_addr=inet_addr(ip[0]);
    seraddr.sin_port=htons(PORT);

    for(qq=1;qq<4;qq++)
    {
        cliaddr[qq].sin_family=AF_INET;
        cliaddr[qq].sin_addr.s_addr=inet_addr(ip[qq]);
        cliaddr[qq].sin_port=htons(PORT);
    }
    ser=socket(AF_INET,SOCK_DGRAM,0);
    bind(ser,(struct sockaddr*)&seraddr,sizeof(seraddr));

    if(STDIN_FILENO>ser)
        maxsock=STDIN_FILENO;
    else 
        maxsock=ser;

    while(1)
    {
        FD_ZERO(&readfds);  
        FD_SET(STDIN_FILENO,&readfds);
        FD_SET(ser,&readfds);
        ret=select(maxsock+1,&readfds,NULL,NULL,0); 
        if(ret>0)
        {
            if(FD_ISSET(STDIN_FILENO,&readfds))
            {
                fgets(bufsend,SIZE,stdin);
                for(qq=1;qq<4;qq++)
                    sendto(ser,bufsend,SIZE,0,(struct sockaddr*)&cliaddr[qq],clilen);
            }
            if(FD_ISSET(ser,&readfds))
            {
                for(qq=1;qq<4;qq++)
                {
                    recvfrom(ser,bufrecv,SIZE,0,(struct sockaddr*)&getcliaddr[qq],&clilen);
                    com=compare(getcliaddr[qq]);//
                    printf("<%s>%s\n",name[com],bufrecv);
                }
            }
        }
    }
    return 0;
}
</code>

The file “ip.txt” is just the name-IP file, shows as follows:

<txt>
I 192.168.1.2
Sun 192.168.1.4
Jerry 192.168.1.5
Peter 192.168.1.6
</txt>

The first contained the information of my own, the following 3 were other guy’s.
But when I ran the program with only one guy, first of all, we can chat with each other with nonblocking. After several words, it didn’t work well. I ran gcc and I guessed that when the “sendto” buffer was full, it blocked waiting the other “recvfrom” the buf. The program I wrote is for 4 guys, but I just ran it with only one guy, the other two can’t recvfrom it(still the buffer in “recvfrom” I guess,am I right?). So the “sendto” buffer was full and blocked.
That is what I think,am I right?
If it’s true, and how to solve it? I mean how to clean the “sendto” buffer periodically? Or there is some other way?
Thanks very much ~~;-)

  • 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-13T17:18:28+00:00Added an answer on June 13, 2026 at 5:18 pm

    I think the problem is in your logic, when a client sends you message you loop through all the clients and call recvfrom, you should only call recvfrom once for each time select returns.

    if(FD_ISSET(ser,&readfds))
    {    
        struct sockaddr_in src_addr;
        socklen_t addrlen = sizeof(src_addr);
        recvfrom(ser, bufrecv, SIZE, 0, (struct sockaddr*)&src_addr, &addrlen);
        com=compare(src_addr);//
        printf("<%s>%s\n",name[com],bufrecv);    
    }
    

    Edit:
    It seems that you use getcliaddr in recvfrom as the src_addr which means that each time you receive a message from a client you overwrite the address of another client, this is not a problem for one client but for more than one you could overwrite the first one with the sender’s address and you if you call recvfrom again it would block because you think it’s waiting for the first client when it’s actually waiting for the second one.

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

Sidebar

Related Questions

Hi guys i am going to make a android game. i wanna know that
i wanna do a single selection in my table view here is my code
I wanna make a program automatically tagger the text in a directory. Here's my
I wanna make a post form in code behind. I have simple html post
I wanna make a small code block about c #. first think a listbox
I wanna make option edit product when user login. Here is the link when
im new in android programming.. i have a problem here. i wanna make an
How should I go about parsing a url in php? I wanna make it
i've built my first wpf application, nothing fancy, but i wanna make it look
I will make upload my application today to my homepage and wanna to have

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.