Jump to content

  • Log In with Google      Sign In   
  • Create Account

JJJohan

Member Since 17 Apr 2009
Offline Last Active Mar 23 2012 11:11 PM

Topics I've Started

Client and server on same machine causes infinite packet loop

22 March 2012 - 07:50 AM

I've created a client-server scenario, first using broadcast and then moving on to multicasting as an improvement. I began noticing problems in how the networking handled based on whether the client or server opened up first. For example, if I start the server and THEN the client, the client won't be able to read anything from the server, but the other way around worked fine. Turns out that binding both the server and the client on the same port isn't a good idea and doesn't work.

However, my next problem isn't as simple as far as I can tell. In addition to sending a 'disconnect' packet if the client or server closes down, they also send heartbeats in the case that the disconnect packet does not arrive, e.g. the program crashes for any reason.

Now here's the problem scenario:

The server and client are both running.
The server sends a 'ping' every second and the client receives it.
If the client has not received a ping for 10 seconds, the server is removed from the server list.

However, if I close the server, rather than the client ceasing to receive pings, it seems to continue receiving them every single second.

I know it's simply a stack of messages in the buffer because even if I run the server for only around 2 seconds, it will continue to receive pings endlessly, and it's only sending them once a second as verified with some logging.

The server and client use a similar yet basic setup:
-Initiate winsock
-Create a socket
-Bind to port 12345 and 12346 for client and server respectively on INADDR_ANY.
-Send to a multicast address
-select() to determine if there are messages and receive them

Here's a somewhat stripped-down version of the client's receiving code:
fd_set checksockets;
checksockets.fd_count = 1;
checksockets.fd_array[0]=m_socket;
struct timeval t;
t.tv_sec=0;
t.tv_usec=0;
int waiting = select(NULL, &checksockets, NULL, NULL, &t);

// If there is at least one packet receive it.
if (waiting>0) {
	 sockaddr senderAddr;
	 int senderAddrSize = sizeof (senderAddr);
	 recvfrom(m_socket,buf,10000,0,&senderAddr,&senderAddrSize);
	 switch(((Packet*)buf)->m_type) {
		  case PT_SERVERPACKET: {
			   for (auto it = m_serverList.begin();it != m_serverList.end();++it) {
	 			   if (((sockaddr_in*)&(*it).m_address)->sin_addr.s_addr == ((sockaddr_in*)&senderAddr)->sin_addr.s_addr) {
						 OutputDebugString("Server ping.\n"); // Server ping!
	 			   }
			  }
		  } break;
	 }
}

Having trouble creating an array of objects

25 April 2009 - 06:47 PM

I'm working on a simple little game that requires me to constantly create objects, and I figured the best way would be to use arrays as they are individually managed.

Here's the object class, simplified so I can actually see if it works at its base.

class Block
{
    private:
    SDL_Rect box;

    public:
    SDL_Rect* blockclip;
    Block( int x, int y, int w, int h, int randomnumber );
    void show();
};

Block::Block( int x, int y, int w, int h, int randomnumber )
{
    box.x = x;
    box.y = y;
    box.w = w;
    box.h = h;

    blockclip = &clip[ randomnumber ];
    lastblockheight = lastblockheight + box.h;
    lastclip = randomnumber;

}

void Block::show()
{
    apply_surface( box.x, box.y + newblockheight, blocks, screen, blockclip );
}




And I'm trying to create them as so:
     Block Blocks[4];
     for(int i =0;i<4;i++)
     {
         Blocks[i] = Block( 164 - (clip[randomnumber].w), lastblockheight, clip[randomnumber].w, clip[randomnumber].h, randomnumber );
     }




Then show them again later like this in the rendering loop:
     for(int i =0;i<4;i++)
     {
         Blocks[i].show();
     }




Now I can't figure out how I can actually perform this. As I'm getting a range of error messages.
error: no matching function for call to `Block::Block()

I tried using this method instead:
            Block* Blocks[4];
            for(int i =0;i<4;i++)
            {
                Blocks[i] = new Block( 164 - (clip[randomnumber].w), lastblockheight, clip[randomnumber].w, clip[randomnumber].h, randomnumber );
            }




But then I get this error message:
error: request for member `show' in `Blocks[i]', which is of non-class type `Block*

Using physFS with SDL

24 April 2009 - 06:37 PM

A few weeks ago I had asked how to use zlib to load files for SDL, and after little discussion I've been directed towards physFS. I've managed to install it, it works perfectly fine (I'm still shocked I got it working) but now here's the part of integrating it into SDL. How would I go about using zip file resources in my application? I'm currently focusing on the template file that is created when making a new SDL project in CodeBlocks, but I've had no luck so far. I've looked for tutorials, but all I found was ones which were using other libraries which are only going to make it harder for me. Here's a very rough test version of what I'm trying to do:
#ifdef __cplusplus
    #include &lt;cstdlib&gt;
#else
    #include &lt;stdlib.h&gt;
#endif
#ifdef __APPLE__
#include &lt;SDL/SDL.h&gt;
#else
#include &lt;SDL.h&gt;
#endif

#include "physfs.h"
#include &lt;iostream&gt;

int main ( int argc, char** argv )
{

    /* This is the RWops structure we'll be using */
    SDL_RWops *rw;

    SDL_Surface *one;
    SDL_Rect rect;

    PHYSFS_init(NULL);
    PHYSFS_addToSearchPath("myzip.zip", 1);
    PHYSFS_file* cb = PHYSFS_openRead("cb.bmp");
    PHYSFS_sint64 file_size = PHYSFS_fileLength(cb);

    //on a platform where char = 1 byte
    char *myBuf;
    myBuf = new char[PHYSFS_fileLength(cb)];

    int length_read = PHYSFS_read (cb, myBuf, 1, PHYSFS_fileLength(cb));

    // initialize SDL video
    if ( SDL_Init( SDL_INIT_VIDEO ) &lt; 0 )
    {
        printf( "Unable to init SDL: %s\n", SDL_GetError() );
        return 1;
    }

    // make sure SDL cleans up before exit
    atexit(SDL_Quit);

    // create a new window
    SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,
                                           SDL_HWSURFACE|SDL_DOUBLEBUF);
    if ( !screen )
    {
        printf("Unable to set 640x480 video: %s\n", SDL_GetError());
        return 1;
    }

  rw = SDL_RWFromMem(myBuf, file_size);
  one = SDL_LoadBMP_RW(rw, 0);

    // program main loop
    bool done = false;
    while (!done)
    {
        // message processing loop
        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
            // check for messages
            switch (event.type)
            {
                // exit if the window is closed
            case SDL_QUIT:
                done = true;
                break;

                // check for keypresses
            case SDL_KEYDOWN:
                {
                    // exit if ESCAPE is pressed
                    if (event.key.keysym.sym == SDLK_ESCAPE)
                        done = true;
                    break;
                }
            } // end switch
        } // end of message processing

        // DRAWING STARTS HERE

        // clear screen
        SDL_FillRect(screen, 0, SDL_MapRGB(screen-&gt;format, 0, 0, 0));

  rect.x = rect.y = 20;
  rect.w = one -&gt; w;
  rect.y = one -&gt; h;
  SDL_BlitSurface(one, NULL, screen, &rect);

        // DRAWING ENDS HERE

        // finally, update the screen :)
        SDL_Flip(screen);
    } // end main loop

    PHYSFS_close(cb);
    PHYSFS_deinit();

    // all is well ;)
    printf("Exited cleanly\n");
    return 0;
}

Now I obviously can't directly load the pointer file and use it as an image in SDL, but that's why I'm posting this :). [Edited by - JJJohan on April 25, 2009 1:15:50 AM]

Zlib - compressing multiple files into one compressed file?

17 April 2009 - 02:38 AM

I've spend quite a few hours looking for a bunch of zlib resources since I have no idea what to actually use.. Anyway, I downloaded zlib along with zlibtool.zip elsewhere, which basically lets me do this to compress and uncompress files:
#include "zlibengn.h"

#pragma comment(lib,"zdll.lib")

ZlibEngine z;

int main(int argc, char* argv[])
{
	z.compress( "zlib.h","zlib.h__");
	z.decompress( "zlib.h__", "test.h");
	return 0;
}
Now the question is, how would I go at compressing multiple files into one file, then obviously decompressing them all again in their original place? I tried using a folder which doesn't seem to work, and I don't arrays are working either. If I can't use zlibtools, then please be so kind to redirect me to what I need to use. I'd really like to use this method to store a hefty number of game resources into a single compress folder, then if needed uncompress them while the game loads etc. (or just load them while compressed if that's possible)

PARTNERS