Advertisement

UDP and recieving Broadcasts

Started by April 07, 2004 05:03 PM
0 comments, last by obj63 20 years, 10 months ago
I am writing a program that is supposed to recieve broadcast messages from a server that I wrote. I know that the broadcasting is working correctly. Also as a specification of the client only the ip address for the server (unicast address) is specified. The problem that I am having is that the client can not recieve the mesages from the server. Here is my code. if anyone can see why this isn''t working thanks sendSock = socket(AF_INET,SOCK_DGRAM,0); recvSock = socket(AF_INET,SOCK_DGRAM,0); int on = 1; setsockopt(recvSock, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)); //get the host name of the server host = gethostbyname(argv[1]); if(host == NULL) { cerr<<"Unknown Host "<h_addr, host->h_length); servAddr.sin_port = htons(atoi(argv[2])); broadAddr.sin_family = AF_INET; broadAddr.sin_port = htons(atoi(argv[2])); broadAddr.sin_addr.s_addr = htonl(INADDR_BROADCAST); cout<<"Type ''quit'' to exit the program\n\n\n"; bind(recvSock, (struct sockaddr *) &broadAddr, sizeof(broadAddr)); while(1) { FD_ZERO(&mySet); FD_SET(0, &mySet); FD_SET(recvSock, &mySet); while(select((MAX(0, recvSock) + 1), &mySet , NULL, NULL, NULL)<0) ; if(FD_ISSET(recvSock, &mySet)) { cout<<"c_here\n\n"; memset(buf, 0x0, MAXBUFSIZE); cout<<"\n\n===============BEGIN FORTUNE==========================\n"; //now recieve fortune from the server if ''\r\n'' //then it is done sending while(1) { int n = recvfrom(recvSock, buf, MAXBUFSIZE, 0, NULL, NULL); if((buf[n-2] == ''\r'')&&(buf[n-1] == ''\n'')) break; cout<<buf; memset(buf, 0x0, MAXBUFSIZE); } cout<<endl <<"\n\n==============END FORTUNE=============================\n"; } every thing else below this works fine Thanks again Joe
bind() binds to LOCAL interfaces. Thus, you should bind the receiving socket to INADDR_ANY / your-port. It's the server that should send to BROADCAST (which is 0xffffffff).


[edited by - hplus0603 on April 7, 2004 8:44:28 PM]
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement