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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:58:29+00:00 2026-05-20T23:58:29+00:00

I have to write a C app that will act as both UDP sender

  • 0

I have to write a C app that will act as both UDP sender & receiver. The program should first detect the broadcast address, then send the message JOIN (name is read), 1 time/minute, then some messages. I understood the part with broadcasting packages. I can’t figure out how to turn my sender into a receiver also.
My code so far:
—old code —

Edit:
I now know that I have solve this without select() or poll(), but with fork()..Something like creating 2 separate processes that will deal with writing to the socket and, respectively, reading data from the socket. I manage to send data. I have some troubles reading data from the socket. I receive smth like the last characters of the string I send. Also, I have an error “:Invalid argument”..I’ve tried debugging but I don’t really know where this error comes from. It appears just after I do the fork(). I have to be able to ignore mssages that I send somehow, but testing this locally it’s confusing (I have to receive messages from others and also, know the IP address that sent me the message). Now, if I start the app twice, In one app I don’t receive messages from the other, just the last characters of what the app sends.
Here’s my code:

#includes..
#define MAXLEN 100
#define MAXNAME 20
#define PORT 8888
#define MAX 20

int sockfd;
struct sockaddr_in socket_in;   //connector's info
char name[MAXNAME];
int numbytes;
 char senders[MAX];

void sendJoin (int signal) {
    if ((numbytes=sendto(sockfd, name, strlen(name), 0,
             (struct sockaddr *)&socket_in, sizeof(socket_in))) == -1) {
        perror("sendto");
        exit(1);
    }
    printf("Sent %s to %s (%d bytes)\n", name,inet_ntoa(socket_in.sin_addr),numbytes);
    alarm(5);
}

void leaveGroup(int signal) {
    /* Broadcast LEAVE and end execution */
    char msg[10];
    strcpy(msg, "LEAVE\0");
    if ((numbytes=sendto(sockfd, msg, strlen(msg), 0,
             (struct sockaddr *)&socket_in, sizeof(socket_in))) == -1) {
        perror("sendLEAVE");
        exit(1);
    }
    printf("\nSent LEAVE message to %s (%d bytes)\n",      inet_ntoa(socket_in.sin_addr),numbytes);    
    scanf("%s", msg);
    close(sockfd);
    exit(1);
}

void read_socks() {   
    char message[MAXLEN];
    int size = sizeof(socket_in);
    numbytes = recvfrom (sockfd, &message, 1, 0,
                (struct sockaddr*) &socket_in, &size);
    if(strcmp(message, name)!=0) {
        printf("Received: %s\n", message);
    }
}

int main(void){

struct hostent *hst;   
int broadcast = 1; 

if ((hst=gethostbyname("192.168.240.255")) == NULL) {  // get the host info
    perror("gethostbyname");
    exit(1);
}

if((sockfd=socket(AF_INET, SOCK_DGRAM, 0))==-1) {
    perror("socket");
    exit(1);
}
if(setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast))==-1){
    perror("setsockopt(SO_BROADCAST)");
    exit(1);
}

memset(&socket_in, 0, sizeof(socket_in));
socket_in.sin_family = AF_INET;
socket_in.sin_port = htons(PORT);
socket_in.sin_addr = *((struct in_addr *)hst->h_addr);

signal(SIGINT, leaveGroup);
signal(SIGALRM, sendJoin);

printf("NAME: ");
fgets(name,MAXNAME,stdin);
strcpy(name,strcat("JOIN: ", name));
name[strlen(name)-1]='\0';
if ((numbytes=sendto(sockfd, name, strlen(name), 0,
             (struct sockaddr *)&socket_in, sizeof(socket_in))) == -1) {
        perror("sendto");
        exit(1);
    }

printf("Sent %s to %s (%d bytes)\n", name,inet_ntoa(socket_in.sin_addr),numbytes);      
alarm(5);

while (1)
{        
    int pid = fork();
    if(pid==0){            //Child process reads from the socket
        memset(&socket_in, 0, sizeof(socket_in));
        socket_in.sin_family = AF_INET;
        socket_in.sin_port = htons(PORT);
        socket_in.sin_addr = *((struct in_addr *)hst->h_addr);
        sockfd = bind(sockfd, (struct sockaddr*) &socket_in, sizeof(socket_in));
        if(sockfd==-1) perror("Bind error. Port is already in use!");
        read_socks();
        exit(0);
    }
    else if(pid > 0){    //Parent process sends data through the socket
        memset(&socket_in, 0, sizeof(socket_in));
        socket_in.sin_family = AF_INET;
        socket_in.sin_port = htons(PORT);
        socket_in.sin_addr = *((struct in_addr *)hst->h_addr);
        if ((numbytes=sendto(sockfd, name, strlen(name), 0,
             (struct sockaddr *)&socket_in, sizeof(socket_in))) == -1) {
            perror("sendto");
            exit(1);
        }
    }
    sleep(4);
    }
close(sockfd);
return 0;
}

I would greatly appreaciate some suggestions.

  • 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-20T23:58:29+00:00Added an answer on May 20, 2026 at 11:58 pm

    What you need to do is suspend your program until one of two things happen:

    • somebody sends you a UDP message
    • the minute is up (you need to send your broadcast message)

    The solution on unix, if you want to suspend until one of multiple things happen, is the function select().

    Another possible solution in your case is to use non-blocking I/O and check for new packages once in a while — but that’s basically active polling and active polling is bad, bad, bad.

    The problem you face with your code is that your program is blocked in recvfrom() until it gets a message. (Note: I’m not totally sure whether your SIGALRM method should indeed work during blocked I/O wait but I assume the signal gets queued until your reading operation is finished (data received))

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

Sidebar

Related Questions

When you write an app that will have vast number of classes, do you
I'm about to have to write a web page/app that will serve the agenda
I have to write a program that read from a file that contains the
I'm trying to write a managed library in C# that will act as an
I have a console app that will be running through a scheduled task and
I need to write an app that that will iterate over our database, and
im trying to write an app that will display a list off lines from
I'm looking to write a method for an iPhone app that will auto adjust
I am writing a simple Android app and have a database that will send
I'm trying to write an app that will open an excel spreadsheet find the

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.