udp chat server

This is a discussion on udp chat server within the Linux Programming forums, part of the Platform Specific Boards category; Hi everyone. i am trying to write udp chat server in c language. i have written tcp chat server before ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    2

    udp chat server

    Hi everyone. i am trying to write udp chat server in c language. i have written tcp chat server before and it is ok.

    in tcp server, accept function returns an fd number and server communicate with a specific client according to this fd number.

    but in udp, how can i listen a specific client?

    In my tcp server, after a client connect to server, a thread is created and it listen this client. So for each client, there is a thread that listen to according to fd number that is returned from accept function. so any message can send according to this fd number to specific client.

    How can i achieve this in udp server?

    Thanks for answers.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,767
    recvfrom
    recvfrom() also returns information about who sent the message. Knowing this, you can use sendto() to chat back to them.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    2
    Quote Originally Posted by Salem View Post
    recvfrom
    recvfrom() also returns information about who sent the message. Knowing this, you can use sendto() to chat back to them.
    it is ok but there is no problem with sendto. the problem is that, for example i want to receive data from only cliemt 1. so how can i achieve recvfrom works for a specified client.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    greece
    Posts
    13
    you can use a structure to save different data types for different clients.....and send it via send() and recieve via recv()....
    but you should have struct both to client and server.....(better in external header file.....)
    e.g:
    mystruct.h :
    Code:
    struct data{        
        int i;                 
        char info[50];
      }r1;

    at server side:

    Code:
    r1.i=10;
    
    n = send(sock, (void *) &r1, sizeof(r1),0);
       if (n < 0) perror("ERROR in send()");
    and at client side:
    Code:
    n = recv(sock, (void *) &r1, sizeof(r1),0);
    printf("i=%d",r1.i);

    To understand how udp sockets works look at this picture:
    Attached Images Attached Images  

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Might I add that it is extremely unwise to use UDP for a chat server. A chat server should be reliable and thus should use TCP, with only extremely rare exceptions.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    greece
    Posts
    13
    Quote Originally Posted by evoex View Post
    might i add that it is extremely unwise to use udp for a chat server. A chat server should be reliable and thus should use tcp, with only extremely rare exceptions.
    +100000!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. detect UDP server failure
    By trinli in forum Networking/Device Communication
    Replies: 10
    Last Post: 03-17-2009, 10:57 AM
  2. Chat udp server/client
    By Phoenix_Rebirth in forum C Programming
    Replies: 1
    Last Post: 11-17-2008, 12:30 PM
  3. problems with linux UDP server
    By Lopizda in forum Networking/Device Communication
    Replies: 5
    Last Post: 04-26-2007, 09:42 AM
  4. Multi-threaded UDP Server Problem
    By nkhambal in forum C Programming
    Replies: 0
    Last Post: 07-05-2005, 03:27 PM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 02:54 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21