Thread: Problems with UDP sockets

  1. #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??

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    http://cboard.cprogramming.com/showthread.php?t=41926
    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.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #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...

  4. #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?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #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..)

  7. #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....

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You have to call WSAStartup() before any other socket calls.

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    30
    that's right!
    I can't believe I forgot to do this!!
    thanks a lot

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

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