• Create Account

Banner advertising on our site currently available from just \$5!

### #ActualVortez

Posted 19 September 2013 - 11:31 AM

You usually prefix the lenght of the data you want to send with an int, so yea, you have to do it yourself. The "how" is up to you.

However... with a lot of work, you can automate all this process, like i did in my networking class. For example, to send data i do something like this:

struct CNetworkMsg {
DWORD MsgSize;
DWORD MsgID;
BYTE* pData;
};

...

BYTE SomeBuffer[1000];
...

CNetworkMsg Msg;
Msg.MsgID   = 1; // <-- Whatever you want...
Msg.MsgSize = 1000;
Msg.pData   = (BYTE*)&SomeBuffer[0];

Client.SendMsg(&Msg);



On the other side, the read is automated inside the network class, when it's done reading it, this virtual funciton is called automatically, and do what i want with the message received

void CVortezNetworkClientExt::OnCustomNetworkMsgRecv(SOCKET s, CNetworkMsg *pMsg)
{
//AddListboxMessageArg(MainForm->ListBox, "Custom Message Received: MsgID: 0x%8.8X MsgSize: %d (Socket: 0x%8.8X)", pMsg->MsgID, pMsg->MsgSize, (UINT)s);

switch(pMsg->MsgID)
{
case 1:
{
BYTE SomeBuffer[1000];

// useless memory copy, just an example...
memcpy(&SomeBuffer[0], pMsg->pData, pMsg->MsgSize);

// ... do something with the data
}
break;
}
}


Im remaking one of my networked application right now with it and it work like a charm. So much easier (and faster) than my previous code that was polling the socket manually each time too (i now use windows messages to handle my sockets)

You probably won't understand the code im going to post, but here's how i do my read (just to give you an idea) As you can see, i start by reading the header, which is always 8 bytes long (2 x UINT), then i read as much bytes as Msg.MsgSize value.

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
{
BYTE  *pBuf = NULL;
DWORD *pBufIndx = NULL;

} else {
pBuf     = MsgBuffer.pData;
pBufIndx = MsgBuffer.GetDataIndxPtr();

}

// Find how many bytes we have to read, in chunk of MAX_PACKET_SIZE
DWORD MaxBytesToProcess = (BufSize - *pBufIndx);
if(MaxBytesToProcess > MAX_PACKET_SIZE)
MaxBytesToProcess = MAX_PACKET_SIZE;

/////////////////////////////////////////////////

DWORD NumBytesProcessed = recv(*pSock, (char*)&pBuf[*pBufIndx], MaxBytesToProcess, 0);

/////////////////////////////////////////////////

/////////////////////////////////////////////////

if(NumBytesProcessed != 0 && NumBytesProcessed != 0xFFFFFFFF){
// Inc. our counters
*pBufIndx += NumBytesProcessed;
// Store how many bytes we've read
// Have we finished reading/writing data?
Res.IsBufferLimitReach = *pBufIndx == BufSize;
// Have we hit M or Size limit?
Res.IsPacketLimitReach = NumBytesProcessed == MAX_PACKET_SIZE;
// Return sucess...
Res.Succes = TRUE;
}

return Res;
}

/////////////////////////////////////////////////////////////////////////////////////////////////

{

if(!Res.Succes)
return FALSE;

// Do we have some msg data left or available to read?

CNetworkMsg Msg;
Msg.pData = MsgBuffer.pData;

//ProcessMsg(&Msg);
SendMessage(hHostWnd, ON_NETWORK_MESSAGE, (WPARAM)*pSock, (LPARAM)&Msg);

MsgBuffer.Init();
return TRUE;
} else {
MsgBuffer.AllocateDataBuffer();
}
}
// but not the data
// Begin or resume reading the message data
if(!Res.Succes)
return FALSE;

// Do we have some msg data left or available to read?

CNetworkMsg Msg;
Msg.pData = MsgBuffer.pData;

// This cause a call the virtual function OnCustomNetworkMsgRecv() shown above
SendMessage(hHostWnd, ON_NETWORK_MESSAGE, (WPARAM)*pSock, (LPARAM)&Msg);

MsgBuffer.Init();
return TRUE;
}
}

return 0;
}



### #11Vortez

Posted 19 September 2013 - 11:29 AM

You usually prefix the lenght of the data you want to send with an int, so yea, you have to do it yourself. The "how" is up to you.

However... with a lot of work, you can automate all this process, like i did in my networking class. For example, to send data i do something like this:

struct CNetworkMsg {
DWORD MsgSize;
DWORD MsgID;
BYTE* pData;
};

...

BYTE SomeBuffer[1000];
...

CNetworkMsg Msg;
Msg.MsgID   = 1; // <-- Whatever you want...
Msg.MsgSize = 1000;
Msg.pData   = (BYTE*)&SomeBuffer[0];

Client.SendMsg(&Msg);



On the other side, the read is automated inside the network class, when it's done reading it, this virtual funciton is called automatically, and do what i want with the message received

void CVortezNetworkClientExt::OnCustomNetworkMsgRecv(SOCKET s, CNetworkMsg *pMsg)
{
//AddListboxMessageArg(MainForm->ListBox, "Custom Message Received: MsgID: 0x%8.8X MsgSize: %d (Socket: 0x%8.8X)", pMsg->MsgID, pMsg->MsgSize, (UINT)s);

switch(pMsg->MsgID)
{
case 1:
{
BYTE SomeBuffer[1000];

// useless memory copy, just an example...
memcpy(&SomeBuffer[0], pMsg->pData, pMsg->MsgSize);

// ... do something with the data
}
break;
}
}


Im remaking one of my networked application right now with it and it work like a charm. So much easier (and faster) than my previous code that was polling the socket manually each time too (i now use windows messages to handle my sockets)

You probably won't understand the code im going to post, but here's how i do my read (just to give you an idea)

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
{
BYTE  *pBuf = NULL;
DWORD *pBufIndx = NULL;

} else {
pBuf     = MsgBuffer.pData;
pBufIndx = MsgBuffer.GetDataIndxPtr();

}

// Find how many bytes we have to read, in chunk of MAX_PACKET_SIZE
DWORD MaxBytesToProcess = (BufSize - *pBufIndx);
if(MaxBytesToProcess > MAX_PACKET_SIZE)
MaxBytesToProcess = MAX_PACKET_SIZE;

/////////////////////////////////////////////////

DWORD NumBytesProcessed = recv(*pSock, (char*)&pBuf[*pBufIndx], MaxBytesToProcess, 0);

/////////////////////////////////////////////////

/////////////////////////////////////////////////

if(NumBytesProcessed != 0 && NumBytesProcessed != 0xFFFFFFFF){
// Inc. our counters
*pBufIndx += NumBytesProcessed;
// Store how many bytes we've read
// Have we finished reading/writing data?
Res.IsBufferLimitReach = *pBufIndx == BufSize;
// Have we hit M or Size limit?
Res.IsPacketLimitReach = NumBytesProcessed == MAX_PACKET_SIZE;
// Return sucess...
Res.Succes = TRUE;
}

return Res;
}

/////////////////////////////////////////////////////////////////////////////////////////////////

{

if(!Res.Succes)
return FALSE;

// Do we have some msg data left or available to read?

CNetworkMsg Msg;
Msg.pData = MsgBuffer.pData;

//ProcessMsg(&Msg);
SendMessage(hHostWnd, ON_NETWORK_MESSAGE, (WPARAM)*pSock, (LPARAM)&Msg);

MsgBuffer.Init();
return TRUE;
} else {
MsgBuffer.AllocateDataBuffer();
}
}
// but not the data
// Begin or resume reading the message data
if(!Res.Succes)
return FALSE;

// Do we have some msg data left or available to read?

CNetworkMsg Msg;
Msg.pData = MsgBuffer.pData;

// This cause a call the virtual function OnCustomNetworkMsgRecv() shown above
SendMessage(hHostWnd, ON_NETWORK_MESSAGE, (WPARAM)*pSock, (LPARAM)&Msg);

MsgBuffer.Init();
return TRUE;
}
}

return 0;
}



### #10Vortez

Posted 19 September 2013 - 11:26 AM

You usually prefix the lenght of the data you want to send with an int, so yea, you have to do it yourself. The "how" is up to you.

However... with a lot of work, you can automate all this process, like i did in my networking class. For example, to send data i do something like this:

struct CNetworkMsg {
DWORD MsgSize;
DWORD MsgID;
BYTE* pData;
};

...

BYTE SomeBuffer[1000];
...

CNetworkMsg Msg;
Msg.MsgID   = 1; // <-- Whatever you want...
Msg.MsgSize = 1000;
Msg.pData   = (BYTE*)&SomeBuffer[0];

Client.SendMsg(&Msg);



On the other side, the read is automated inside the network class, when it's done reading it, this virtual funciton is called automatically, and do what i want with the message received

void CVortezNetworkClientExt::OnCustomNetworkMsgRecv(SOCKET s, CNetworkMsg *pMsg)
{
//AddListboxMessageArg(MainForm->ListBox, "Custom Message Received: MsgID: 0x%8.8X MsgSize: %d (Socket: 0x%8.8X)", pMsg->MsgID, pMsg->MsgSize, (UINT)s);

switch(pMsg->MsgID)
{
case 1:
{
BYTE SomeBuffer[1000];

// useless memory copy, just an example...
memcpy(&SomeBuffer[0], pMsg->pData, pMsg->MsgSize);

// ... do something with the data
}
break;
}
}


Im remaking one of my networked application right now with it and it work like a charm. So much easier (and faster) than my previous code that was polling the socket manually each time too (i now use windows messages to handle my sockets)

You probably won't understand the code im going to post, but here's how i do my read (just to give you an idea)

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
{
BYTE  *pBuf = NULL;
DWORD *pBufIndx = NULL;

} else {
pBuf     = MsgBuffer.pData;
pBufIndx = MsgBuffer.GetDataIndxPtr();

}

// Find how many bytes we have to read, in chunk of MAX_PACKET_SIZE
DWORD MaxBytesToProcess = (BufSize - *pBufIndx);
if(MaxBytesToProcess > MAX_PACKET_SIZE)
MaxBytesToProcess = MAX_PACKET_SIZE;

/////////////////////////////////////////////////

DWORD NumBytesProcessed = recv(*pSock, (char*)&pBuf[*pBufIndx], MaxBytesToProcess, 0);

/////////////////////////////////////////////////

/////////////////////////////////////////////////

if(NumBytesProcessed != 0 && NumBytesProcessed != 0xFFFFFFFF){
// Inc. our counters
*pBufIndx += NumBytesProcessed;
// Store how many bytes we've read
// Have we finished reading/writing data?
Res.IsBufferLimitReach = *pBufIndx == BufSize;
// Have we hit M or Size limit?
Res.IsPacketLimitReach = NumBytesProcessed == MAX_PACKET_SIZE;
// Return sucess...
Res.Succes = TRUE;
}

return Res;
}

/////////////////////////////////////////////////////////////////////////////////////////////////

{

if(!Res.Succes)
return FALSE;

// Do we have some msg data left or available to read?

CNetworkMsg Msg;
Msg.pData = MsgBuffer.pData;

//ProcessMsg(&Msg);
SendMessage(hHostWnd, ON_NETWORK_MESSAGE, (WPARAM)*pSock, (LPARAM)&Msg);

MsgBuffer.Init();
return TRUE;
} else {
MsgBuffer.AllocateDataBuffer();
}
}
// but not the data
// Begin or resume reading the message data
if(!Res.Succes)
return FALSE;

// Do we have some msg data left or available to read?

CNetworkMsg Msg;
Msg.pData = MsgBuffer.pData;

//ProcessMsg(&Msg);
SendMessage(hHostWnd, ON_NETWORK_MESSAGE, (WPARAM)*pSock, (LPARAM)&Msg);

MsgBuffer.Init();
return TRUE;
}
}

return 0;
}



### #9Vortez

Posted 19 September 2013 - 11:25 AM

You usually prefix the lenght of the data you want to send with an int, so yea, you have to do it yourself. The "how" is up to you.

However... with a lot of work, you can automate all this process, like i did in my networking class. For example, to send data i do something like this:

struct CNetworkMsg {
DWORD MsgSize;
DWORD MsgID;
BYTE* pData;
};

...

BYTE SomeBuffer[1000];
...

CNetworkMsg Msg;
Msg.MsgID   = 1; // <-- Whatever you want...
Msg.MsgSize = 1000;
Msg.pData   = (BYTE*)&SomeBuffer[0];

Client.SendMsg(&Msg);



On the other side, the read is automated inside the network class, when it's done reading it, this virtual funciton is called automatically, and do what i want with the message received

void CVortezNetworkClientExt::OnCustomNetworkMsgRecv(SOCKET s, CNetworkMsg *pMsg)
{
//AddListboxMessageArg(MainForm->ListBox, "Custom Message Received: MsgID: 0x%8.8X MsgSize: %d (Socket: 0x%8.8X)", pMsg->MsgID, pMsg->MsgSize, (UINT)s);

switch(pMsg->MsgID)
{
case 1:
{
BYTE SomeBuffer[1000];

// useless memory copy, just an example...
memcpy(&SomeBuffer[0], pMsg->pData, pMsg->MsgSize);

// ... do something with the data
}
break;
}
}


Im remaking one of my networked application right now with it and it work like a charm. So much easier (and faster) than my previous code that was polling the socket manually each time too (i now use windows messages to handle my sockets)

You probably won't understand the code im going to post, but here's how i do my read (just to give you an idea)

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
{
BYTE  *pBuf = NULL;
DWORD *pBufIndx = NULL;

} else {
pBuf     = MsgBuffer.pData;
pBufIndx = MsgBuffer.GetDataIndxPtr();

}

// Find how many bytes we have to read, in chunk of MAX_PACKET_SIZE
DWORD MaxBytesToProcess = (BufSize - *pBufIndx);
if(MaxBytesToProcess > MAX_PACKET_SIZE)
MaxBytesToProcess = MAX_PACKET_SIZE;

/////////////////////////////////////////////////

DWORD NumBytesProcessed = recv(*pSock, (char*)&pBuf[*pBufIndx], MaxBytesToProcess, 0);

/////////////////////////////////////////////////

/////////////////////////////////////////////////

if(NumBytesProcessed != 0 && NumBytesProcessed != 0xFFFFFFFF){
// Inc. our counters
*pBufIndx += NumBytesProcessed;
// Store how many bytes we've read
// Have we finished reading/writing data?
Res.IsBufferLimitReach = *pBufIndx == BufSize;
// Have we hit M or Size limit?
Res.IsPacketLimitReach = NumBytesProcessed == MAX_PACKET_SIZE;
// Return sucess...
Res.Succes = TRUE;
}

return Res;
}

/////////////////////////////////////////////////////////////////////////////////////////////////

{

if(!Res.Succes)
return FALSE;

// Do we have some msg data left or available to read?

CNetworkMsg Msg;
Msg.pData = MsgBuffer.pData;

//ProcessMsg(&Msg);
SendMessage(hHostWnd, ON_NETWORK_MESSAGE, (WPARAM)*pSock, (LPARAM)&Msg);

MsgBuffer.Init();
return TRUE;
} else {
MsgBuffer.AllocateDataBuffer();
}
}
// but not the data
// Begin or resume reading the message data
if(!Res.Succes)
return FALSE;

// Do we have some msg data left or available to read?
// Does the message have no data (MsgSize == 0)

CNetworkMsg Msg;
Msg.pData = MsgBuffer.pData;

//ProcessMsg(&Msg);
SendMessage(hHostWnd, ON_NETWORK_MESSAGE, (WPARAM)*pSock, (LPARAM)&Msg);

MsgBuffer.Init();
return TRUE;
}
}

return 0;
}



### #8Vortez

Posted 19 September 2013 - 11:19 AM

You usually prefix the lenght of the data you want to send with an int, so yea, you have to do it yourself. The "how" is up to you.

However... with a lot of work, you can automate all this process, like i did in my networking class. For example, to send data i do something like this:

struct CNetworkMsg {
DWORD MsgSize;
DWORD MsgID;
BYTE* pData;
};

...

BYTE SomeBuffer[1000];
...

CNetworkMsg Msg;
Msg.MsgID   = 1; // <-- Whatever you want...
Msg.MsgSize = 1000;
Msg.pData   = (BYTE*)&SomeBuffer[0];

Client.SendMsg(&Msg);



On the other side, the read is automated inside the network class, when it's done reading it, this virtual funciton is called automatically, and do what i want with the message received

void CVortezNetworkClientExt::OnCustomNetworkMsgRecv(SOCKET s, CNetworkMsg *pMsg)
{
//AddListboxMessageArg(MainForm->ListBox, "Custom Message Received: MsgID: 0x%8.8X MsgSize: %d (Socket: 0x%8.8X)", pMsg->MsgID, pMsg->MsgSize, (UINT)s);

switch(pMsg->MsgID)
{
case 1:
{
BYTE SomeBuffer[1000];

// useless memory copy, just an example...
memcpy(&SomeBuffer[0], pMsg->pData, pMsg->MsgSize);

// ... do something with the data
}
break;
}
}


Im remaking one of my networked application right now with it and it work like a charm. So much easier (and faster) than my previous code that was polling the socket manually each time too (i now use windows messages to handle my sockets)

You probably won't understand the code im going to post, but here's how i do my read (just to give you an idea)

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
{
BYTE  *pBuf = NULL;
DWORD *pBufIndx = NULL;

} else {
pBuf     = MsgBuffer.pData;
pBufIndx = MsgBuffer.GetDataIndxPtr();

}

// Find how many bytes we have to read, in chunk of MAX_PACKET_SIZE
DWORD MaxBytesToProcess = (BufSize - *pBufIndx);
if(MaxBytesToProcess > MAX_PACKET_SIZE)
MaxBytesToProcess = MAX_PACKET_SIZE;

/////////////////////////////////////////////////

DWORD NumBytesProcessed = recv(*pSock, (char*)&pBuf[*pBufIndx], MaxBytesToProcess, 0);

/////////////////////////////////////////////////

/////////////////////////////////////////////////

if(NumBytesProcessed != 0 && NumBytesProcessed != 0xFFFFFFFF){
// Inc. our counters
*pBufIndx += NumBytesProcessed;
// Store how many bytes we've read
// Have we finished reading/writing data?
Res.IsBufferLimitReach = *pBufIndx == BufSize;
// Have we hit M or Size limit?
Res.IsPacketLimitReach = NumBytesProcessed == MAX_PACKET_SIZE;
// Return sucess...
Res.Succes = TRUE;
}

return Res;
}

/////////////////////////////////////////////////////////////////////////////////////////////////

{

if(!Res.Succes)
return FALSE;

// Do we have some msg data left or available to read?

CNetworkMsg Msg;
Msg.pData = MsgBuffer.pData;

//ProcessMsg(&Msg);
SendMessage(hHostWnd, ON_NETWORK_MESSAGE, (WPARAM)*pSock, (LPARAM)&Msg);

MsgBuffer.Init();
return TRUE;
} else {
MsgBuffer.AllocateDataBuffer();
}
}
// but not the data
// Begin or resume reading the message data
if(!Res.Succes)
return FALSE;

// Do we have some msg data left or available to read?

CNetworkMsg Msg;
Msg.pData = MsgBuffer.pData;

//ProcessMsg(&Msg);
SendMessage(hHostWnd, ON_NETWORK_MESSAGE, (WPARAM)*pSock, (LPARAM)&Msg);

MsgBuffer.Init();
return TRUE;
}
}

return 0;
}



### #7Vortez

Posted 19 September 2013 - 11:15 AM

You usually prefix the lenght of the data you want to send with an int, so yea, you have to do it yourself. The "how" is up to you.

However... with a lot of work, you can automate all this process, like i did in my networking class. For example, to send data i do something like this:

struct CNetworkMsg {
DWORD MsgSize;
DWORD MsgID;
BYTE* pData;
};

...

BYTE SomeBuffer[1000];
...

CNetworkMsg Msg;
Msg.MsgID   = 1; // <-- Whatever you want...
Msg.MsgSize = 1000;
Msg.pData   = (BYTE*)&SomeBuffer[0];

Client.SendMsg(&Msg);



On the other side, the read is automated inside the network class, when it's done reading it, this virtual funciton is called automatically, and do what i want with the message received

void CVortezNetworkClientExt::OnCustomNetworkMsgRecv(SOCKET s, CNetworkMsg *pMsg)
{
//AddListboxMessageArg(MainForm->ListBox, "Custom Message Received: MsgID: 0x%8.8X MsgSize: %d (Socket: 0x%8.8X)", pMsg->MsgID, pMsg->MsgSize, (UINT)s);

switch(pMsg->MsgID)
{
case 1:
{
BYTE SomeBuffer[1000];

// useless memory copy, just an example...
memcpy(&SomeBuffer[0], pMsg->pData, pMsg->MsgSize);

// ... do something with the data
}
break;
}
}


Im remaking one of my networked application right now with it and it work like a charm. So much easier (and faster) than my previous code that was polling the socket manually each time too (i now use windows messages to handle my sockets)

PARTNERS