Find the answer to your Linux question:
Results 1 to 9 of 9
Hi there, how are u all doing? I am writing a simple UDP server, and I have a quick question, I want my server to serve only one client at ...
  1. #1
    Just Joined!
    Join Date
    May 2009
    Posts
    35

    writting a simple UDP server, a little help please?

    Hi there,

    how are u all doing?

    I am writing a simple UDP server, and I have a quick question, I want my server to serve only one client at a time, but I really have no idea where to start, any tips on how to do that, is there a specific function i should use, like for example, connect?

    I googled it but to no avail!!

    any help is appreciated!!

    thank u in advance

  2. #2
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,422
    What language are you writing this in? Ill deliver a sample
    Also, wrong section.. my advice reaches that you will use the right section on the forums next time for a quicker reply and answer.
    New Users, please read this..
    Google first, then ask..

  3. #3
    Just Joined!
    Join Date
    May 2009
    Posts
    35
    I am writing my program in C, and sorry if its in the wrong forum, i was confused on where to ask my question, but thought since its mostly a programming question i should put it here

  4. #4
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,422
    woops my bad its the right section.. I thought it said ubuntu.. (sorry :P I was mistaken with a diffrent one)

    Edit:
    Leeched:
    Code:
    /* Sample UDP server */
    
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <stdio.h>
    
    int main(int argc, char**argv)
    {
       int sockfd,n;
       struct sockaddr_in servaddr,cliaddr;
       socklen_t len;
       char mesg[1000];
    
       sockfd=socket(AF_INET,SOCK_DGRAM,0);
    
       bzero(&servaddr,sizeof(servaddr));
       servaddr.sin_family = AF_INET;
       servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
       servaddr.sin_port=htons(32000);
       bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
    
       for (;;)
       {
          len = sizeof(cliaddr);
          n = recvfrom(sockfd,mesg,1000,0,(struct sockaddr *)&cliaddr,&len);
          sendto(sockfd,mesg,n,0,(struct sockaddr *)&cliaddr,sizeof(cliaddr));
          printf("-------------------------------------------------------\n");
          mesg[n] = 0;
          printf("Received the following:\n");
          printf("%s",mesg);
          printf("-------------------------------------------------------\n");
       }
    }
    Does that help?
    New Users, please read this..
    Google first, then ask..

  5. #5
    Just Joined!
    Join Date
    May 2009
    Posts
    35
    unfortionatly no, as I stated before I already googled it, and came by a couple of examples on how to write a UDP server, my question is, how can we make a server handle only one client at a time, is there a function that does that?
    or does
    Code:
    bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
    solve this problem?

    This is my first time I am writing a server so if I ask a stupid question go easy on me

  6. #6
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    unless you specifically write your code to use threads, it will only serve one at a time

  7. #7
    Just Joined!
    Join Date
    May 2009
    Posts
    35
    Quote Originally Posted by coopstah13 View Post
    unless you specifically write your code to use threads, it will only serve one at a time
    are u sure? can't a UDP socket accept messages from different clients??

    I think I read that somewhere (or heard it from someone) but i am not sure

  8. #8
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    blocking socket can only read from one source at a time

  9. #9
    Just Joined!
    Join Date
    May 2009
    Posts
    35
    thanks to all who replied, i think i got it now =)

    just one last question, since i dont want to wast another post on it, what is best way in your opinion to deal with packet reordering?

    thank u in advance

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...