Solved

Winsock Recv/Send problem

Posted on 2009-07-13
3
1,592 Views
Last Modified: 2013-11-17
I've been having one hell of a problem with a program I'm making.  Basically, it's inter-process communication using sockets. I've done sockets for years, and just recently started running into this roadblock that I can't figure out.  I've gotten it to connect to the "server" (which is just another program running a bind on 127.0.0.1), but when we exchange information, I've used a set of MessageBox's to show me the data going between the two.  The MessageBox's hault program execution until user input, but from what I understand, messages are supposed to queue up, right? Anyways, this is what I'm getting (an example conversation):

-Connect to localhost-
server SEND: 0,CONNECTED
client RECV: 0,CONNECTED_TO_SERVER

client SEND: 1,COMMAND,1,ARGS,HERE
server RECV: 1,COMMAND,1,ARGS,HERE

server SEND: 2,FLUSH
client RECV: 2,FLUSHCTED_TO_SERVER

-----

You'll notice that my client is recv'ing but it almost looks like the buffer/recv queue I'm using isn't being flushed properly and it has remnants of previous packets in it.

Maybe it has something to do with my methodology? I'm using a while() loop on a non-blocking socket inside of a different thread in my GUI, and skipping past anything with a WSAEWOULDBLOCK error to recieve everything.  I hope the experts aren't laughing at me at this point :P

Here's the strange part...I'm doing EVERYTHING I can to zero this buffer out though..during every loop iteration, I set my buffer's 0 index to 0x00 like so:
recvBuff[0] = 0x00;
Which should set the string to null (just to make sure I'm not overwiting what I had)..from the server side, I used a MessageBox to show me what the server was sending, and it IS sending properly formatted packets, so I know the problem is on my client end...I even tried taking the recvBuff upon a successful recv call and doing a strncpy into a NEW buffer based on the size of data receieved like so:

lResult = recv(socket,recvBuff,512,0);
if(lResult > 0)
{
   strncpy(buff2,recvBuff,lResult);
  ..etc..
}

It probably has a whole lot to do with my loop, but I can't for the LIFE of me figure out why my buffer isn't clearing/why my socket recv data is all mashing together.

OH I FORGOT -- If I put a message box after the recv call showing the AMOUNT of data I received like so:

sprintf(buffer,"%i -- Number of bytes recvd",lResult);
MessageBox(0,buffer,"Recv",0);

Then it actually shows me the correct number AND parses correctly..but only if that message box is there. Is it a speed issue? What the hell is going on?? lol

Any help at all would be GREATLY appreciated...and if you can think of a better way to do winsock in C++ (I'm using MingW via Ultimate++), perhaps async sockets or something, PLEASE let me know. I'm about to smash my computer due to this lol :P

-Kevin
0
Comment
Question by:ProjectZIG
3 Comments
 
LVL 16

Expert Comment

by:George Tokas
ID: 24848619
With C++ Builder implementation is easy...
What compiler are you using??

George Tokas.
0
 

Author Comment

by:ProjectZIG
ID: 24850672
I'm using MinGW 32 and the Ultimate++ development environment.

http://www.ultimatepp.org/
0
 
LVL 27

Accepted Solution

by:
BigRat earned 500 total points
ID: 24961690
>> I'm using isn't being flushed properly and it has remnants of previous packets in it.

Which looks as if you're treating the socket connection as a packet sending/receiving system.

>> I've done sockets for years

Which suggests that you know that the system is a stream system.

Could you be a little more specific on what "client SEND: 1,COMMAND,1,ARGS,HERE" is?

In particular what exactly contitues a "packet"?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
topping1 challenge 7 72
How  do I get an older program to run in Windows 10? 20 103
Device same like our heart 12 75
iframe detection of parent window scale 20 60
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now