WinSock and if statement problems

Please, first off. No hard to do explanations, I am still learning about WinSock and it's functions so take it easy.

The problem I am having is, when I send data to the "server" it takes it, processes it and spits it back so I have a running log of what is happening, but the problem is, I have a command called "login" which does what we all know, but the problem comes, when I enter a bogus command like "fsdfs" and then type login, nothing comes of it, it only responds when I do it first off, then no other time until I restart the computer.

My code:
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
#include <iostream>
#include <fstream>
#include <winsock2.h>
#include <sstream>

#pragma comment(lib,"ws2_32.lib")


int main()
{
	WSADATA WsaDat;
	WSAStartup(MAKEWORD(2, 2),&WsaDat);
	SOCKET Listen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	SOCKADDR_IN serverInf;
	serverInf.sin_family = AF_INET;
	serverInf.sin_addr.s_addr =	INADDR_ANY;
	serverInf.sin_port =htons(8089);
	bind(Listen, (SOCKADDR*)(&serverInf), sizeof(serverInf));
	listen(Listen, 1);
	SOCKET TempSock = SOCKET_ERROR;
	while (TempSock == SOCKET_ERROR)
	{
		std::cout << "Waiting for incoming connections...\r\n";

		TempSock = accept(Listen, NULL, NULL);
	}
	Listen = TempSock;
	std::cout << "Client connected! @\n" ;
	char *Welcome = "Welcome to Operation!\n\r";
	char *Introduction = "My name is K.L.I.B. How may I help ? \n\n\r"; //Kompletely Lifelike Intellegent Bot
	send(Listen, Welcome, strlen(Welcome), 0);
	send(Listen, Introduction, strlen(Introduction), 0);
	
	char OrderRecv[100];
	std::stringstream OrderTemp;
	std::string Order;
	do
	{
		memset(OrderRecv, 0, sizeof(OrderRecv));
	    recv(Listen, OrderRecv, sizeof(OrderRecv), 0);

		OrderTemp << OrderRecv;
		OrderTemp >> Order;
		std::cout << OrderRecv;

		if (Order == "Login")
		{
			std::cout << "Login attempt @" /*Insert IP here*/;
		}

	} while (true);

	system("pause"); //Don't hate me, I am removing it as soon as I get this all sorted.
// Even though the while loop goes forever, I want to add a break command so this is it's
// fail safe so I can add code later
	shutdown(Listen, SD_SEND);
	closesocket(Listen);
	WSACleanup();
}
Last edited on
It's been a while since I've used winsock, but I'd check to see if you are getting any extra whitespace (specifically newlines or carriage returns) when you call recv().
How would I check for that?
I am still pretty new to winsock and it's functions.
1
2
3
	char OrderRecv[100];
	std::stringstream OrderTemp;
	std::string Order;
Do you really need all this to receive a string?

Please, first off. No hard to do explanations, ... but the problem comes, when I enter a bogus command like "fsdfs" and then type login, nothing comes of it
Your code just sits in a loop calling recv(). If you want the program to do something, you have to tell it to. I hope that isn't too hard an explanation.
Last edited on
If your "client" application works anything like telnet, where it sends each character one at a time as you type them, then I would go with the string member function "append()" http://www.cplusplus.com/reference/string/string/append/ instead of the stringstream operators on Lines 42 & 43.

I don't understand why you would have to reboot your PC because of this application though; could you go into more detail about that part?
I don't understand why you would have to reboot your PC because of this application though; could you go into more detail about that part?


This is a server program; windows most likely does not clean up automatically when the program ends and the port is "still in use" ...
That's not the case though, I compiled and ran it several times on one of my machines and I was able to reallocate that port fine with each new instance of the server app. The "Login" command actually doesn't work at all but the OP seems to claim that it does, I was wondering about that to.
Topic archived. No new replies allowed.