Thread: Redirecting PrintF to a socket

  1. #1
    Registered User
    Join Date
    Aug 2019
    Posts
    7

    Question Redirecting PrintF to a socket

    Hi Everyone, I am finally on this forum. I registered back in 2018 and the registration bugged out and failed and the admins never got back to me so I generated a new account!

    Anyways, Welcome to be here. I have quite a large project where I have a Main.c that then includes a MySocket.H
    In this Socket.H it has all the handles to newly created sockets and functions. If Main.C does not include this Main.C has no socket code nor global handles to implement any socket functionality.

    When I include MySocket.h I then call my init command that kicks off the API and fills the GlobalVars in MySocket.H with active socket handle information.

    From my Main.C I then call functions in MySocket.H to send data through the socket.

    I then from my Main.C include another header for Debug information, we can call this MyDebug.H

    It works wounder, However in MyDebug.H I use PrintF which forwards information through a different method to pipe out to my console, Not the same as a NET socket.

    Now if from MyDebug.h I call the API's like Main.C would it would say that the function does not exist.

    What is the best way for MyDebug.h to call commands into Socket.h if and only if Socket.H is included in Main.C.

    My thoughts were that at the top of MyDebug.h I would put

    #IfDef SomeVar
    #Define FunctionToCall SocketSend
    #else
    #Define FunctionToCall OS_PrintF
    #endif

    and in Socket.H I would put #Define SomeVar then go and rename all my printfs to FunctionToCall (Making them require the same argument parameters and type).. But how do I get MyDebug.H to properly route the calls, Do I need an Extern function? How can I validate the Extern function exist before calling it. Now every project I include sockets (Aka MySockets.h).

    I hope this is a simple question. I appreciate the feedback!!
    -Agent
    Last edited by Keldecknight; 08-14-2019 at 02:57 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,078
    I suggest:
    • Make it such that socket.h can be included multiple times, anywhere.
    • Come up with an interface for the functions in mydebug.h
    • Implement the same interface in mydebugsocket.h, but you include socket.h here so as to use the socket interface
    • Conditionally include mydebug.h or mydebugsocket.h as needed, and likewise conditionally compile the source for the one that is included.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2019
    Posts
    7
    Quote Originally Posted by laserlight View Post
    I suggest:
    • Make it such that socket.h can be included multiple times, anywhere.
    • Come up with an interface for the functions in mydebug.h
    • Implement the same interface in mydebugsocket.h, but you include socket.h here so as to use the socket interface
    • Conditionally include mydebug.h or mydebugsocket.h as needed, and likewise conditionally compile the source for the one that is included.
    Thanks, So I'll need to research how to include the same file multiple times. I think the whole challenge of that part is redefinition correct?

    Do you have an example of Implementing an "Interface" I am uncertain is thats like a C99 term or if thats like if thats in the general meaning?

  4. #4
    Registered User
    Join Date
    May 2019
    Posts
    209
    I think the whole challenge of that part is redefinition correct?
    Since we have no code to study, we have to assume from your description that you have declared global variable in socket.h. If by "redefinition" you are referring to the fact that the linker may complain about multiple copies of such global variables, you're correct. The "extern" keyword may be useful, and to move the definition of those globals into a single translation unit (c file).

    Otherwise, the ability to include any "h" file is generally the notion that such a file does not cause the linker to complain about duplicates, and that the h file deals with including what it requires from the libraries it consumes.

    An interface is more in the line of a computer science idea, applicable to nearly any language. An example of a C interface can be found in several of the libraries. The commonly used family of functions fopen/fread/fwrite/fclose (and related) constitute an interface that can be used by any program by merely including the appropriate header files from the library, and linking with the required C run time as configured and required.

  5. #5
    Registered User
    Join Date
    Aug 2019
    Posts
    7
    I see, Thank you Niccolo for clearing that up. It helps me understand the context. Your right, I'll gather some simple snippet from my .H and .C and post a skewed down version here. It would be best to get the best guidance. Thanks for the two of you chiming in. I will return!

  6. #6
    Registered User
    Join Date
    Aug 2019
    Posts
    7
    Quote Originally Posted by Keldecknight View Post
    I see, Thank you Niccolo for clearing that up. It helps me understand the context. Your right, I'll gather some simple snippet from my .H and .C and post a skewed down version here. It would be best to get the best guidance. Thanks for the two of you chiming in. I will return!

    Hi Everyone, So here is an example of how I have my code setup. I was thinking this is the best way to go but want to confirm if there is a better way to do this more of a module vs. statically filling in the function in the definitions.

    Thanks!!

    Doing it this way requires otherheader to be defined last to correctly use the define.

    Main.c
    Code:
    #include socket.h
    #include otherheader.h
    
    int main(int arg)
    {
        initsocket
    }
    Socket.h
    Code:
    #define socket.h
    socket mysocket; //creates socket handle in Socket.h Global
    void initsocket()
    {
       //Allocates global socket "mysocket"
    }
    void senddata(char * buffer)
    {
       //Function sends buffer data out
    }
    otherheader.h
    Code:
    #ifdef socket.h
    #define functiontocall senddata
    #else
    #define functiontocall printf
    #endif
    void myfunc(void)
    {
        functiontocall("test");
    }
    Last edited by Keldecknight; 08-25-2019 at 06:21 PM.

  7. #7
    Registered User
    Join Date
    Aug 2019
    Posts
    7
    What about using "extern"? is there a way to implement that in such a way that if the function exist to call it?

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    3,654
    You do realize that GCC thinks both "Main.C" and "main.C" are C++ programs because of the ".C".

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,859
    Quote Originally Posted by stahta01 View Post
    You do realize that GCC thinks both "Main.C" and "main.C" are C++ programs because of the ".C".

    Tim S.
    I saw that and thought that it was possibly an auto-capital after the full stop when typing here

  10. #10
    Registered User
    Join Date
    Aug 2019
    Posts
    7
    Yes disregard that.

  11. #11
    Registered User
    Join Date
    Aug 2019
    Posts
    7
    Any assistance would be greatly appreciated *ThreadBump* Any additional details I can get for anyone to help with my goal?

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,421
    Is all the weird capitalisation something actually in your code we need to be paying attention to, or just your weird funky writing style?

    > What is the best way for MyDebug.h to call commands into Socket.h if and only if Socket.H is included in Main.C.
    I would say that code which changes its behaviour when you try to debug it is bad code.
    Debug should always be 'extra' behaviour (ie the debug information you want).
    Having 'different' behaviour just leads to unnecessary complications.

    And you talk about .h files "calling code".

    Consider this, you can map stdout to any open descriptor of your choosing.
    Code:
    $ cat foo.c
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    
    int main ( int argc, char *argv[] ) {
      int fd = 0;
      if ( argc > 1 ) {
        // a socket or a pipe would also work
        fd = open("my_other_stdout.txt",O_CREAT|O_WRONLY,0755);
        dup2(fd,STDOUT_FILENO);
      }
      printf("Hello world\n");
      if ( fd ) close(fd);
    }
    $ gcc foo.c
    $ ./a.out 
    Hello world
    $ ./a.out file
    $ cat my_other_stdout.txt 
    Hello world
    You seem to have 4 cases
    main -> printf goes to stdout
    main + socket -> printf goes to socket
    main + debug -> printf goes where ?
    main + socket + debug -> printf goes where?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Redirecting printf
    By mad-hatter in forum C Programming
    Replies: 3
    Last Post: 05-11-2015, 04:29 PM
  2. Redirecting stdout to socket
    By rancor in forum C Programming
    Replies: 9
    Last Post: 10-18-2008, 05:18 AM
  3. Redirecting I/O
    By suwie in forum C Programming
    Replies: 6
    Last Post: 09-30-2004, 09:53 PM
  4. redirecting stdout to a socket
    By Kinasz in forum Linux Programming
    Replies: 2
    Last Post: 03-25-2004, 08:01 AM
  5. Redirecting cin?
    By rafe in forum C++ Programming
    Replies: 4
    Last Post: 10-10-2002, 09:28 AM

Tags for this Thread