![]() |
| | #1 |
| Registered User Join Date: Feb 2005
Posts: 30
| Problems with UDP sockets 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 | |
| | #2 |
| and the hat of Jobseeking 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() |
| Salem is offline | |
| | #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 | |
| | #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 | |
| | #5 |
| and the hat of Jobseeking 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? |
| Salem is offline | |
| | #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");
|
| majoub is offline | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #10 |
| and the hat of Jobseeking 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. |
| Salem is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |