Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Socket main() working, Class is not

Socket main() working, Class is not

Scheduled Pinned Locked Moved C / C++ / MFC
sysadminhelpc++htmlcom
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gvanto
    wrote on last edited by
    #1

    I've been following Beej's Guide to network programming, (which is really great) and when implementing his streaming socket server ( http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#simpleserver[^] ) it works just fine. To test it I opened a Konsole (using Kubuntu) and ran: pacific@mainbox:~$ telnet localhost 3490 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Hello, world!Connection closed by foreign host. However, when trying to make a class out of the above code, as shown below, I get a "accept: Bad file descriptor" error, repeatedly. Not sure if these two functions (which are not part of the class) have anything to do with it: void sigchld_handler(int s); void *get_in_addr(struct sockaddr *sa); Help on this would be much appreciated !! Gvanto Sources also here: http://www.sharedigest.com/socketserver.tar.gz[^] main (WLFeeder.cpp): #include #include #include #include #include #include #include #include #include #include "SocketServer.h" #define DO_MAIN 1 using namespace std; using namespace wlns; #ifdef DO_MAIN int main(int argc, char *argv[]) { SocketServer *s = new SocketServer(); if( s->initAddrInfo() != SUCCESS ) { cout << "Error initializing address " << endl; exit(1); } if( s->initBind() != SUCCESS ) { cout << "Error initializing bind " << endl; exit(1); } if( s->initListen() != SUCCESS ) { cout << "Error initializing listen " << endl; exit(1); } if( s->clearDeadProcesses() != SUCCESS ) { cout << "Error clearing dead processes " << endl; exit(1); } if( s->listenForConnections() != SUCCESS ) { cout << "Error in listening for connections " << endl; exit(1); } return 0; } #endif SocketServer.h: /* * SocketServer.h * * Created on: 11/02/2009 * Author: pacific * * Generic, extendable socket-managing class handling * network communications via sockets */ #ifndef SOCKETSERVER_H_ #define SOCKETSERVER_H_ #in

    S 1 Reply Last reply
    0
    • G gvanto

      I've been following Beej's Guide to network programming, (which is really great) and when implementing his streaming socket server ( http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#simpleserver[^] ) it works just fine. To test it I opened a Konsole (using Kubuntu) and ran: pacific@mainbox:~$ telnet localhost 3490 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Hello, world!Connection closed by foreign host. However, when trying to make a class out of the above code, as shown below, I get a "accept: Bad file descriptor" error, repeatedly. Not sure if these two functions (which are not part of the class) have anything to do with it: void sigchld_handler(int s); void *get_in_addr(struct sockaddr *sa); Help on this would be much appreciated !! Gvanto Sources also here: http://www.sharedigest.com/socketserver.tar.gz[^] main (WLFeeder.cpp): #include #include #include #include #include #include #include #include #include #include "SocketServer.h" #define DO_MAIN 1 using namespace std; using namespace wlns; #ifdef DO_MAIN int main(int argc, char *argv[]) { SocketServer *s = new SocketServer(); if( s->initAddrInfo() != SUCCESS ) { cout << "Error initializing address " << endl; exit(1); } if( s->initBind() != SUCCESS ) { cout << "Error initializing bind " << endl; exit(1); } if( s->initListen() != SUCCESS ) { cout << "Error initializing listen " << endl; exit(1); } if( s->clearDeadProcesses() != SUCCESS ) { cout << "Error clearing dead processes " << endl; exit(1); } if( s->listenForConnections() != SUCCESS ) { cout << "Error in listening for connections " << endl; exit(1); } return 0; } #endif SocketServer.h: /* * SocketServer.h * * Created on: 11/02/2009 * Author: pacific * * Generic, extendable socket-managing class handling * network communications via sockets */ #ifndef SOCKETSERVER_H_ #define SOCKETSERVER_H_ #in

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      The one thing I notice is that your forked process doesn't exit (unlike the equivalent code in Beej's guide)?

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      G 1 Reply Last reply
      0
      • S Stuart Dootson

        The one thing I notice is that your forked process doesn't exit (unlike the equivalent code in Beej's guide)?

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        G Offline
        G Offline
        gvanto
        wrote on last edited by
        #3

        Thanks a million Stuart, that did it ! I thought exit(0) was only to exit from a main routine or something, did not realize it would exit from the if( ) { } routine ... Gvanto

        Find Your Dream IT Job in Australia, Free! http://www.WebCV.com.au

        S 1 Reply Last reply
        0
        • G gvanto

          Thanks a million Stuart, that did it ! I thought exit(0) was only to exit from a main routine or something, did not realize it would exit from the if( ) { } routine ... Gvanto

          Find Your Dream IT Job in Australia, Free! http://www.WebCV.com.au

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          gvanto wrote:

          exit(0) was only to exit from a main routine or something

          No, it exits the current process - of course, which process is 'current' can get confusing with *nix, as the fork function creates a new process by duplicating the caller, so you have to track the two code paths, the parent process (where fork returns the child PID) and the child process (wehere fork returns zero). In some ways it's easier to work with than Win32's CreateProcess, in other ways not.

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          G 1 Reply Last reply
          0
          • S Stuart Dootson

            gvanto wrote:

            exit(0) was only to exit from a main routine or something

            No, it exits the current process - of course, which process is 'current' can get confusing with *nix, as the fork function creates a new process by duplicating the caller, so you have to track the two code paths, the parent process (where fork returns the child PID) and the child process (wehere fork returns zero). In some ways it's easier to work with than Win32's CreateProcess, in other ways not.

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            G Offline
            G Offline
            gvanto
            wrote on last edited by
            #5

            Ah ic, yeah that's what kind of got me I think. Not quite got the hang of these socket processes (and I'm pretty new to C++) so still got a way to go. and a loooooooooong way before I start coding c++ in windows ! :-) Thanks again for the help Stuart, much appreciated ! Best, gvanto

            Find Your Dream Job in Australia, Free! http://www.WebCV.com.au

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups