Sockets on Unix

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anonymous

    #1

    Sockets on Unix

    Hi Folks,

    Perhaps someone knows the answer to this. I wrote a small socket app on
    fedora linux that simply loops and sends data until the connection is lost
    (by the remote peer). I look for a lost connection by waiting for send to
    return -1 or 0; however, what seems to be happening is that the application
    crashes (no core file or anything) before I get a -1. Some sample code ...
    if I connect and disconnect before the 3 seconds is up (note the sleep(3)),
    then the output is something along the lines of :

    **OUTPUT***
    send 1: 2
    errno 1: 0

    If I connect and wait to receive the data then the output is:

    ***OUTPUT***
    send 1: 2
    errno 1: 0
    send 2: 2
    errno 2: 0
    Hello world!

    So it looks like the program is crashing on the second send in the first
    scenario; however, I have no core file or any indication that it's anything
    other than the end of the program. Could someone help?

    ***CODE***
    int main(int argc, char *argv[]) {

    // open

    int fd = socket(AF_INET, SOCK_STREAM, 0);



    // bind

    struct sockaddr_in address;

    memset(&address , 0, sizeof(address) );

    address.sin_fam ily=AF_INET;

    address.sin_por t=htons(1025);

    address.sin_add r.s_addr=htonl( INADDR_ANY);

    bind(fd, (struct sockaddr*)&addr ess, sizeof(address) );



    //listen

    listen(fd, 16);



    // accept

    unsigned int n = sizeof(sockaddr );

    sockaddr_in clientAddress;

    int cfd = accept(fd, (sockaddr*)&cli entAddress, &n);



    sleep(3);



    char buffer[512];

    cout << "send 1: " << send(cfd, buffer, 2, 0) << endl;

    cout << "errno 1: " << errno << endl;

    cout << "send 2: " << send(cfd, buffer, 2, 0) << endl;

    cout << "errno 2: " << errno << endl;



    cout << "Hello world!\n";



    close(fd);



    return 0;

    }


  • Thomas J. Gritzan

    #2
    Re: Sockets on Unix

    Anonymous schrieb:
    Hi Folks,
    >
    Perhaps someone knows the answer to this. I wrote a small socket app on
    fedora linux that simply loops and sends data until the connection is lost
    (by the remote peer). I look for a lost connection by waiting for send to
    return -1 or 0; however, what seems to be happening is that the application
    crashes (no core file or anything) before I get a -1.
    [...]

    A crash? What a crash? A segfault? A bluescreen?

    However, nothing of this has to do with C++. Read here:



    --
    Thomas

    Comment

    • Anonymous

      #3
      Re: Sockets on Unix

      To be honest, I'm not sure. From my perspective I see the program running
      and then, all of a sudden, I see the command prompt. When I have a seg
      fault I normally see "SEGMENTATI ON FAULT" before seeing the command prompt
      again, but here it just drops to the command prompt, just as it does in the
      second case, except I get the additional printouts before it does.


      "Thomas J. Gritzan" <Phygon_ANTISPA M@gmx.dewrote in message
      news:egub2d$snl $1@newsreader2. netcologne.de.. .
      Anonymous schrieb:
      >Hi Folks,
      >>
      >Perhaps someone knows the answer to this. I wrote a small socket app on
      >fedora linux that simply loops and sends data until the connection is
      >lost
      >(by the remote peer). I look for a lost connection by waiting for send
      >to
      >return -1 or 0; however, what seems to be happening is that the
      >application
      >crashes (no core file or anything) before I get a -1.
      [...]
      >
      A crash? What a crash? A segfault? A bluescreen?
      >
      However, nothing of this has to do with C++. Read here:
      >

      >
      --
      Thomas
      http://www.netmeister.org/news/learn2quote.html

      Comment

      • Larry Smith

        #4
        Re: Sockets on Unix

        Anonymous wrote:
        Hi Folks,
        >
        Perhaps someone knows the answer to this. I wrote a small socket app on
        fedora linux that simply loops and sends data until the connection is lost
        (by the remote peer). I look for a lost connection by waiting for send to
        return -1 or 0; however, what seems to be happening is that the application
        crashes (no core file or anything) before I get a -1. Some sample code ...
        if I connect and disconnect before the 3 seconds is up (note the sleep(3)),
        then the output is something along the lines of :
        >
        **OUTPUT***
        send 1: 2
        errno 1: 0
        >
        If I connect and wait to receive the data then the output is:
        >
        ***OUTPUT***
        send 1: 2
        errno 1: 0
        send 2: 2
        errno 2: 0
        Hello world!
        >
        So it looks like the program is crashing on the second send in the first
        scenario; however, I have no core file or any indication that it's anything
        other than the end of the program. Could someone help?
        >
        ***CODE***
        int main(int argc, char *argv[]) {
        >
        // open
        >
        int fd = socket(AF_INET, SOCK_STREAM, 0);
        >
        >
        >
        // bind
        >
        struct sockaddr_in address;
        >
        memset(&address , 0, sizeof(address) );
        >
        address.sin_fam ily=AF_INET;
        >
        address.sin_por t=htons(1025);
        >
        address.sin_add r.s_addr=htonl( INADDR_ANY);
        >
        bind(fd, (struct sockaddr*)&addr ess, sizeof(address) );
        >
        >
        >
        //listen
        >
        listen(fd, 16);
        >
        >
        >
        // accept
        >
        unsigned int n = sizeof(sockaddr );
        >
        sockaddr_in clientAddress;
        >
        int cfd = accept(fd, (sockaddr*)&cli entAddress, &n);
        >
        >
        >
        sleep(3);
        >
        >
        >
        char buffer[512];
        >
        cout << "send 1: " << send(cfd, buffer, 2, 0) << endl;
        >
        cout << "errno 1: " << errno << endl;
        >
        cout << "send 2: " << send(cfd, buffer, 2, 0) << endl;
        >
        cout << "errno 2: " << errno << endl;
        >
        >
        >
        cout << "Hello world!\n";
        >
        >
        >
        close(fd);
        >
        >
        >
        return 0;
        >
        }
        >
        >
        Try one of these newsgroups:

        comp.os.linux.d evelopment.apps
        comp.os.linux.d evelopment.syst em

        Comment

        • Mehturt@gmail.com

          #5
          Re: Sockets on Unix


          Anonymous wrote:
          To be honest, I'm not sure. From my perspective I see the program running
          and then, all of a sudden, I see the command prompt. When I have a seg
          fault I normally see "SEGMENTATI ON FAULT" before seeing the command prompt
          again, but here it just drops to the command prompt, just as it does in the
          second case, except I get the additional printouts before it does.
          >
          >
          You can try to use http://nnl.sf.net
          m.

          Comment

          Working...