ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi,
you will know that your socket is close when you get SIGPIPE when you try to write (as you have discovered yourself ) or you will have a return value of 0 when you try to read from it. You should also know that when the socket is closed by the other side (or broken) there will be "data" to read, which will be the end of "file" (or connection in this case).
You can use a signal handler to catch the SIGPIPE signal.
The handler that you write for it will set a global flag to indicate that the socket is no longer valid.
Before writing to the socket, check this global flag. If an error is indicated, cleanup your client socket and reconnect.
As you now handle the SIGPIPE, your program will not 'crash'.
/* simple-client.c
*
* Copyright (c) 2000 Sean Walton and Macmillan Publishers. Use may be in
* whole or in part in accordance to the General Public License (GPL).
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*****************************************************************************/
/*** simple-client.c ***/
/*** ***/
/*****************************************************************************/
/**************************************************************************
* This is a simple client socket reader. It opens a socket, connects
* to a server, reads the message, and closes.
**************************************************************************/
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <resolv.h>
#define PORT_TIME 13 /* "time" (not available on RedHat) */
#define PORT_FTP 21 /* FTP connection port */
#define PORT_TEST 9999 /* connection port */
#define SERVER_ADDR "127.0.0.1" /* localhost */
#define MAXBUF 1024
int socket_OK=0;void sigpipe_handler()
{
printf("SIGPIPE caught\n");
socket_OK=0;
}
int main()
{
int sockfd;
struct sockaddr_in dest;
char buffer[MAXBUF];
int counter=0;
/*---Open socket for streaming---*/
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
{
perror("Socket");
exit(errno);
}
/*---Initialize server address/port struct---*/
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(PORT_TEST);
if ( inet_aton(SERVER_ADDR, &dest.sin_addr.s_addr) == 0 )
{
perror(SERVER_ADDR);
exit(errno);
}
// instal sigpipe handler
signal(SIGPIPE,sigpipe_handler);
/*---Connect to server---*/
if ( connect(sockfd, (struct sockaddr*)&dest, sizeof(dest)) != 0 )
{
perror("Connect ");
exit(errno);
}
// indicate that socket is OK
socket_OK=1;
while(1)
{
if(!socket_OK)
break;
send(sockfd, "test\n", 5, 0);
printf("message sent\n");
/*---Get "Hello?"---*/
bzero(buffer, MAXBUF);
recv(sockfd, buffer, sizeof(buffer), 0);
printf("[%6d]:%s", ++counter,buffer);
sleep(1);
}
/*---Clean up---*/
close(sockfd);
return 0;
}
It compiles with some warnings, but I did not feel like looking into that now.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.