The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
socket programming pvamsikr UNIX for Advanced & Expert Users 3 11-17-2008 03:39 PM
Which application has a TCP socket open murphyboy UNIX for Dummies Questions & Answers 3 10-26-2005 06:29 AM
ftp application using socket programming toughguy2handle High Level Programming 1 09-23-2005 11:47 PM
Socket Programming socket soshell High Level Programming 2 06-29-2004 08:49 AM
Socket programming Nadeem Mistry High Level Programming 1 07-23-2001 05:33 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-05-2008
unsweety unsweety is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 3
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***************/

Code:
// 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****************/

Code:
#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****************/

Code:

// 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);	
}


Last edited by otheus; 02-03-2009 at 12:08 PM.. Reason: added [code] tags
  #2 (permalink)  
Old 02-03-2009
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,893
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).
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:29 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0