LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-26-2013, 12:45 PM   #1
kikilinux
Member
 
Registered: Sep 2012
Posts: 125

Rep: Reputation: Disabled
c socket programming


hi
i have problem with c socket programming
i just wanna know how can i get the error of a function , for example the error of recv() function ,
what include is needed,
how to use error function in my program,
if it is possible show by an example

best
 
Old 05-26-2013, 01:58 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
The recv() function returns a value; when this value is less than zero, it denotes that an error has occurred. By examining errno, you can get a better idea as to the exact error.

For example:
Code:
#include <errno.h>     // for errno global value
#include <string.h>    // for strerror()
...

int bytesReceived = recv(sd, ...);

if (bytesReceived > 0)
{
    // received data
}
else if (bytesReceived == 0)
{
    // was the socket closed?
}
else
{
    fprintf(stderr, "Error receiving from socket; reason is %s [%d]\n", strerror(errno), errno);
}
 
2 members found this post helpful.
Old 05-27-2013, 01:01 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You'll find this useful http://beej.us/guide/bgnet/
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Lab 5: Socket Programming and Pthreads Programming teesha Linux - Newbie 2 02-27-2012 12:25 AM
Socket Programming jawadhashmi Programming 4 05-24-2005 03:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration