C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 04-29-2005, 05:14 AM   #1
Registered User
 
Join Date: Feb 2005
Posts: 30
Problems with UDP sockets

hi,

It's the first time I use UDP socket so my questions might seem very basic...

I'm building a client server application using UDP protocols...


can anyone explain to me the recvfrom() function works? for example the first parameter is the client socket but how do I initialize the client socket since there is no accept() in UDP??
majoub is offline   Reply With Quote
Old 04-29-2005, 09:13 AM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,662
Useful Links And Good Books
I think it's socket() / bind() / recvfrom()
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 04-29-2005, 11:05 AM   #3
Registered User
 
Join Date: Feb 2005
Posts: 30
another question...

something really wierd is happeneing to my UDP server...I just can't open a socket...the socket descriptor keeps getting a negative value...

the funny thing is that the socket I opened in the client application works perfectly...

does this has something to do with the OS?? I'm using WIndows...
majoub is offline   Reply With Quote
Old 04-29-2005, 11:46 AM   #4
I am me, who else?
 
Join Date: Oct 2002
Posts: 250
You might want to post some of your code, especially the initialization.

Are you writing with MFC or straight win32? If MFC are you using CAsyncSockets? Furthermore are you wanting blocking or non-blocking sockets? All these sort of things would be helpful (well to me at least).

Have you bound the socket to a port yet?
dpro is offline   Reply With Quote
Old 04-29-2005, 11:58 AM   #5
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,662
> the socket descriptor keeps getting a negative value
Which means there is an error.
What error checking code have you put in, and what is it telling you?
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 04-29-2005, 05:27 PM   #6
Registered User
 
Join Date: Feb 2005
Posts: 30
ok here's one part of my code...
Code:
int main(int argc, char **argv)
{
	int sock;
	int port, queryCode, length;
	int clientSock;
	char message[260];
	struct sockaddr_in servSock_in;
	struct sockaddr_in client_in;
	char *serverIP;			//the IP of the FTP server
	char *hostname;
	
	
	debug = 0;  //debug is OFF by default
	port = 53000; 
	
	if(argc == 2)
	{
		if( strcmp(argv[1], "1") == 0 )
		{
			debug = 1;	//debug mode is ON
			printf("debug mode is ON\n\n");
		}
		
	}

	sock = socket(PF_INET, SOCK_DGRAM,0);   //the dns server socket

	if ( sock< 0)
	{
		printf("I am too tired... I failed to open gate...\n");
		return -1;
	}
	
	memset((char *)&servSock_in,0,sizeof(servSock_in));
	servSock_in.sin_family = PF_INET;
	servSock_in.sin_addr.s_addr = htonl(INADDR_ANY);
	servSock_in.sin_port = htons((u_short)port);


	//bind server
	if (bind(sock, (struct sockaddr *)&servSock_in, sizeof(servSock_in)) < 0)
	{
		printf("I couldn't attach gate to port...\n");
		return -1;
	}
	else
		printf("Waiting for messages...\n");
you asked me about blocking or non blocking sockets..actually I don't know the difference between them (i'm new in network programming..)
majoub is offline   Reply With Quote
Old 04-29-2005, 06:31 PM   #7
Registered User
 
Join Date: Feb 2005
Posts: 30
i'm sure that the code is OK but I keep on getting a negative descriptor here:
-> sock = socket(PF_INET, SOCK_DGRAM,0);

I'm using Windows visual C++... I made a link to wsock32.lb....
majoub is offline   Reply With Quote
Old 04-29-2005, 06:32 PM   #8
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 3,020
You have to call WSAStartup() before any other socket calls.
bithub is offline   Reply With Quote
Old 04-29-2005, 06:53 PM   #9
Registered User
 
Join Date: Feb 2005
Posts: 30
that's right!
I can't believe I forgot to do this!!
thanks a lot
majoub is offline   Reply With Quote
Old 04-30-2005, 12:25 AM   #10
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,662
Which, if you'd actually BOTHERED to look at some error information rather than the programming equivalent of *shrug*, you would have found out.

I won't be telling you to check for errors again.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple Client - Server problems (Async Sockets ) Cpp_Noob Networking/Device Communication 4 06-19-2009 07:12 AM
No clue how to make a code to solve problems! ctnzn C Programming 8 10-16-2008 02:59 AM
Best way to poll sockets? 39ster Networking/Device Communication 3 07-22-2008 01:43 PM
problems with linux UDP server Lopizda Networking/Device Communication 5 04-26-2007 08:42 AM
Traceroute using UDP + ICMP Unregistered Windows Programming 0 08-05-2002 10:50 AM


All times are GMT -6. The time now is 09:22 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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