LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 06-08-2009, 06:08 AM   #1
wagmare
Member
 
Registered: Jan 2009
Posts: 62

Rep: Reputation: 15
doubt in recv() of unix socket prog .


hi friends,
if the peer is down .. the send will get EPIPE error no after receiving
SIGPIPE ... but for recv it just returning 0 value ..

is it possible to get any error value or property of socket that the peer is closed on the other side ...
 
Old 06-08-2009, 05:47 PM   #2
stevexyz
LQ Newbie
 
Registered: Apr 2008
Location: Hampshire, UK
Distribution: None
Posts: 27

Rep: Reputation: 16
Hi Wagmare

Have a look at poll(2) - there's a POLLRDHUP bit returned which might be what you want.

Cheers, Steve
 
Old 06-09-2009, 05:03 AM   #3
wagmare
Member
 
Registered: Jan 2009
Posts: 62

Original Poster
Rep: Reputation: 15
poll() is not working ...

my server will send() msg in a while loop .. and i code my client as
Code:
#define _GNU_SOURCE
#include <poll.h>
 int s1;
        int rv, portNo;
        int buffsize = 1024;
        char *buf1 = malloc(buffsize);
        struct pollfd ufds[2];
         struct sockaddr_in serv_addr;


        portNo = atoi(argv[2]);

        s1 = socket(PF_INET, SOCK_STREAM, 0);

        bzero((struct sockaddr_in *) &serv_addr, sizeof(serv_addr));
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_port = htons(portNo);
        inet_pton(AF_INET, argv[1], &serv_addr.sin_addr);

        if(connect(s1, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0)
                perror("Connect");

        ufds[0].fd = s1;
        ufds[0].events = POLLIN | POLLRDHUP;     //check for normal or out of band
 //wait for event on the socket , 5 second timeout
        rv = poll(ufds, 2, 5000);
        while(1){
                if(rv == -1)
                        perror("poll");

                else if(rv == 0)
                        printf("Timeout error occured ! ..\n");

                else{
                        if(ufds[0].revents & POLLRDHUP){
                                printf("Stream socket peer closed connection, or shut down  writing  half of connection \n");
                                break;
                        }
                        //check for events on s1
                        if(ufds[0].revents & POLLIN) {
                                recv(s1, buf1, buffsize, 0); // receive normal data
                        }
                        if (ufds[0].revents & POLLPRI) {
                                recv(s1, buf1, sizeof buf1, MSG_OOB); // out-of-band data
                                printf("received out of band data ...\n");
                        }

                }
                printf("the message received:%s \n", buf1);
                sleep(1);
        }
        close(s1);
}
but it is not working ... can any one help me ... some suggestions ..
 
Old 06-10-2009, 04:01 AM   #4
stevexyz
LQ Newbie
 
Registered: Apr 2008
Location: Hampshire, UK
Distribution: None
Posts: 27

Rep: Reputation: 16
Hi Wagmare,

Please describe where it it failing. What doesn't work?

I had to use __USE_GNU rather than _GNU_SOURCE to get it to compile.

Cheers, Steve
 
Old 06-10-2009, 11:31 PM   #5
wagmare
Member
 
Registered: Jan 2009
Posts: 62

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by stevexyz View Post
Hi Wagmare,

Please describe where it it failing. What doesn't work?

I had to use __USE_GNU rather than _GNU_SOURCE to get it to compile.

Cheers, Steve
thanks for reply ....
the POLLRDHUP is not working but others POLLIN or POLLPRI POLLOUT anre working fine ...

i will try compiling with __USE_GNU now ... thanks for reply again ..
 
  


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
recv() and send() in socket lrios Programming 5 04-23-2008 11:32 AM
recv: Bad address (UNIX Socket programming) anamericanjoe Programming 1 12-13-2006 08:25 PM
recv() function of SOCKET ashucool83 Programming 1 10-08-2005 07:38 PM
doubt in pcap library (n/w prog) live_dont_exist Programming 0 05-10-2005 08:42 PM
function recv() in socket husniteja Programming 1 08-18-2004 09:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 10:55 AM.

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