C++ Winsock2 - Receiving variable sized messages

C++ Winsock2 - Receiving variable sized messages

Page 1 of 1

1 Replies - 1563 Views - Last Post: 10 November 2008 - 01:19 AM Rate Topic: -----

#1 ggoutam7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-September 08

C++ Winsock2 - Receiving variable sized messages

Post icon  Posted 09 November 2008 - 02:15 AM

Hello,

Introduction :
- I am working on a Visual C++ program with Winsock2 to send and receive messages from a client.
- My program functions as the server. I can bind, listen and accept without any problem.
- There are various type of message with different message size involved.
- I wouldn't know what is the size of a incoming new message until I read the first few bytes.


Observations :
- All the messages sent to the client is received and recognized.
- I can receive the 1st type of message but could not receive the other type of messages, which is of different sizes.
- Every time a new (subsequent) 1st type of message is sent, my program can receive it.
- My connection is still valid. No problem with connection. No error message on connection. I can still receive as many new 1st type of messages.
- Messages were missing without any error message.


What I tried :
- I tried to increase the message size to the maximum so all the messages can fit in. It didn't work.
- I tried to read one byte by one byte of message instead of the whole bulk of message. The result is the same. I can receive the 1st type of message but could not receive the other type of messages.


As my program is very long, I am showing only the related codes below :
typedef struct R_Message
{
	char sData[20];
} ReceiveMessage;

SOCKET	   cli_sock = 0;
int		  nRetRecv = 0;
ReceiveMessage msMsgServerRecv;

memset( &msMsgServerRecv, 0x00, sizeof(ReceiveMessage) );
nRetRecv = recv( cli_sock, (char *)&msMsgServerRecv, sizeof(msMsgServerRecv), 0 );




I wonder where those messages has gone? Any idea?

with warmest regards,
goutam

Is This A Good Question/Topic? 0
  • +

Replies To: C++ Winsock2 - Receiving variable sized messages

#2 ggoutam7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-September 08

Re: C++ Winsock2 - Receiving variable sized messages

Posted 10 November 2008 - 01:19 AM

Hello friends,

I got it solved.

What I did :
I tried to read one byte by one byte of message instead of the whole bulk of message.

Root Cause of the problem :
The second and third messages were merged together with the first one. When I read one byte by one byte I can separate them out.

Thanks for those who at least read and tried.

with warmest regards,
goutam
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1