winsock recv not working...

Hi! I have a windows client/server pair that are supposed to chat with each other using buffers. The client connects fine to the server, and the server sends the client, and the client sends the server. THEN, when I am writing the SECOND server message, the server receives before the client has a chance to send, and then outputs garbage, and then goes to the send point again where I type a message to send to the client. However, the client works FINE if I ignore the garbage and send a message to the server as normal. Here's my relevant code (you will see I have several breakpoints installed):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
(SERVER)
                    do {
			std::cout << "begining loop...\n";
			er = recv(jake, recvstr, DEFAULT_BUFFER_LENGTH, NULL);
			std::cout << "receving...\nrecv = '" << recvstr << "'\n";
			if (er == 0) {
				std::cout << "client closed connection\n";
			} else if (er < 0) {
				error("error with receiving data");
				return (-1);
			} else if (er > 0) {
				std::cout << "printing recv...\n";
				std::cout << recvstr << "\n";
				std::cout << "enter stuff\n";
				std::cin >> st;
				strcpy_s(msg, st.c_str());
				std::cout << "your messages were:" << msg;
				std::cout << "\n";
				er = send(jake, msg, DEFAULT_BUFFER_LENGTH, NULL);
				if (er == SOCKET_ERROR) {
					std::cout << "\nerror with sending: your last message was not sent\n";
				}
				std::cout << "going...\n";
				//goto e;
			} else {
				error("undefined error");
				return 1;
			}
			//e:
			std::cout << "reseting...\n";
			memset(msg, '\0', sizeof(msg));
			memset(recvstr, '\0', sizeof(recvstr));
			st.clear();
		} while (er > 0);
	} while(1==1);

(CLIENT)

            do {
		std::cout << "begining loop...\n";
		er = recv(bill, recvstr, DEFAULT_BUFFER_LENGTH, NULL);
		std::cout << "receving...\nrecv = '" << recvstr << "'\n";
		if (er == 0) {
			std::cout << "server closed connection\n";
		} else if (er < 0) {
			std::cout << "recv failed\n";
		} else if (er > 0) {
			std::cout << "printing recv...\n";
			if (e)
				std::cout << "___chat begins here___\n";
			std::cout << recvstr << "\n";
			if (e)
				std::cout << "type a message to send the server (the other person)\n";
			std::cout << "enter stuff\n";
			std::cin >> st;
			strcpy_s(msg, st.c_str());
			std::cout << "your messages were:" << msg << "\n";
			std::cout << "\n";
			er = send(bill, msg, DEFAULT_BUFFER_LENGTH, 0);
			if (er == SOCKET_ERROR) {
				std::cout << "\nyour last message was not sent\n";
				std::cout << "error: " << WSAGetLastError() << "\n";
			}
			std::cout << "going...\n";
		} else {
			std::cout << "\nan unknown error with an unknown error code";
		}
		std::cout << "reseting...\n";
		e = false;
		memset(msg, '\0', sizeof(msg));
		memset(recvstr, '\0', sizeof(recvstr));
		st.clear();
	} while (er > 0);


Thank you for the help. If you need any more code, I will upload.
closed account (48bpfSEw)
did you compared your solution with other source code in web?

for example this one:

http://www.binarytides.com/receive-full-data-with-recv-socket-function-in-c/


@Necip yes I did, that didn't help. It was that exact same one and others like it.

What eventually helped is re-declaring recvstr like so:

instead of: memset(recvstr, '\0', sizeof(recvstr));,
do char recvstr[(whatever your buffer length is)]; //this is re-declaring recvstr every time so it wipes the memory

The reason for this is because even if you are wiping recvstr (or whatever your buffer is), it is still declared and has content (be it a bunch of nulls). Apparently, the recv function knows its received by seeing if recvstr is set, so it doesn't work to just memset it.

Anyways, my case was special, and most times you don't have to worry about this sort of thing.
That d way it work.
Topic archived. No new replies allowed.