Networking Code for Linux
I havent used this code for the game yet, but I will probably
use something similar to this.
//
// com_object.h
//
#ifndef _COM_OBJECT_H_
#define _COM_OBJECT_H_
#include
// that is one big message
#define MESSAGE_SIZE 4096
#define RECV_MSG_TYPE 1
#define SEND_MSG_TYPE 2
class Com_Object {
public:
Com_Object();
~Com_Object();
void Load_Address(const char *ip, const int port);
void Init(void);
void Connect(void);
void Disconnect(void);
void SendMsg(const char *msg);
void RecvMsg(void);
void Recv(void); // recv character at a time
void GetMsg(char *res_msg)
{ strcpy(res_msg, m_aMsg); }
private:
char ComGetChar(void); // get chars one at a time
char m_aHostName[256];
bool m_bConnectFlag;
int m_iPortNo;
int m_iSocketDesc; // socket descriptor
char m_aMsg[MESSAGE_SIZE]; // send and receive messages
int m_iMsgType; // last message type recv/send
int m_iMsgSize;
};
#endif
//
// com_object.cpp
//
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "com_object.h"
#include "game.h"
//
// Com
//
Com_Object::Com_Object()
{
Init();
} // end of the function
Com_Object::~Com_Object()
{
} // end of teh functino
//
// Init
//
void Com_Object::Init(void)
{
m_bConnectFlag = false;
m_iPortNo = -1;
} // end of the functio
//
// Load IP
//
void Com_Object::Load_Address(const char *ip, const int port)
{
strcpy(m_aHostName, ip);
m_iPortNo = port;
} // end of the function
//
// Disconnect
// - this can be tricky, but I think the
// best time to call this function would be
// before you delete the object
//
void Com_Object::Disconnect(void)
{
close(m_iSocketDesc);
m_bConnectFlag = false;
} // end of the function
//
// Send Msg
//
void Com_Object::SendMsg(const char *msg)
{
strcpy(m_aMsg, msg);
m_iMsgType = SEND_MSG_TYPE;
m_iMsgSize = strlen(m_aMsg);
if (send(m_iSocketDesc, m_aMsg, strlen(m_aMsg) + 1, 0) < 0)
{
Game_Error("send() failed:\n");
} // end of the if
} // end of the function
//
// RcvMsg
//
void Com_Object::RecvMsg(void)
{
recv(m_iSocketDesc, m_aMsg, sizeof(m_aMsg), 0);
// get the string length
m_iMsgSize = strlen(m_aMsg);
} // end of the function
//
// ComGetChar
// - wait for new line
char Com_Object::ComGetChar(void)
{
char tiny[10];
recv(m_iSocketDesc, tiny, 1, 0);
tiny[1] = '\0';
return tiny[0];
} // end of tehfunction
//
// Recv only a char at a time
//
void Com_Object::Recv(void)
{
char c;
int index= 0;
while(1)
{
c = ComGetChar();
m_aMsg[index] = c;
if (index >= MESSAGE_SIZE)
{
m_aMsg[MESSAGE_SIZE-1] = '\0';
break;
} // end of the if
// break on a new line, we may get a carriage
// return though
if (c == '\n')
{
m_aMsg[index] = c;
index++; // for null terminator
m_aMsg[index] = '\0';
break;
} // end of the if
index++;
} // end of the while
m_iMsgSize = index; // I hope this close
m_iMsgType = RECV_MSG_TYPE;
// Note: newline should be included
} // end of the function
//
// Connect
//
void Com_Object::Connect(void)
{
struct sockaddr_in serv_addr;
struct hostent *hp;
if (m_bConnectFlag)
return; // already connected
if ((hp = gethostbyname(m_aHostName)) == NULL)
{
Game_Error("gethostbyname() failed:\n");
} // end of the if
m_iSocketDesc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (m_iSocketDesc < 0)
Game_Error("socket() failed: exiting\n");
// load the server address structure
memset(&serv_addr, 0, sizeof(serv_addr));
memcpy(&serv_addr.sin_addr, hp->h_addr, hp->h_length);
serv_addr.sin_family = hp->h_addrtype; // IP family
serv_addr.sin_port = htons(m_iPortNo);
// get connection
if (connect(m_iSocketDesc, (struct sockaddr *)&serv_addr,
sizeof(serv_addr)) < 0)
{
Game_Error("connect():\n");
} // end of if
} // end of the function