Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Snash

WinSOCK and Network question(s)

This topic is 5649 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I''m going to create a simple bomberman clone with network support, but I''ve never created a multiplayer game before. A few questions. If i have a player struct array. struct Player { int x; int y; }; ThePlayer[4] I want my program to first startup a server, then connect with a client. I''ve got this to work, but, if i send something from the client, how does my server know that it''s the var Player[0].X?? Can I send a whole Struct? Eg. can I send the Player[0] to the server, so the server nows both the x and y cord of the player 0 ? Also, is it easier to create the game with UDP packets instead of TCP? I''ve looked into this, and it seems UDP packets are more unreliable, since you don''t make a connection to a server. Thanks for your time.

Share this post


Link to post
Share on other sites
Advertisement
ok..
yes you can send a structure, and the way you know what your recieving is by adding a small bit of header information.. so instead of sending 123 you send [1][123] where the 1st number represents the type of data being sent.. and u define say..
1= player1.x
2= player1.y
3= player2.x
etc..

so if you recieve [1][123][2][321] you read that as
player1.x=123
player1.y=321

and dont bother with UDP.. it will make things harder for you and give you no benefit for that project

good luck

Share this post


Link to post
Share on other sites
Here's a quick & dirty class helper you can use to pack structures for transport:


    
//******************************************************

// $$C sBuffer

//

// General linear buffer manupulation. It bears no

// storage (uses external space), but keeps track of

// the data position and size in the buffer.

//

//******************************************************

class sBuffer
{
public:
//---- General

sBuffer( unsigned char * );
sBuffer( char * );
~sBuffer( );
//---- Encode.

void WriteByte( unsigned char );
void WriteShort( short );
void WriteLong( long );
void WriteFloat( float );
void WriteString( char * );
void WriteMulti( void *, int );
//---- Decode.

unsigned char ReadByte( void );
short ReadShort( void );
long ReadLong( void );
float ReadFloat( void );
char * ReadString( void );
void ReadMulti( void *, int );
public:
unsigned char * m_pData;
int m_iPos;
};


inline sBuffer::sBuffer( unsigned char * in_str ) : m_pData( in_str ), m_iPos(0)
{
}


inline sBuffer::sBuffer( char * in_str ) : m_pData( (unsigned char*) in_str ), m_iPos(0)
{
}


inline sBuffer::~sBuffer()
{
}

inline void sBuffer::WriteByte( unsigned char in_ch )
{
m_pData[ m_iPos++ ] = in_ch;
}

inline void sBuffer::WriteShort( short in_s )
{
m_pData[ m_iPos++ ] = (unsigned char) ( (in_s >> 8 ) & 0xFF );
m_pData[ m_iPos++ ] = (unsigned char) ( (in_s ) & 0xFF );
}

inline void sBuffer::WriteLong( long in_l )
{
m_pData[ m_iPos++ ] = (unsigned char) ( (in_l >> 24 ) & 0xFF );
m_pData[ m_iPos++ ] = (unsigned char) ( (in_l >> 16 ) & 0xFF );
m_pData[ m_iPos++ ] = (unsigned char) ( (in_l >> 8 ) & 0xFF );
m_pData[ m_iPos++ ] = (unsigned char) ( (in_l ) & 0xFF );
}

inline void sBuffer::WriteFloat( float in_f )
{
m_pData[ m_iPos++ ] = ( (unsigned char*)&in_f )[ 0 ];
m_pData[ m_iPos++ ] = ( (unsigned char*)&in_f )[ 1 ];
m_pData[ m_iPos++ ] = ( (unsigned char*)&in_f )[ 2 ];
m_pData[ m_iPos++ ] = ( (unsigned char*)&in_f )[ 3 ];
}

inline void sBuffer::WriteString( char * in_str )
{
while ( *in_str )
m_pData[ m_iPos++ ] = (unsigned char) *in_str++;
m_pData[ m_iPos++ ] = 0;
}

inline void sBuffer::WriteMulti( void * in_pData, int in_iLen )
{
::memmove( &m_pData[ m_iPos ], in_pData, in_iLen );
m_iPos += in_iLen;
}

inline unsigned char sBuffer::ReadByte( void )
{
return( m_pData[ m_iPos++ ] );
}

inline short sBuffer::ReadShort( void )
{
short s;
s = (short) m_pData[ m_iPos++ ];
s = (s << 8) + (short) m_pData[ m_iPos++ ];
return( s );
}

inline long sBuffer::ReadLong( void )
{
long l;
l = (long) m_pData[ m_iPos++ ];
l = (l << 8) + (long) m_pData[ m_iPos++ ];
l = (l << 8) + (long) m_pData[ m_iPos++ ];
l = (l << 8) + (long) m_pData[ m_iPos++ ];
return( l );
}

inline float sBuffer::ReadFloat( void )
{
float f;
( (unsigned char*)&f )[ 0 ] = m_pData[ m_iPos++ ];
( (unsigned char*)&f )[ 1 ] = m_pData[ m_iPos++ ];
( (unsigned char*)&f )[ 2 ] = m_pData[ m_iPos++ ];
( (unsigned char*)&f )[ 3 ] = m_pData[ m_iPos++ ];
return( f );
}

inline char * sBuffer::ReadString( void )
{
char * l_pStr = (char*) &m_pData[ m_iPos ];
while( m_pData[ m_iPos ] )
m_iPos ++;
m_iPos ++;
return( l_pStr );
}

inline void sBuffer::ReadMulti( void * in_pData, int in_iLen )
{
::memmove( in_pData, &m_pData[ m_iPos ], in_iLen );
m_iPos += in_iLen;
}



And he're how you can use it:


struct Player * l_player;

//============= OUTGOING ==================

char l_cBuffer[ 256 ];
sBuffer l_buff( l_cBuffer );

//---- Encode query header and send.

l_buff.WriteShort( 0xDEAD ); // ID: magic key

l_buff.WriteShort( 8 ); // Total length = 8

l_buff.WriteShort( player->x ); // Player X pos

l_buff.WriteShort( player->y ); // Player X pos

SendPacket( l_cBuffer, l_buff.m_iPos );

//============= INCOMING ==================

char l_cBuffer[ 256 ];
sBuffer l_buff( l_cBuffer );

//---- Read a packet and decode

ReadPacket( l_cBuffer, sizeof(l_cBuffer) );
short l_sMagic = l_buff.ReadShort( );
short l_sLen = l_buff.ReadShort( );
l_player->x = (int) l_buff.ReadShort( );
l_player->y = (int) l_buff.ReadShort( );




-cb


[edited by - cbenoi1 on April 1, 2003 9:12:37 AM]

Share this post


Link to post
Share on other sites
Thanks for the info, and the class.
But why is UDP noo good?
UDP is faster than TCP, right?
I''m thinking making it both Lan and Internet, but I guess that 8 Players + Bombs doesnt take alot of bandwith or am I wrong.

One more question, how many times a sec should I update my player positions? Eg. 30 Packets pr. player pr. second? Or less?

Thanks

Share this post


Link to post
Share on other sites
Why don''t you read some of the articles posted here at gamedev on the subject ?

UDP is faster but the packet/message might not arrive at it''s destination.

Share this post


Link to post
Share on other sites
Read this at Gamasutra (sign up for a free account):

http://www.gamasutra.com/features/19970919/aronson_01.htm

Share this post


Link to post
Share on other sites
> Eg. 30 Packets pr. player pr. second?

The human eye has a resolution of 24 images/sec and an eye-hand coordination response time of about 100 ms (10 FPS). Aim for 15 FPS worst case and 30 FPS average case; everything above 30 is candy.

-cb

Share this post


Link to post
Share on other sites
quote:
Original post by cbenoi1
> Eg. 30 Packets pr. player pr. second?

The human eye has a resolution of 24 images/sec and an eye-hand coordination response time of about 100 ms (10 FPS). Aim for 15 FPS worst case and 30 FPS average case; everything above 30 is candy.




*sigh*. Why do people keep saying that. This is only true in movies because of motion blur.......
Aim for 60 fps framerate. Send updates 10 times/seconds and between sends you update your gameobjects using the info about speed and direction you have.

Share this post


Link to post
Share on other sites
> Send updates 10 times/seconds and
> between sends you update your gameobjects
> using the info about speed and direction you have.

Maybe a ''bomberman'' game may not be as demanding as a combat sim... Your numbers are about right. You can always interpolate the projectiles'' trajectory in-between updates for the graphics renderer.

> Why do people keep saying that.
> (...) Aim for 60 fps framerate.

Because that''s the way the human vision and reflex systems work. That said, the numbers are benchmarks for an eye-hand coordination gameplay, blending both network and graphics update rates. I concur with your separation of graphics and networking update rates and I should have made it clearer in my earlier post.

-cb

Share this post


Link to post
Share on other sites
I must have special eyes then, I can tell a difference between 30 fps, 60 fps and even 80 fps in a FPS game. *shrug*

-=[ Megahertz ]=-

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!