socket programming using udp for chat application

hi,
i have a source code for 1 server and 2 clients ...but the clients are not able to send data..1 server only receives data from clients and forwards to any other client, the data is in the buffer.....please help... thank you in advance.....

/**********client1***************/

// Here Data sending order goes like this.
// First Client 1 then client 2 has to send data to server for recognization.
// Then Client 2 has to start conversation then client1 , client2... 
// in this order data has to be sent
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#define MAXSIZE 25

main()
{

	struct sockaddr_in c_addr, s1_addr;
	int sockfd, rval, addr_len;
	char buff[MAXSIZE];

	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if (sockfd == -1)
	   printf("Error while socket creation");
	
	s1_addr.sin_family = AF_INET;
	s1_addr.sin_port = htons(7035);
	s1_addr.sin_addr.s_addr = inet_addr("127.0.0.1") ;
        
	gets(buff);
	
	rval = sendto(sockfd, buff, sizeof(buff), 0, (struct sockaddr *)&s1_addr, sizeof(s1_addr) );
	if (rval == -1)
	{   printf("While sending problem");close(sockfd);

        } 

	while(1)
	{  //printf("from Client 1");

	

	

	addr_len = sizeof(s1_addr);
  	rval = recvfrom(sockfd, buff, sizeof(buff), 0, (struct sockaddr *)&s1_addr, &addr_len );
	if (rval == -1)
	{   printf("While receving  problem");close(sockfd);}
	else
	   { puts("from server received  "); 
	     puts(buff);  }

	gets(buff);
	
	rval = sendto(sockfd, buff, sizeof(buff), 0, (struct sockaddr *)&s1_addr, sizeof(s1_addr) );
	if (rval == -1)
	{   printf("While sending problem");close(sockfd);

        } 	
	
        } // while end 

  close(sockfd);
}

/******************client2****************/

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#define MAXSIZE 25

main()
{

	struct sockaddr_in c_addr, s1_addr;
	int sockfd, rval, addr_len;
	char buff[MAXSIZE];

	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if (sockfd == -1)
	   printf("Error while socket creation");
	
	s1_addr.sin_family = AF_INET;
	s1_addr.sin_port = htons(7035);
	s1_addr.sin_addr.s_addr = inet_addr("127.0.0.1") ;

	gets(buff);
	
	rval = sendto(sockfd, buff, sizeof(buff), 0, (struct sockaddr *)&s1_addr, sizeof(s1_addr) );
	if (rval == -1)
	{   printf("While sending problem");close(sockfd);

        } 

	while(1)
	{  //printf("from Client 2");

	

	gets(buff);
	
	rval = sendto(sockfd, buff, sizeof(buff), 0, (struct sockaddr *)&s1_addr, sizeof(s1_addr) );
	if (rval == -1)
	{   printf("While sending problem");close(sockfd);

        } 

	addr_len = sizeof(s1_addr);
  	rval = recvfrom(sockfd, buff, sizeof(buff), 0, (struct sockaddr *)&s1_addr, &addr_len );
	if (rval == -1)
	{   printf("While receving  problem");close(sockfd);}
	else
	   { puts("from server received  "); 
	     puts(buff);  }	
	
        } // while end 

  close(sockfd);
}

/**************server****************/

// Here Data sending order goes like this.
// First Client 1 then client 2 has to send data to server for recognization.
// Then Client 2 has to start conversation then client1 , client2... 
// in this order data has to be sent.


#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#define MAXSIZE 25


main()
{

    struct sockaddr_in ss_addr, c_addr,c1_addr, c2_addr ;
    int sockfd, rval, addr_len, addr_len1;
    char buff[MAXSIZE], buff1[MAXSIZE]; 

    sockfd = socket(AF_INET, SOCK_DGRAM, 0); 
    if (sockfd == -1 )
	printf("socket(logical port) creation error");  	
    //printf("bef");		 	
    ss_addr.sin_family = AF_INET;
    ss_addr.sin_port = htons(7035);
    ss_addr.sin_addr.s_addr = htonl(INADDR_ANY);	   	
     
    rval = bind(sockfd, (struct sockaddr*) &ss_addr, sizeof(ss_addr)  );
    //printf("hHHHH");
    if (rval == -1)
	{ printf( "Binding error");close(sockfd);}
    else 
 	{
              // printf("in else");
               addr_len = sizeof(c_addr);
               addr_len1 = sizeof(c1_addr);
    rval = recvfrom(sockfd, buff, sizeof(buff), 0, (struct sockaddr*)&c_addr, &addr_len );
	
    if (rval == -1)
        printf("while recving from 1st problem.");

    puts(buff);

    rval = recvfrom(sockfd, buff1, sizeof(buff1), 0, (struct sockaddr*)&c1_addr, &addr_len1 );	
    puts(buff1);

    while(1)
    {	
   
     // For Client 2
     rval = recvfrom(sockfd, buff1, sizeof(buff1), 0, (struct sockaddr*)&c1_addr, &addr_len1 );	  
    if (rval == -1)
     {   printf("while recving from 2nd problem.");close(sockfd);}

     puts(buff1);
	
    rval = sendto(sockfd, buff1, sizeof(buff1), 0, (struct sockaddr*)&c_addr, addr_len );	
 
    if (rval == -1)
        printf("while sending from server to 1st client problem.");



  // For Client 1

 

 rval = recvfrom(sockfd, buff, sizeof(buff), 0, (struct sockaddr*)&c_addr, &addr_len );	
    if (rval == -1)
    {    printf("while recving from 1st problem.");close(sockfd);}
     puts(buff);


 rval = sendto(sockfd, buff, sizeof(buff), 0, (struct sockaddr*)&c1_addr, addr_len1 );	

     
    if (rval == -1)
    {    printf("while sending from server to 2nd client problem.");close(sockfd);}

   if (rval == -1)
   { printf("breaking from loop ");close(sockfd);	
     break; 
   }  


  }  // while end 

 // printf("at end"); 
   // puts(buff);			
}
close(sockfd);	
}

Hi there. I think you are confused about something. The server (as it is written) cannot distinguish between "client1" and "client2". So whichever client you start first becomes "client1". The value of c1 and of c depend on which client was talking to the server when the recvfrom() is called.

In order to do this right, I think, you have to build an array of clients, each of which you get after a call to recvfrom(). Then you write to all the clients in that array (except the one that matches the current one).