My game to online..

This is a discussion on My game to online.. within the C++ Programming forums, part of the General Programming Boards category; I made game using C++ and SDL libraries, now i want to make it to be an online game. But ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    21

    Unhappy My game to online..

    I made game using C++ and SDL libraries, now i want to make it to be an online game.
    But don't know how. I have source codes of Server and Client from MSDN using winsock i tried to put client code into the code of game, it works, but there is a lot of things i don't know to do. The server i shut downing when he receive first message from client. To avoid that i put while loop, so when he shutdown, he starts up again. And the for the client, is the same thing, he needs to start up and shutdown whenever i click. I don't know how to make a server that will be always turn on, receive messages (numbers) from 4 clients (not at same time, first from client 1, then client 2, then client 3, then client 4 and then from client 1....)
    and send received messages (numbers, which sends one client) to all client in same time.

    Maybe this is not a right question, but can somebody make online part (server and put right client code into the code of game), i will send pm with all codes... ?? :/

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    Put the game to one side for now, and practice writing client-server code.

    Each client sends simple text messages to the server, which in turn sends these on to other clients.
    So for example
    "Player1: X=10 Y=20"

    Player 1 sends this, and it should be printed on the screen for players 2, 3 and 4.

    When you get stuck, post your code.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    21
    How to get integer input from client 1 and send it to server, and then server add 10 on that number and send result to client 2
    int X;
    scanf("%d", &X);
    send(ConnectSocket, X, recvbuflen, 0);

    It says can not convert int to char* .. :/
    --------------

    Client 1:

    Enter number: 2
    send number 10 to server

    Server:
    receive number 2
    result = 2 + 10
    send result to client 2

    Client 2:
    receive result
    printf("Result: %d", result)

    ---------------
    Last edited by clzola; 11-13-2010 at 01:44 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    Use sprintf() to create your text messages.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    21
    thansk, i used it and it work fine

    I make 2 clients and one server
    Start Server
    then start client 1 and client 2

    client 1 -> scanf X -> send to server -> server: X+10 -> send to client 2 -> client 2: printf X
    client 1 -> scanf Y -> send to server -> server: Y+15 -> send to client 2 -> client 2: printf Y

    then tried to made, to client 1 send to server, server send to client 2 and then client 2 send to server then send to client 1
    but i want to print X and Y on screen of Client 2 before inputing X and Y of client 2 in client 2

    i cant't do that, i can make first input in client 1 send it to server send it to client 2
    input in client 2 send ti to server send it to client 1
    And then it prints in both clients the values that received from each other.


    Code:
        // ****************************************************************
        // * SEND X, Y
        // *
        
        int x;
        int y;
        
        scanf("%d", &x);
        const int bufferSize = 20;
        char buffer[bufferSize];
        sprintf(buffer, "%i", x);
         
        // Send an initial buffer
        iResult = send( ConnectSocket, (char*)buffer, (int)strlen(buffer), 0 );
        if (iResult == SOCKET_ERROR) {
            printf("send failed with error: %d\n", WSAGetLastError());
            closesocket(ConnectSocket);
            WSACleanup();
            return 1;
        }
        
        scanf("%d", &y);
        sprintf(buffer, "%i", y);
        // Send an initial buffer
        iResult = send( ConnectSocket, (char*)buffer, (int)strlen(buffer), 0 );
        if (iResult == SOCKET_ERROR) {
            printf("send failed with error: %d\n", WSAGetLastError());
            closesocket(ConnectSocket);
            WSACleanup();
            return 1;
        }
        
        do { 
    
            iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
            if ( iResult > 0 )
                printf("%s\n", recvbuf);
    
        } while( iResult > 0 );
    
    
        // shutdown the connection since no more data will be sent
        iResult = shutdown(ConnectSocket, SD_SEND);
        if (iResult == SOCKET_ERROR) {
            printf("shutdown failed with error: %d\n", WSAGetLastError());
            closesocket(ConnectSocket);
            WSACleanup();
            return 1;
        }
    As you can see, i put first scanf and send functions, and then receive function, it is obviously a problem, but if i change they places, then i can send from client 1, print in client 2 and there is the end, I cant input anything...

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    Well the basic problem you now have is that your user input is a blocking function. If you're waiting for input, you can't do anything else.

    Now, if you were using a real OS like Unix/Linux, this would be an easy thing to do. You would just use the select() system call with the descriptors of interest (stdin and your socket), and wait for something interesting to happen.

    But the MS bastardisation of select only works with socket descriptors (not ALL descriptors).

    Where to go from here?
    The basic problem is that you can't get stuck waiting for input from one place, whilst there may be data waiting from the other place.

    Winsock IOCTLs (Windows)
    You can easily set the socket to non-blocking. recv() will always return immediately, so you need to be more careful with examining the return results. It is unlikely that it will return a complete "message" in one single call. Often, it will return nothing at all, and then it would return things line "mes" and "sage". Reassembling this into "message" is up to you.

    For the console input, things are a little trickier.
    Win32 Console Applications 5 coupled with GetNumberOfConsoleInputEvents Function (Windows) would allow you to peek and then read the standard input. But again, you have the problem that information will arrive per character. You can't just use scanf() to read and convert directly.
    What you can do is build a buffer one character at a time until you read a newline. Then you can use regular sscanf() to perform the conversion.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    Are you using C or C++?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    21
    Well i saved source files in C++, but i think, it is more C.
    printf and scanf can also be used in C++, you just need to put '#include <stdio.h>'


    I made progress. I made SERVER + 3 Clients that communicate to each other. Going to make 4th client and then I'll try to make it work via internet

    I'll post here if I get stuck
    Last edited by clzola; 11-14-2010 at 04:28 PM.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    Quote Originally Posted by clzola View Post
    ...
    printf and scanf can also be used in C++, you just need to put '#include <stdio.h>'
    You can, but people generally sneer at it, and for good reason.
    If you're using C++, you should be able to extend your design by making use of the generous amount if tools available.
    If you don't, then you may as well use C and its limited sets of tools.

    Hence the question: are you planning on utilizing C++ or just doing old C?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    21
    In school, we are learning C, so i practice C at home, then I come up with this idea, to make this game. (my 3 friends and I invented this game an year ago )
    I read on MSDN for winsock: make new C++ project/file, so I saved source codes in .cpp. So, I use and know, some basic of C.

    I think I will use old C to finish this game...

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    Then it is probably better that you ask for help in the C forum in the future. Otherwise you will get a lot of advice on changing your code into C++ and C++-specific hints and help.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Posts
    21
    I made SERVER and 4 Clients, and I can connect all 4 clients to the server, and make them communicate to each other. And this is all on my computer.

    How to make these 4 clients to connect on server, from different computers (via Internet)??

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    The server has a fixed known address (like this forum), and every client (like your browser) connects to it when it needs to.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Registered User
    Join Date
    Nov 2010
    Posts
    21
    I mean, i start server on my computer, and when i tried to start client on other computer, it could not connect to it. I dont know how to tell the client the ip of server.

    Is the server's ip same as my computer's ip?

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    > Is the server's ip same as my computer's ip?
    Yes.

    But the next problem is most likely your firewall preventing every random driveby from trying to connect to whatever on your machine.

    Whichever port you've chosen for your server, you need to make sure there is a nice narrow rule (pin-hole) that allows external traffic to connect to that port.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for major help on hacking a online game
    By bergerman123 in forum C++ Programming
    Replies: 5
    Last Post: 12-09-2008, 10:27 PM
  2. craps game & dice game..
    By cgurl05 in forum C Programming
    Replies: 3
    Last Post: 03-25-2006, 07:58 PM
  3. beach bar (sims type game)
    By DrKillPatient in forum Game Programming
    Replies: 1
    Last Post: 03-06-2006, 01:32 PM
  4. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-22-2006, 12:23 AM
  5. Online Game
    By Korhedron in forum Game Programming
    Replies: 7
    Last Post: 03-10-2005, 10:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21