CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2005
    Posts
    63

    Communication between a C++ and C# program?

    I have written a very extensive game in (unmanaged) C++, using the console. I'm also currently enrolled in an Internet Programming class at my university, and after writing my own HTTP server in java, I started thinking about making my game net-enabled and visual using C# and VS2005.

    The way that a player interacts with the game in console mode is simple: when it's the player's turn to act (there are AI opponents also playing), the game outputs the current state of the game (about 15 lines of text), and the user types in 1 of 3 action commands and an optional argument.

    The obvious problem for me was: how do I get the data which would normally be presented to the player via the C++ program's console window, to the C# server and then on to the C# client? And once that was done, I had to do the same thing but reversed to get the action message back to the server and then back to the C++ game.

    My solution was to have the same information passed via text files, using a handshake protocol. What I would do is have 2 text files per player, a state file and action file, and each player would have their own handshake integer. So the flow of the game goes a little something like this:

    Code:
    C++ Program (aka Backend): It's playerA's turn.
    Open state file.
    Output the current hand shake value as the first line of the state file, followed by the state text.
    Close the state file and open up the Action file.
    Read in the first line of the Action file and store it as newHandshake
    wait until newHandshake > current handShake (the one output to the state file)
    
    C# Server (aka Server): Open state file and read in handshake.
    Send state text to client.
    Close state text.
    Wait for response from client.
    
    C# Client (aka Client): Receive state text, convert to graphical representation, and wait for user action.
    
    User: click action A.
    
    Client: Send text "A" back to server.
    
    Server: Receive "A".
    Open Action file.
    Increment handshake.
    Output handshake to Action file, followed by action.
    
    Backend: newHandshake > current handshake.
    Read in Action command.
    Convert to action in game.
    Seems pretty straight forward, it's just 3 programs basically saying to the next "let me know when you're ready", and then passing on the data. I'm running into issues I think because I'm using calls to Sleep to help keep the waiting loops from killing my computer. So every now and then the server and backend collide, and one tries to write to a file while the other is reading, which causes the write to fail.

    So I'm looking for a better solution. I'm thinking right now that instead of having to open the files and read/write handshakes, I could just have the writer create the file, while the reader checks if it exists. When it exists, I could read in the information, close the file, and delete it. So in essence I'd be replacing the handshake int with a check for existence.

    I haven't used C# much, so I don't really know if there's a better way to interface C++ with C#. Does anyone have a better solution?

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Communication between a C++ and C# program?

    Does anyone have a better solution?
    I would suggest using sockets or named pipes.

    - petter

  3. #3
    Join Date
    Nov 2005
    Posts
    63

    Re: Communication between a C++ and C# program?

    I use sockets for the client/server communication...I don't see what you mean about using it for the backend->server communication.

  4. #4
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Communication between a C++ and C# program?

    I use sockets for the client/server communication...I don't see what you mean about using it for the backend->server communication.
    Well, I meant exactly that... you can use sockets for communication between your 'backend' and 'server' just as you use it for communication between your 'server' and 'client'.

    - petter

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

Featured