Thread: Redirecting PrintF to a socket

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

    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; 5 Days Ago at 02:57 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,937
    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
    3
    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
    200
    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
    3
    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!

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