Networking Source Code.

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Networking Source Code.

      Hi,

      After a few days of tying to find the problems to the
      following code, I thought I would ask, here is the code
      followed my the errors.

      ----IPacket.h----

      class IPacket
      {
      public:
      virtual char const * const VGetType() const=0;
      virtual char const * const VGetData() const=0;
      virtual u_long VGetSize() const =0;
      virtual ~IPacket() { }
      };


      ----BinPacket.h-----
      #include <winsock2.h>
      #include "CPacket.h"
      #include <string.h>
      #include <sys/types.h>
      #include <assert.h>

      class BinaryPacket : public IPacket
      {
      protected:
      char *m_Data;

      public:
      inline BinaryPacket(char const * const data, u_long size);
      inline BinaryPacket(u_long size);
      virtual ~BinaryPacket() { SAFE_DELETE(m_Data); }
      virtual char const * const VGetType() const { return g_Type; }
      virtual char const * const VGetData() const { return m_Data; }
      virtual u_long VGetSize() const { return ntohl(*(u_long *)m_Data); }
      inline void MemCpy(char const *const data, size_t size, int destOffset);

      static const char *g_Type;
      };

      (Other code here for the implementation of above methods. )

      Here are the errors:

      error C2146: syntax error : missing ';' before identifier 'VGetSize'
      error C2253: 'IPacket::VGetSize' : pure specifier only applies to virtual function - specifier ignored
      error C2433: 'IPacket::u_long' : 'virtual' not permitted on data declarations
      error C2501: 'IPacket::u_long' : missing storage-class or type specifiers
      warning C4183: 'VGetSize': missing return type; assumed to be a member function returning 'int'

      As far as I can see there is NOTHING wrong with this code, it is the same as in the authors book. The only thing I can think of is since I split the code into diffrent files, its having some problem. (?)
      I put the following statments in so the compiler will not include then more than twice

      #ifndef IBinaryPacket_H
      #define IBinaryPacket_H

      But this also does not herlp, any ideas?

      Thank you.
    • the only error you are getting is that u_long isn't defined properly. either change them all to unsigned long or add a typedef unsigned long u_long; at the top. It's better than #including an extra header just for one little typedef.
      -Larrik Jaerico

      www.LarrikJ.com
    • Compiler error

      Hi,

      Here is compiler error question,

      I get the following error:

      "error C2027: use of undefined type 'CNetSocket'"


      #include "CNetSocket.h"
      .
      .
      class CNetSocket;.
      .
      .


      //
      int BaseSocketManager::AddSocket( CNetSocket *socket )
      {
      socket->m_id = m_NextSocketId; //ERROR....
      m_SockList.push_front(socket);
      .
      .
      .
      }

      Any ideas on what I am missing here?

      Any help would be great.

      Thanks again.

      S.
    • RE: Compiler error

      Somehow, the compiler missed the definition of CNetSocket inside "CNetSocket.h".

      Do you really need that 'class CNetSocket; after the '#include "CNetSocket.h"'?

      Perhaps that class declaration of 'CNetSocket' is overriding the real definiton of the class that exists inside CNetSocket.h.
    • RE: Compiler error

      Hi,

      I am still TRYING to get the authors code to compile so
      that I see how it works, but with all the problems I just
      gave up! I think for now its alittle over my head and is
      just too complicated and confusing. BUT....

      I started my own network class that is based some of
      the authors suggestions and ideas. Hopefully, I can get
      something to work with my pacman program. :]

      Thanks,

      S.
    • I'm assuming the source code you are trying to compile is the stuff from here?
      mcshaffry.com/GameCode/images/GameCode2.exe

      I remember you saying that you wanted to type it out yourself instead of starting with this giant codebase and figuring it out from there. I still think you have the right idea, but you also took the more difficult path.

      The author's source code for that teapot game requires a lot of external downloads from outside websites like NovodeX and OggVorbis because he wasn't allowed to include those external SDKs in his code distribution. So you would have had to have grabbed those SDKs from their respective websites as well as properly set up your project settings to read them and the code involved.... it is definitely not trivial.

      I think you're doing the right thing. You are writing Pac-Man, which is different from a game of Teapot Wars, so it makes sense that you're going to tailor your project to your own needs. There is just way too much information in that book for Pac-Man, and I wouldn't be surprised if you go back to the book after you finish your project and find that it feels like a different book from when you first read it. Good luck, and you know where to find us. We are here for all your Pac-Man programming needs. :)
    • Hi Kain,

      I gave up because if the "KISS" rule. My pacman game
      is pretty primitive. All I think I need to do is send the
      players name, location on the map, and points. I
      think your right on the authors code being to
      big for something like my game.

      Ok, here is another (painfull) question (hehe).

      Source Code

      1. unsigned int CClientSocket::GetHostByName( std::string HostName )
      2. {
      3. struct hostent *pHostEnt;
      4. struct sockaddr_in TmpSockAddr;
      5. pHostEnt = gethostbyname( HostName.c_str() );
      6. if ( pHostEnt == NULL )
      7. {
      8. assert(0 && "opps, Hummm?!");
      9. return ( 0 );
      10. }
      11. memcpy( &TmpSockAddr.sin_addr, pHostEnt->h_addr, pHostEnt->h_length );
      12. return ( ntohl( TmpSockAddr.sin_addr.s_addr) );
      13. }
      Display All


      The code sniplet above breaks at the assertion, When I
      debug it by placing a breakpoint at "pHostEnt = gethostbyname( HostName.c_str() )", pHostEnt has no
      value.

      Any thoughts?
    • Argggggg...... I got it... Sorry. I found another way
      of doing it.

      Here is what I did, Lets say I want to;

      Connect("195.27.27.0", 3076);

      I did this in the "connection" method,
      .
      .
      ServerAddress.sin_addr.s_addr = inet_addr( (char*) IP);
      ServerAddressl.sin_port = htons( port);
      .
      .

      S.
    • Networking bugs can be maddening.

      For a systems programming course a few semesters ago, we decided our final project would be a multiplayer version of the game snake. We had it running beautiful pretty quickly (like 2 weeks in ncurses), but there were a few crash bugs. We spent the next month and a half working on these bugs, effectively rewriting the entire codebase. In the end, we had improved the game only marginally.

      I won't go into what wrong, but even in a simple network game you need a very stable structure of 'gamestate over time', with the ability to alter the gamestate to account for late commands coming from the other end. Don't even consider trying to put in anti-cheating code, but don't consider NOT putting in (at least simple) anti-lag code.

      Oh, and if you ever find yourself wishing you could just start over from scratch, you should probably do that.
      -Larrik Jaerico

      www.LarrikJ.com

      The post was edited 1 time, last by Larrik ().

    • Hi,

      Would you be willing to share your code with me (us)
      Larrik? I have a few examples of server and client code,
      but it not realy helping. Accourding to the authors book
      I use "streaming" but I have not seen any code on the
      web that helps.

      Right now, I can listen for connections, and thats
      about it. I really have no idea what do do next. I have a
      "send(..)" and "recv(..)", and thats about it.

      As far as anit-cheating goes, I am not worried about it.
      I would like to ask however, what would be some of
      the considerations with writting code to account for
      cheaters? How do cheaters, cheat? What would be
      some of the 'basics' that you could put into your code?

      Thanks again,

      Sabrina.
    • RE: Networking Source Code.

      Hi,

      This is a question about the code in the book.
      The code for the server has;

      Source Code

      1. GameServerListening(int port) { init(port) }


      This invokes the "NetListeningSocket" and calls
      the "init(int port)",

      My question is is when/how does "WSAStartup(0x0202, &m_WsaData);" get called. In the "init(in port)"
      function, if does the following;

      Source Code

      1. if (( m_Socket = socket(AF_INET, SOCK_STREAM, 0 ))
      2. == INVALID_SOCKET)


      If it does not call the BaseSocketManager (Where WSAsocket is located) then how are we declaring
      WSASocket? Dont we need to call WSAStartup first? I dont think the server uses the "BaseSocketmanager"
      class does it?

      Thanks.
    • RE: Networking Source Code.

      Doesnt this seem odd too??

      Source Code

      1. void GameListeningServer::HandleInput()
      2. {
      3. SOCKET new_socket;
      4. unsigned in theipaddr; //WHY PASS THIS IN?!
      5. new_socket = AcceptConnection( &theipaddr);
      6. .
      7. .
      8. .


      The class will invoke the HandleInput() in NetListenSocket class;

      Source Code

      1. SOCKET CListenSocket::AcceptConnection( u_int *pAddr )
      2. {
      3. SOCKET New_Socket;
      4. struct sockaddr_in sock;
      5. int size;
      6. size = sizeof(sock);
      7. if ((New_Socket = accept(m_Socket, (struct sockaddr *)&sock, &size))== INVALID_SOCKET)
      8. return INVALID_SOCKET;
      9. if ( getpeername(New_Socket, (struct sockaddr *)&sock, &size) == SOCKET_ERROR )
      10. {
      11. closesocket(New_Socket);
      12. return INVALID_SOCKET;
      13. }
      14. *pAddr = ntohl(sock.sin_addr.s_addr);
      15. return New_Socket;
      16. }
      Display All


      My question here is what does "*pAddr" suppose to do?
      I tested 2 computers to connect to the server, and
      both times it returns the same number?
    • RE: Networking Source Code.

      I'm sure i'm the wrong person to be responding to this, but it looks like it is pulling out the server address and giving it to you as "theipaddr". That seem right?

      whats ntohl() actually do?
      Wort wort wort.

      The post was edited 1 time, last by Kaimera ().

    • RE: Networking Source Code.

      Kaimera:

      ntohl() takes in your data as the standard network format (byte-ordering) and turns into the host machine's format. Technically, if you are real sure that you will only be sending to and from the platform, you can get away without it. I wouldn't.

      Byte-ordering is not something you should learn from me: it's a weird, confusing, simple concept.

      Sabrina:

      I assume you really are asking why accept() returns a -1, since neither and if, a == or an = will result in -1 (Alright, so the = WILL result in a -1, but that's just from the accept()).

      So, your accept is failing. You can't call this method without:
      A)) A valid socket (m_Socket)
      B)) An incoming connection which is already trying to connect[i]. (Discovered by listen())

      Accept is analogous to picking up the phone when it rings. Pick it up too early, and not only do you NOT hear Grandma's voice, but since you are too busy being hypnotized by the dialtone to receive calls, Grandma's call winds up no where.

      Since you only posted the one line, I can't actually answer your question, though.
      -Larrik Jaerico

      www.LarrikJ.com
    • RE: Networking Source Code.

      Hi Larrik,

      I should have clearified what I was talking about.
      The initial set up for my server is fine, the binding,
      and listening are fine and do not return any errors.
      I can test this by executing the "netstat -an" at the
      command line, and looking for my server and its
      status, (my server state is in 'listening mod'). Also, I can
      connect to my server by just telnet-ing to it, and it
      indeed does connect (although no information is being
      passed). So thats good, the connection part! :)

      So after the initial setup for listening, I want to accept
      users and process their socket, right? right. So what I
      need to do is invoke the 'accept()', and here in lays
      my problem. I am getting a return value of -1. (not good
      news).

      I should note that new_socket is SOCKET as well as
      m_Socket and sock is struct sockaddr_in.

      Thanks again Larrik. (Mr Mentor :) )


      S.
    • RE: Networking Source Code.

      Hi,

      Well, I think I got it. DOH. "..Pick it up too early..."
      That was a big hint after I read it again. I have to give it
      time to initilize and set itself up to accept connections. I
      fixed it by placing a "Sleep(500)" between the initilizing and
      accepting connections. I'm not sure if I like doing this, but.

      S.