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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:00:44+00:00 2026-06-17T19:00:44+00:00

I built very basic UDP chat using C language and Fedora 16 OS. When

  • 0

I built very basic UDP chat using C language and Fedora 16 OS.

When my server connect, he got 5 ports and listen to them.

My problem is: when I’m trying to connect to the server with new client but gives him port that my server don’t know, I want him to exit Immediately.

I don’t know how to check the “MSG_DONTWAIT” flag.

Here is my code:

Server side:

#define MAX_MESSAGE_SIZE 1024
#define SERVER_CONNECTIONS 5

// Implementation of server that manages a chat.
#include "server.h"

int main(int argc,char* argv[])
{
    if(argc != 2)  //check if user insert more then one argument to the program
    {
        printf("Usage server <port>\n‬‬");
        fflush(stdout);
        exit (-1);
    }

/*!
========Server creation flow:========
1) create the socket
2) bind the socket to a port
3) recvfrom (read from socket)
4) sendto (close the socket)
5) close the socket
*/

//!------------------------------------- 1) create the socket-------------------------------------
//!------------------------------- 2) bind the socket to a port-----------------------------------
    int fd[SERVER_CONNECTIONS];  //socket descriptor
    int port[SERVER_CONNECTIONS];  //socket fd port
    int i=0;
    for(i=0; i<SERVER_CONNECTIONS; i++)
    {
        port[i] = atoi(argv[1])+i;
    }
    create_sockets(fd, port);

    char buf[MAX_MESSAGE_SIZE]; //used by read() & write()
    int maxfd = find_maxfd(fd);
    struct sockaddr_in cli;  //used by read() & write()
    int cli_len = sizeof(cli);  //used by read() & write()

    fd_set readfds;
    fd_set writefds;
    struct timeval timeout;
    timeout.tv_sec = 1;
    int nbytes=0;

    while(1)
    {
        FD_ZERO(&readfds);
        FD_ZERO(&writefds);
        for(i=0; i<SERVER_CONNECTIONS; i++)
        {
            FD_SET(fd[i], &readfds);
            FD_SET(fd[i], &writefds);
        }

        /* Now use FD_SET to initialize other fd’s that have already been returned by accept() */
        if (select(maxfd+1, &readfds, 0, 0, 0) < 0)
        {
            perror("select");
            exit(1);
        }

        for(i=0; i<SERVER_CONNECTIONS; i++)
        {
//!------------------------------- recvfrom (read from socket)-----------------------------------
            if(FD_ISSET(fd[i], &readfds))
            {
                fprintf(stderr, "ready to read from %d\n", fd[i]);
                memset(&buf, 0, sizeof(buf)); //init buf
                if((nbytes = recvfrom(fd[i], buf, sizeof(buf), 0 /* flags */, (struct sockaddr*) &cli, (socklen_t*)&cli_len)) < 0)
                {
                    perror("recvfrom");
                    exit(1);
                }
//!------------------------------- sendto (close the socket)-----------------------------------
                FD_ZERO(&writefds);
                FD_SET(fd[i], &writefds);

                if (select(maxfd+1, 0, &writefds, 0, &timeout) < 0)
                {
                    perror("select");
                    exit(1);
                }

                if(FD_ISSET(fd[i], &writefds))
                {
                    fprintf(stderr, "ready to write to %d\n", fd[i]);
                    string_to_hex(buf);
                    if ((nbytes = sendto(fd[i], buf, strlen(buf), 0 /* flags */, (struct sockaddr*) &cli, sizeof(cli))) < 0)
                    {
                        perror("sendto");
                        exit(1);
                    }
                }
            }
        }
    }
    return 0;
}


void create_sockets(int fd[], int port[])
{
    int i=0;
    for(i=0; i<SERVER_CONNECTIONS; i++)
    {
//!------------------------------------- 1) create the socket-------------------------------------
        if((fd[i] = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
        {
            perror("socket");
            exit(1);
        }

//!------------------------------- 2) bind the socket to a port-----------------------------------
        struct sockaddr_in srv;  //used by bind()
        srv.sin_family = AF_INET;  //use the Internet address family
        srv.sin_port = htons(port[i]);  //socket ‘fd’ to port
        srv.sin_addr.s_addr = htonl(INADDR_ANY);  //a client may connect to any of my addresses

        if(bind(fd[i], (struct sockaddr*) &srv, sizeof(srv)) < 0)
        {
            perror("bind");
            exit(1);
        }
    }
}

int find_maxfd(int fd[])
{
    int i=0;
    int res=fd[0];
    for(i=1; i<SERVER_CONNECTIONS; i++)
    {
        if(fd[i]>res)
        {
            res = fd[i];
        }
    }
    return res;
}

void string_to_hex(char buf[])
{
    int buf_size = strlen(buf);

    char result[buf_size*3+1];
    memset(&result, 0, sizeof(result));
    char temp[4];

    int i=0;
    for (i=0; i<buf_size-1; i++)
    {
        memset(&temp, 0, sizeof(temp));
        sprintf(temp, "%X:", (int)buf[i]);
        strcat(result, temp);
    }
    memset(&temp, 0, sizeof(temp));
    sprintf(temp, "%X", (int)buf[i]);
    strcat(result, temp);

    strcpy(buf, result);
}

Client side:

#define MAX_MESSAGE_SIZE 1024

// Implementation of client that will use the chat.
#include "client.h"

int main(int argc,char* argv[])
{
    if(argc != 3)  //check if user insert more then one argument to the program
    {
        printf("Usage client <host name> <port>\n‬‬");
        fflush(stdout);
        exit(-1);
    }

/*!
========Client creation flow:========
1) create the socket
2) sendto (close the socket)
3) recvfrom (read from socket)
4) close the socket
*/

    fprintf(stderr, "please enter something: \n");

//!------------------------------------- 1) create the socket-------------------------------------
    int fd;  //socket descriptor
    if((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
    {
        perror("socket");
        exit(1);
    }

    struct sockaddr_in srv;  //used by sendto()

    srv.sin_family = AF_INET;
    srand ( time(NULL) );  //new random seed
    int rand_num = (rand() % 5) + atoi(argv[2]);  //
    srv.sin_port = htons(rand_num);

    char *srv_name = argv[1];
    struct hostent *hp; //ptr to host info for remote
    hp = gethostbyname(srv_name);
    if( hp == NULL)
    {
        herror("gethostbyname");
        exit(-1);
    }

    srv.sin_addr.s_addr = ((struct in_addr*)(hp->h_addr))->s_addr; //set IP Address to "srv_name"

    char buf[MAX_MESSAGE_SIZE]; //used by read() & write()
    int nbytes=0;
    while(1)
    {
//!------------------------------------- 2) sendto (close the socket)-------------------------------------
        memset(&buf, 0, sizeof(buf)); //init buf
        fgets(buf, sizeof(buf), stdin); //get input from user

        if(strcmp(buf, "quit\n") == 0)
        {
            break;
        }

        if(!((strlen(buf) == 1) && (buf[1] == '\n')))
        {
            buf[strlen(buf)-1] = '\0';
        }
        //write_to_server(fd, buf, srv);
        if ((nbytes = sendto(fd, buf, strlen(buf), MSG_DONTWAIT /* flags */, (struct sockaddr*) &srv, sizeof(srv)) < 0))
        {
            perror("sendto");
            exit(1);
        }

//!------------------------------------- 3) recvfrom (read from socket)-------------------------------------
        memset(&buf, 0, sizeof(buf)); //init read_buf
        //read_from_server(fd, buf);
        if((nbytes = recvfrom(fd, buf, sizeof(buf), 0 /* flags */, 0, 0) < 0))
        {
            perror("recvfrom");
            exit(1);
        }
        if( (errno == EAGAIN) || (errno == EWOULDBLOCK ) )
        {
            perror("EWOULDBLOCK");
            exit(1);
        }
        printf("%s\n", buf); //print result to client
        fflush(stdout);
    }
//!------------------------------------- close the socket-------------------------------------
    close(fd);
    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-17T19:00:45+00:00Added an answer on June 17, 2026 at 7:00 pm

    I got it..

    You need to use connect function from client and the “MSG_DONTWAIT” flag.

    then when the client connect and you type anything he exit right away..

    //!--------------- 2) connect to the server--------------------------
    if(connect(fd, (struct sockaddr*) &srv, sizeof(srv)) < 0) //connect to server "srv_name"
    {
        perror("connect");
        exit(-1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've built a very basic grid of divs by using float: However when expanding
so I have built my own drag and drop shopping cart(very basic) using Jquery
i built a very basic video player app in Xcode 4.3.2 i have 3
I've built a very simple website using PHP Includes to display the footer and
I have built a very simple blog application using Ruby on Rails. New to
I have built my own, very basic data warehouse. In it I have very
I'm building a solution for a client which allows them to create very basic
I m very new to Node.js and Socket.io. I have built a very basic
I've built a very basic jQuery plugin that essentially positions a sprite image left
I am currently trying to build a very basic serial shell with my arduino.

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.