Fedora Linux Support Community & Resources Center
  #1  
Old 24th November 2009, 08:07 AM
A_n_H Offline
Registered User
 
Join Date: Mar 2009
Posts: 64
windows_xp_2003firefox
UDP server not sending to client

Hi there,

I have a problem with my connected UDP server, I want to to send info to the client using send(), but for some reason I always get an error message such as: "bad file descriptor", any idea why?

here is a sample of my code:

Code:
int main(int argc, char *argv[]) 
{
	unsigned int localPort;
	int serverSock;
	int clientSock;
	int messageLength;
	unsigned int clientLen;
	unsigned char buffer[BUFFER_SIZE];
	unsigned char message[MESSAGE_SIZE];
	struct sockaddr_in serverAddr;
	struct sockaddr_in clientAddr;
	char* ptr;	
	FILE* file;



	localPort = strtol(argv[1], &ptr, 10);

	if ((serverSock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) 
	{
		printf("Failed to create socket\n");	
		exit(1);
	}

	memset(&serverAddr, 0, sizeof(serverAddr));   
	serverAddr.sin_family = AF_INET;             
	serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); 
	serverAddr.sin_port = htons(localPort);      
	clientAddr.sin_addr.s_addr = htonl(INADDR_ANY);

	if (bind(serverSock, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0) 
	{
	        printf("Failed to bind the server socket\n");
		exit(1);
	}

	while (1) 
	{
               //let the server connect to the client address so that only one client
              // maybe processed at a time
		clientLen = sizeof(clientAddr);
		if (connect(serverSock, (struct sockaddr *) &clientAddr, clientLen) < 0) 
		{
			perror("Failed to connect with server");
			exit(1);
			
		}
	
               //recv works gr8
		if ((bytes = recv(serverSock, buffer, BUFFER_SIZE-1, 0)) < 1 ) 
		{
			fprintf(stderr,"Failed to receive bytes from server\n");
					 
		}

		file = fopen(buffer, "r");

		if( file == NULL ) 
		{
			perror("stat");
			memset(message,0xff,1);
			
			messageLength = strlen(message);
		
                       //here is the problem I always get destination address required
			if ( send(serverSock,message,messageLength,0) != messageLength) 
			{
				perror("stat");
				exit(1);
			}
		}

            /*
                 more code here

         */
		clientAddr.sin_addr.s_addr = htonl(AF_UNSPEC);

	}

	exit(0);

}
Edit: Edit: anyone has any ideas please, ive been stuck on this problem for 2 days now

Last edited by A_n_H; 24th November 2009 at 09:35 PM.
Reply With Quote
Reply

Tags
client, sending, server, udp

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
setting up sendmail to use ISP server for sending mail homebase Servers & Networking 4 5th June 2008 05:43 AM
mail server and clients sending out spam ? pailott Security and Privacy 1 15th April 2008 03:40 PM
mail server not sending externally except root. cykill Servers & Networking 0 9th March 2007 11:45 PM
email server not sending out mails ... HELP!!! langakali Servers & Networking 10 24th January 2006 05:47 PM
Problem sending mail with postfix server... linux_fed2 Servers & Networking 5 12th November 2005 02:28 PM


Current GMT-time: 23:06 (Thursday, 18-12-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat
Creek Side - Stockholm-Arlanda Airport (ARN) Travel Photos on Instagram - Marina of Koper Travel Photos - Te Whiti Park Instagram Photos - Texas State Fair Photos on Instagram