Error Handling In Socket Programming
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about how to handle multiple clients socket programming Stack Overflow the company Business Learn more about hiring developers or posting ads with how to handle multiple clients socket programming in java us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is python socket error handling a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up C socket recv() error handling up vote 0 down vote favorite I have a socket io error handling question about recv() function in socket (on linux Raspberri Pi) Why does my program stops at: if ((numbytes = recv(fd, odp, 100, 0)) == -1) { printf("\n error while test recv 1"); perror("recv"); reconnect = 1; } Yes, there is an error: "Resource remporarily unavaliable" When i see: printf("\n error while test recv 1"); i want to handle reconnect what is made later. But i see on terminal window that
Programming Error Handling Best Practices
my program stops on: error while test recv 1 I've tried with: signal(SIGPIPE, SIG_IGN); than with: signal(SIGPIPE, my_function); but it stops at either. Some code: int main(int argc, char *argv[]) { while(1) { if(reconnect){ close(fd); fd = createSocket(argv[1]); reconnect=0; } reconnect = connectionTest(fd); } int connectionTest(int *fd) { numbytes=send(fd, buf, 100,0); if ((numbytes = recv(fd, reply, 100, 0)) == -1) { /* HERE IT STOPS */ perror("recv"); printf("\n error while test recv 1"); reconnect = 1; } return reconnect; } int createSocket(char *server_addr){ int sockfd; struct addrinfo hints, *servinfo, *p; int rv; char s[INET6_ADDRSTRLEN]; int set = 1; signal(SIGPIPE, SIG_IGN); printf("connect to: %s", server_addr); memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; if ((rv = getaddrinfo(server_addr, PORT, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return 1; } // loop through all the results and connect to the first we can for(p = servinfo; p != NULL; p = p->ai_next) { if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("client: socket"); continue; } else printf("socketd created! \n"); int set = 1; setsockopt(sockfd, SOL_SOCKET, MSG_NOSIGNAL, (void *)&set, sizeof(int)); if (setsockopt( sockfd, SOL_SOCKET, SO_KEEPALIVE, (void *)&set, sizeof(int)) < 0 ) perror("setsockopt failed \n"); struct timeval timeout; timeout.tv_sec = 4; timeout.tv_usec = 0; if (setsockopt(sockfd, SOL_SOCKET,
Tips/Tricks Top Articles Beginner Articles Technical Blogs Posting/Update Guidelines Article Help Forum Article Competition Submit an article or tip Post your Blog quick answersQ&A Ask a Question View Unanswered Questions View All Questions... C# questions Linux questions ASP.NET
Error Handling In Programming Languages
questions SQL questions VB.NET questions discussionsforums All Message Boards... Application Lifecycle> Running a Business minecraft java socket exception error Sales / Marketing Collaboration / Beta Testing Work Issues Design and Architecture ASP.NET JavaScript C / C++ / MFC> ATL / socket exception c# WTL / STL Managed C++/CLI C# Free Tools Objective-C and Swift Database Hardware & Devices> System Admin Hosting and Servers Java .NET Framework Android iOS Mobile SharePoint Silverlight / WPF Visual Basic Web Development Site Bugs http://stackoverflow.com/questions/16039275/c-socket-recv-error-handling / Suggestions Spam and Abuse Watch features Competitions News The Insider Newsletter The Daily Build Newsletter Newsletter archive Surveys Product Showcase Research Library CodeProject Stuff communitylounge Who's Who Most Valuable Professionals The Lounge The Insider News The Weird & The Wonderful The Soapbox Press Releases Non-English Language > General Indian Topics General Chinese Topics help What is 'CodeProject'? General FAQ Ask a Question Bugs and Suggestions Article Help Forum Site Map http://www.codeproject.com/Questions/155292/Linux-socket-programming-Error-handling Advertise with us About our Advertising Employment Opportunities About Us Ask a Question All Questions All Unanswered FAQ Linux socket programming, Error handling Rate this: Please Sign up or sign in to vote. See more: Linux Sockets Hello, Can someone please look at the code below and tell me what I am missing here. Below is a simple tcp client code that opens a TCP connection with a server and sends a constant message in a loop. The code works fine as far as establishing the connection and sending the message. The problem I have is error handling: When the program starts running and if the connection is established, it starts sending the messages. I then purposely close the server in the middle before the client finishes all the messages to see how does it handle the error (check error code returned by send() function). I was surprised that after I shutdown the server program, the function returns success code for the first attempt after the server is closed. On the second attempt the client crashes (Linux terminates the program and I get the shell prompt back). Why does not send function return error code?. Why does Linux terminates the program?. How do I do a robust error checking then ?.
exists and that crashing due to exceptions is helpful education for you. trying to locate realistic examples often pushes you into complex examples using hundreds of lines http://www.digi.com/wiki/developer/index.php/Handling_Socket_Error_and_Keepalive of code to do real work. So here is a simple-dummy example https://en.wikibooks.org/wiki/C_Programming/Error_handling TCP client application which runs on either a PC or Digi product: It creates the socket It sets the socket timeout to 5.0 seconds, this means non-blocking and all requests will fault in 5.0 seconds unless they succeed. It tests and enables the TCP Keepalive - which by default is error handling OFF on most systems (Windows, Linux and Digi Python) This test is NOT required; it is here merely to show how it is done. It tries to open (connect) to a fixed IP address and TCP port 2101, which either succeeds rapidly - or fails in 5.0 seconds. "except socket.error" traps the error and causes the code to sleep 5 more seconds, then error handling in restart at socket creation. if the socket is open, it waits 5 seconds for up to 6 bytes of data, and throws an exception if socket errors or no data is received. This block has two-level error trapping: "except socket.timeout" traps the no-data error and loops up to try receiving again. Note that a robust program design would keep track of how frequently (or long) this 'no data' continues. In many designs, a TCP socket sitting idle for more than a few minutes might be best closed. "except socket.error" traps any remaining error, and exits the inner "while True:" and restarts creating the socket. To use this example, set the IP address to any Digi TS/DS with TCP Sockets active. Then by powering up or down the Digi TS/DS you can cause the connect(('x.x.x.x',2101) to succeed or fail. Sending simple ASCII data into the serial port of the Digi TS/DS would enable you to force or skip the "socket.timeout" try-except clause. TCP Keepalive So how does your Python code understand if no data means the TCP peer is being quiet - or if the TCP socket
known as exception handling). By convention, the programmer is expected to prevent errors from occurring in the first place, and test return values from functions. For example, -1 and NULL are used in several functions such as socket() (Unix socket programming) or malloc() respectively to indicate problems that the programmer should be aware about. In a worst case scenario where there is an unavoidable error and no way to recover from it, a C programmer usually tries to log the error and "gracefully" terminate the program. There is an external variable called "errno", accessible by the programs after including