We help IT Professionals succeed at work.

Segmentation fault with sendto() function

lwinkenb
lwinkenb asked
on
767 Views
Last Modified: 2006-11-17
Can anyone tell me why I'm getting a seg fault on my sendto() call here?
Platform is linux.

int CSocket::sendUDP(char* pBuff, int bufflen, struct sockaddr* addr)
{
        if(!m_fsock)
        {
                return -1;
        }
        if(!pBuff)
        {
                printf("invalid pointer\n");
                return -1;
        }
        printf("File: %s To: %s Port: %d\n",
                pBuff,
                inet_ntoa(((struct sockaddr_in*)(addr))->sin_addr),
                ntohs(((struct sockaddr_in*)addr)->sin_port)
                );

        int hr;
        int on = 1;
        hr = setsockopt(m_fsock,SOL_SOCKET,SO_BROADCAST,&on,sizeof(on));
        if(hr == -1) return hr;

        hr = sendto(m_fsock,pBuff,bufflen,0,addr,sizeof(struct sockaddr)); // SEG FAULT HERE!!
        printf("returning...\n");
        return hr;
}
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2012
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

The only var  you don't test or use is "bufflen" can you test its value as well ?

Author

Commented:
I checked bufflen, and it is giving me '8' which is the length of the string I am passing in.  I'll give your other suggestion a shot jkr.

Author

Commented:
Unbelievable, it worked.  Why would that change make any difference?

Author

Commented:
Another strange thing is that if I change it back to the way it was before, it doesn't give me the seg fault anymore.  Does this make sense at all?
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
>>Why would that change make any difference?

'sizeof(<type>)' just is more appropriate than 'sizeof(<variable>)'

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.