Results 1 to 18 of 18

Thread: simple question

  1. #1

    Thread Starter
    Addicted Member FrEaK85's Avatar
    Join Date
    Apr 2002
    Posts
    153

    Arrow simple question

    Hi,

    I'm creating a c++ class for to easily handle multiple connections with sockets. I'd like to work with some kind of events (I'm a vb programmer ) . My class will use a do loop to check for new connections / new messages (non blocking server). Can I possibly pass a pointer to a function from with the actual code to my class. And then call the sub from within my class.

    What I'm trying to say is. I want to call a sub in the code where my class is used. But I don't know wich sub yet. So the user has to tell me (with a function possibly) wich function to call when a new connection is made for example.

    Thanks,

    Walter Brebels

  2. #2
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    If your program is non blocking then why are you looping for new connections? All you have to do is set it up to do a Window Message on connection and disconnection. You can also assign other messages to functions, then you can just call the coresponding class and function.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  3. #3

    Thread Starter
    Addicted Member FrEaK85's Avatar
    Join Date
    Apr 2002
    Posts
    153
    Ok, so I suppose its not a non blocking server . It uses the select function to handle multiple connections.

    here you can see the tutorial for it (i'm new to c++):
    http://www.gamedev.net/reference/art...rticle1494.asp

    Any idea's?

    Thanks

  4. #4
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    AH, I see, this isn't a Windows program persay that's why they aren't using windows messaging to handle the events. But the basic idea is still the same. Where they have:

    PHP Code:
    switch (buffer[0])
      {
      default:
        
    Disconnect(s);
        
    cout << "This Client Has Disconnected: " << ClientIp << endl;
        break;
      case 
    1:
        
    // DO SOMETHING IF YOU RECEIVE PACKET 1
        
    break; 
      case 
    2:
        
    // DO SOMETHING IF YOU RECEIVE PACKET 2
        
    break;
      }

    Is where you can call the class and function you need. An example would be lets say you setup a message called MSG_DOWNLOAD which has 1 assigned to it. When you send this message over the wire, it would fire in this the case event.

    So using the previous code it would go something like this:
    PHP Code:
    switch (buffer[0])
      {
      default:
        
    Disconnect(s);
        
    cout << "This Client Has Disconnected: " << ClientIp << endl;
        break;
      case 
    MSG_DOWNLOAD:
        
    CSocket.Port_Connect(IP,PORT);
        
    CSocket.Download(sDIR,sFILE_NAME);
        break; 
      case 
    2:
        
    // DO SOMETHING IF YOU RECEIVE PACKET 2
        
    break;
      }

    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  5. #5

    Thread Starter
    Addicted Member FrEaK85's Avatar
    Join Date
    Apr 2002
    Posts
    153
    Ok, now I don't understand a thing anymore .
    1st: I taught buffer[] containt the recieved message. (Am I Right).

    PHP Code:
    switch (buffer[0])
      {
      default:
        
    Disconnect(s);
        
    cout << "This Client Has Disconnected: " << ClientIp << endl;
        break;
      case 
    1:
        
    // Here i taught that the user has send "1" to my server. So now i want to call a function in the code that uses my class. With as argument buffer[]

    I just want to pass the recieved data to a function in the code that uses my class.

    For example, i don't think this is right but i'll give it a try:

    Code that uses my class:

    bool RecFunction(char Data)
    {
    }

    MyClass.Function(&RecFunction)

    Then in my class when i recieve a message i want to call that RecFunction when I recieved a message.

  6. #6
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Ok let me see if I can get this strait.

    What you are after is that a message has been caught that says your program is now going to receive data. Now you want to take the data that is comming in and send it to a function to put it some where. Is this what you are after?
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  7. #7

    Thread Starter
    Addicted Member FrEaK85's Avatar
    Join Date
    Apr 2002
    Posts
    153
    I just recieve a message, doesn't matter wich and all i need to do is pass that message to a function.
    But...
    I'm making a class and in that class i recieve the message. And i need to pass that message to a function in the code that uses my class.

    Do you know vb? In there they are events.
    Visual Baisc example:
    Private Sub Winsock1_DataArrival(Data As String)
    'This sub is triggered everytime the client sends data to Winsock1
    End Sub

    I need something like this in C++

  8. #8
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Ok I get it.

    There a problem, the tut you are using because its a bit basic. The message receive buffer:

    PHP Code:
    // Recieve The Data and Put into a Buffer Until we know what it is.
        
    char buffer[1];
        
    recv(Client[s].ClientSocket,buffer,1,0); 
    Is really small. The it is the size of 1 char, so you would be limited in what data & messages you could receive.

    All you would need to do is remove the switch buffer part, then pass the buffer to your class and function. Thats all.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  9. #9

    Thread Starter
    Addicted Member FrEaK85's Avatar
    Join Date
    Apr 2002
    Posts
    153
    I know that

    What i don't know is, how to pass the data from within my class to:
    a function in the code that uses my class.

  10. #10
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Ok....I guess I still don't get it.

    So your message/data comes in. You get it in the buffer var. You then pass it to a function in your class that handles the messages. ie CSocket.Message(*buffer);

    What you want to know is how do you pass it to another function in your class? Another function not in your class?

    Maybe you could give me a more specific example with psuedo code or something
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  11. #11

    Thread Starter
    Addicted Member FrEaK85's Avatar
    Join Date
    Apr 2002
    Posts
    153
    No, thats just it I don't want to pass the data to a function IN my class. I want to pass the data to a function THAT ISN'T in my class.





    For example the name of my class = MyClass
    It has 1 function: SetFunction()


    Now the user wants to use my class, the users code:

    #include <Myclass.h>

    int ParseData(char Data)
    {
    // parse the data
    }

    void main()
    {
    SetFunction()
    }

    What i want to do is:
    I want to call the "ParseData" function from my class when i recieve data. And with the "SetFunction" function i want to tell my class that it is the "ParseData" function that I should call.

    You can compare it with multithreading. You say to CreateThread function to call a function by passing a pointer to that function.

    Is it making any sense to you?

  12. #12
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Phew, man so much for this being a simple question. I guess either I need to go back to bed, or maybe you are making this sound to complicated. If you want to call a function from either your main cpp or from a class that is not in the same cpp then all you have to do is make it globally accessable. There are a couple of ways to do that.

    Personally I would put it in a header file. So if you want it for main.cpp, make a main.h

    Here is a quick one:

    PHP Code:
    #ifndef __MAIN_H__
    #define __MAIN_H__

    /*---------------------------------*/
                    //Libs

    /*---------------------------------*/
                    //Include

    #include <iostream.h> // Input and Output Stuff, needed for cout and such.
    #include <winsock.h> // Needed to use Winsock

    /*---------------------------------*/
                    //Define

    #define PORT 4000 // The Port Number of the Server
    #define Max_Connections 10 // The Maximum Number of Connections allowed to connect

    #define YES 1 // Making 1 as YES, so its easier to understand our code.
    #define NO 2  // Making 2 as NO, so its easier to understand our code.

    /*---------------------------------*/
                    //Global Functions

    int ParseData(char Data) ;

    /*---------------------------------*/
                    //Global VARs

    /*---------------------------------*/

    #endif // MAIN_H 
    Now in your main.cpp and your Myclass.h

    Just add #include "Main.h"

    I hope this is what you are looking for.
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  13. #13

    Thread Starter
    Addicted Member FrEaK85's Avatar
    Join Date
    Apr 2002
    Posts
    153

    Ok, simplify time :)

    A new start, i think i've said some things wrong

    I am creating a dll to simplify the use of multiple connections with winsock.

    It has 3 main public functions:
    - void Setup()
    - bool SetFunction( *Function1) or something like that
    - void Shutdown()

    ------------------------------------------------

    when the user has in its code:

    void Function1()
    {
    //blabla1
    }

    and he does in its main function:

    void main()
    {
    SetFunction(&Function1);
    }

    -------------------------------------------------

    Then when recieve data in my class:
    void GetMessage()
    {
    .... recieved data
    // Now I want to call that function 1
    .... go on with code
    }

    ----------------------------------------------------

    The problem is the GetMessage function is in my dll. And the Function1 is a piece of code that uses my dll


    Thanks,

    Walter Brebels

  14. #14
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Ok now THIS makes sense. Now that we are talking DLLs its more clear.

    Now that we have gone through all of this I feel bad because I am not sure I can help. I have never used function pointers before. You might search the forum because I seem to remember this topic coming up before. These tutorial's might also help

    http://www.gametutorials.com/Tutorials/C++/Cpp_Pg7.htm
    Bottom of the page

    You can also call DLL based function in a program just like in VB. Again I am not sure how to do that. Again check the about tut
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  15. #15
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Code:
    // the function pointer must know which type of function it points to
    // for example the WNDPROC pointer is defined like this:
    typedef LRESULT (CALLBACK* WNDPROC)(HWND, UINT, WPARAM, LPARAM);
    // the brackets around the function name are necessary.
    // Function pointers don't need the typedefs, so this could happen:
    // global function pointer or function?
    int *FuncPtr(int);
    // This is interpreted as a function named FuncPtr with int* return type
    
    // this is a pointer named FuncPtr to functions with int return type:
    int (*FuncPtr)(int);
    // This is a little bit complicated, this is why there are typedefs like above
    // Now it's easy to declare a pointer to a WNDPROC
    WNDPROC pfnWndProc;
    // such a pointer is a member of the WNDCLASS and WNDCLASSEX structs
    
    // to assign a value to a function pointer, use the function name without a parameter list.
    pfnWndProc = MyWndProc;
    
    // and to call:
    FuncPtr = some_func;
    int i = FuncPtr(23);
    // some people prefer to write
    int i = (*FuncPtr)(23);
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  16. #16
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    I knew once you got here, you would have the answer
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  17. #17

    Thread Starter
    Addicted Member FrEaK85's Avatar
    Join Date
    Apr 2002
    Posts
    153
    So let me get this straight:

    Users code:
    Code:
    #include <MyClass.h>
    
    void DataArrival(int SocketIndex, char Data)
    {
         ...
    }
    
    
    void main()
    {
          SetFunction(DataArrival);
    }
    ----------------------------------------------------

    DLL Code:
    Code:
    void *ptrDataFunc(int, char);
    
    void SetFunction(&Function)
    {
           ptrDataFunc = Function;
    }
    Would something like this work???

    Thanks for all your help.

  18. #18
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Code:
    void (*ptrDataFunc)(int, char);
    
    void SetFunction(void (*Function)(int, char))
    {
           ptrDataFunc = Function;
    }
    This will work. But better would be
    Code:
    typedef void (*SOCKRECIEVER)(int, char);
    
    SOCKRECIEVER ptrDataFunc;
    
    void SetFunction(SOCKRECIEVER Function)
    {
           ptrDataFunc = Function;
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width