tags:

views:

43

answers:

1

any one help me that in this matter. my program can not run. code to bind the socket which i write is:

char sendbuf[5]={"\0"};

unsigned int iMsgCode=1;
unsigned int iServiceID=3;
short int iAliveStatus=1;

memcpy(sendbuf,&iMsgCode,2);
memcpy(sendbuf+2,&iServiceID,2);
memcpy(sendbuf+4,&iAliveStatus,1);

char broadcast = '1';
int numbytes = 0;
    int sock;
struct sockaddr_in their_addr;

sock = socket(AF_INET,SOCK_DGRAM,0);

if(setsockopt(sock,SOL_SOCKET,SO_BROADCAST,&broadcast,sizeof(broadcast)) < 0)
{
    printf("Error in setting Broadcast option");
            return 0;
}//End if

their_addr.sin_family      = AF_INET;
//Target Port Number
their_addr.sin_port        = htons(iKeepAlivePort);
//Target IP
their_addr.sin_addr.s_addr = inet_addr (strKIP);
int sentbytes =0;

while(1)
{
    //sendto Fuction for sending data (Non-Standard)
    sentbytes = sendto(sock,sendbuf,5,0,(sockaddr*)&their_addr,sizeof(their_addr));

    if( sentbytes < 0)
    {
        printf("error in SENDTO() function");

                    close(sock);
        break;
    }//End if

    else

        printf("\n-------------------------------------");
        printf("\n KEEP ALIVE SEND AT PORT:%d AND IP:%s\n",iKeepAlivePort,strKIP);

                    sleep(1);
    }//End else


    close(sock);
    fflush(stdout);
A: 

Uhm... you should really be more specific about your problem. Is it a runtime problem? Is it a compile-time problem? What is the output?

Now, in a quick glance, I can see two issues:

First, you're missing a bracket, but I'll assume that's from copy-pasting.

Second, I'm pretty sure that in this part ...

(sockaddr*) &their_addr

... you should be doing this ...

(struct sockaddr*) &their_addr

... since sockaddr is a structure. Unless there's a typedef you haven't shown us, of course.

Santiago Lezica
The question is tagged both C and C++, which should trigger a few alarms. `(sockaddr*)` in C++ is legal, as it doesn't require you to repeat `struct`.
MSalters
Didn't see the tag, and from the code I assumed we were talking C. Thanks!
Santiago Lezica