Q: create second client to send received data from server Here the client1 and server are connected in UDP. Now client 1 sends data to server. Server receives data and sends acknowledgement to client 1. Now i want to send this received data in server to another client 2. 
Kindly suggest how to do it with this? what needs to be changed. 
Thank you in advance :)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h> 

void error(char *msg)
{
perror(msg);
exit(0);
}


void error(char *msg)
{
perror(msg);
exit(0);
}

int main(int argc, char *argv[])
{
    int sock, length, fromlen, n;
    struct sockaddr_in server;
    struct sockaddr_in from;
    char buf[1024];

    if (argc &lt; 2)
    {
        fprintf(stderr, "ERROR, no port provided\n");
        exit(0);
    }

    sock=socket(AF_INET, SOCK_DGRAM, 0);
    if (sock &lt; 0)
    {
    error("Opening socket");
    }
    length = sizeof(server);
    bzero(&server,length);
    server.sin_family=AF_INET;
    server.sin_addr.s_addr=INADDR_ANY;
    server.sin_port=htons(atoi(argv[1]));
    if (bind(sock,(struct sockaddr *)&server,length)&lt;0)
    {
        error("binding");
    }
    fromlen = sizeof(struct sockaddr_in);
    while (1)
    {
        n = recvfrom(sock,buf,1024,0,(struct sockaddr *)&from,&fromlen);
        if (n &lt; 0)
        {
            error("recvfrom");
        }
        write(1,"Received a datagram: ",21);
        write(1,buf,n);
        strcpy(data, buf);
        write(1,data,n);

        n = sendto(sock,"server received data\n",17,0,(struct sockaddr*)&from,fromlen);
        if (n &lt; 0)
        {
            error("sendto");
        }
    }
}


A: Think of it as proxy server; receive requests from clients and pass it to a server, receive response from the server and pass it to clients. You need two sockets, receive data from "client1" through socket1 and pass it "client2" through socket2. One thing to note here is that what you are calling "client2" is actually "server2" which receives data from server. Your "server" acts as a server for "client1" and acts as a "client" for "server2". It might look confusing, look it as a machine which work as a server on one end and as a client on the other end.
