Unexplainable problem

I am trying to compare 2 char arrays with strcmp.

The two char arrays are both exactly the same ([e], [x], [i], [t], [0])

I am trying to send an exit command to my server.

However, strcmp will not return 0.

Code: (the strcmp is in the process() function at the bottom)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include <Windows.h>
#include <string.h>
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <iostream>

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

#define PORT "1231"
#define DEFAULT_BUFLEN 512

SOCKET ListenSocket;
SOCKET ClientSocket;

void process(const char* data);

int main()
{
	WSADATA wsaData;

	int iResult = 0;

	iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);

	if(iResult != 0)
	{
		std::cout << "WSAStartup failed.\n";
		system("pause");
		return 1;
	}

	while(1)
	{

		struct addrinfo hints, *result;

		ZeroMemory(&hints, sizeof(hints));

		hints.ai_family = AF_INET;
		hints.ai_socktype = SOCK_STREAM;
		hints.ai_protocol = IPPROTO_TCP;
		hints.ai_flags = AI_PASSIVE;

		iResult = getaddrinfo(NULL, PORT, &hints, &result);

		if(iResult != 0)
		{
			std::cout << "getaddrinfo failed.\n";
			WSACleanup();
			system("pause");
			return 1;
		}

		ListenSocket = INVALID_SOCKET;

		ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);

		if(ListenSocket == INVALID_SOCKET)
		{
			std::cout << "socket failed.\n";
			freeaddrinfo(result);
			WSACleanup();
			system("pause");
			return 1;
		}

		((sockaddr_in*)result->ai_addr)->sin_addr.S_un.S_addr = inet_addr("10.1.1.3");

		iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);

		if(iResult == SOCKET_ERROR)
		{
			std::cout << "bind failed.\n";
			closesocket(ListenSocket);
			freeaddrinfo(result);
			WSACleanup();
			system("pause");
			return 1;
		}

		freeaddrinfo(result);

		if(listen(ListenSocket, 1) == SOCKET_ERROR)
		{
			std::cout << "listen failed.\n";
			closesocket(ListenSocket);
			WSACleanup();
			system("pause");
			return 1;
		}

		ClientSocket = INVALID_SOCKET;

		ClientSocket = accept(ListenSocket, NULL, NULL);

		if(ClientSocket == INVALID_SOCKET)
		{
			std::cout << "accept failed.\n";
			closesocket(ListenSocket);
			WSACleanup();
			system("pause");
			return 1;
		}

		char recvbuf[DEFAULT_BUFLEN];
		int iSendResult;
		int recvbuflen = DEFAULT_BUFLEN;

		char DataReceived[DEFAULT_BUFLEN * 2] = {NULL};
		unsigned int c = 0;

		// Receive until the peer shuts down the connection
		do {

			iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
			if (iResult > 0)
			{
				//Do something with data received
				for(int i = 0; i < iResult; i++)
				{
					if(recvbuf[i] == '\n')
					{
						DataReceived[c] = 0;
						process(DataReceived);
						c = 0;
						continue;
					}
					DataReceived[c] = recvbuf[i];
					c++;
				}

			}
			else if (iResult == 0)
				printf("Connection closed.\n");
			else
			{
				printf("recv failed: %d\n", WSAGetLastError());
				closesocket(ClientSocket);
				closesocket(ListenSocket);
				WSACleanup();
				return 1;
			}

		} while (iResult > 0);

		closesocket(ClientSocket);
		closesocket(ListenSocket);

	}

	WSACleanup();

	return 0;
}

void process(const char* data)
{
	const char cmd[] = {'e', 'x', 'i', 't', 0};

	if(strcmp(data, cmd) == 0)
	{
		closesocket(ListenSocket);
		closesocket(ClientSocket);
		WSACleanup();
		exit(0);
	}
}
Last edited on
assign recvbuf value "exit\ndfhdfh\0" manually after line 119. If problem persist: you have problem with command handling. If not: you have problem with data sending and receiving.
And use a debugger to find problems more easily.
Last edited on
I am not sure but maybe each sentence in recvbuf is ended with CR + LF that is with '\r' + '\n'.
Ok. I realised the error of my ways.

I was using telnet to send the command to my server. Pressing enter in telnet sends \r\n so thats why it wasn't working.

Thanks for help everyone.
If someone can help me i would be greatfull :) I'm dealing with animated DFS i mean i want to animate it , on some examples runs fine but on the others dont .
Here is the 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
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <stack>
#include <windows.h>
using namespace std;
char map[5][5],render[5][5];
stack<int> s;

struct nvm { 
int rx,sx;
} st,en;

bool start=false,end = false;
int g1,g2;

int TIME = 500; //<----- VARIABLA ZA PODESAVANJE VREMENA , SVAKIH 1000 = 1 SEC, MOGUCE MJENJATI OVDJE!!

int main() {
    printf("\n");
    for(int r=0;r<5;r++) { 
       for(int s=0;s<5;s++)  {again: cin>>(map[r][s]);
                              render[r][s]=map[r][s]; 
                             if(map[r][s] == ' ') {goto again;} 
                             if((map[r][s] == 50)&&(start == false)) {st.rx = r; st.sx = s; start=true;}
                             /*printf("\nR:%d S:%d",r,s);*/}}
       
    Sleep(2000); render[st.rx][st.sx] = 'X';  Sleep(TIME); 
    s.push(st.sx); s.push(st.rx);
    
    while(end!=true)
    {
    system("CLS");
    for(int r=0;r<5;r++) { printf("\n");
       for(int s=0;s<5;s++) {printf("%c ",render[r][s]);}}
       printf("\n");
       printf("\nstart cordinates: x:%d y:%d",st.rx,st.sx);    
       
       
       
/////////////////////////////////////// LOGICKI DIO DFS-a    
//(logic part ---> that i need to fix).
     

          if ((st.sx < 4)&&(render[st.rx][st.sx+1] != 'x')&&(render[st.rx][st.sx+1] != 'X')&&(render[st.rx][st.sx+1] != '1')&&(render[st.rx][st.sx+1] != '.')/*fali dio sa velikim X*/) {if((map[st.rx][st.sx+1]== '0' || map[st.rx][st.sx+1]== '2') && (render[st.rx][st.sx+1]== '0' || render[st.rx][st.sx+1]== '2')) { render[st.rx][st.sx+1] = 'x';  st.sx++; s.push(st.sx); s.push(st.rx); if (map[st.rx][st.sx]== '2') goto fin; }}

     else if ((st.rx > 0)&&(render[st.rx-1][st.sx] != 'x')&&(render[st.rx-1][st.sx] != 'X')&&(render[st.rx-1][st.sx] != '1')&&(render[st.rx-1][st.sx] != '.')/*fali dio sa velikim X*/) {if((map[st.rx-1][st.sx]== '0' || map[st.rx-1][st.sx]== '2') && (render[st.rx-1][st.sx]== '0' || render[st.rx-1][st.sx]== '2')) { render[st.rx-1][st.sx] = 'x';  st.rx--; s.push(st.sx); s.push(st.rx); if (map[st.rx][st.sx]== '2')  goto fin; }}

     else if ((st.sx > 0)&&(render[st.rx][st.sx-1] != 'x')&&(render[st.rx][st.sx-1] != 'X')&&(render[st.rx][st.sx-1] != '1')&&(render[st.rx][st.sx-1] != '.')/*fali dio sa velikim X*/) {if((map[st.rx][st.sx-1]== '0' || map[st.rx][st.sx-1]== '2') && (render[st.rx][st.sx-1]== '0' || render[st.rx][st.sx-1]== '2')) { render[st.rx][st.sx-1] = 'x';  st.sx--; s.push(st.sx); s.push(st.rx); if (map[st.rx][st.sx]== '2')  goto fin; }}

     else if ((st.rx < 4)&&(render[st.rx+1][st.sx] != 'x')&&(render[st.rx+1][st.sx] != 'X')&&(render[st.rx+1][st.sx] != '1')&&(render[st.rx+1][st.sx] != '.')/*fali dio sa velikim X*/) {if((map[st.rx+1][st.sx]== '0' || map[st.rx+1][st.sx]== '2') && (render[st.rx+1][st.sx]== '0' || render[st.rx+1][st.sx]== '2')) { render[st.rx+1][st.sx] = 'x';  st.rx++; s.push(st.sx); s.push(st.rx); if (map[st.rx][st.sx]== '2')  goto fin; }}

   
 else if (((render[st.rx][st.sx+1]== '1')||(render[st.rx][st.sx+1]== 'x')||(render[st.rx][st.sx+1]== 'X')||(render[st.rx][st.sx+1]== '.'))&&((render[st.rx][st.sx-1]== '1')||(render[st.rx][st.sx-1]== 'x')||(render[st.rx][st.sx-1]== 'X')||(render[st.rx][st.sx-1]== '.'))&&((render[st.rx+1][st.sx]== '1')||(render[st.rx+1][st.sx]== 'x')||(render[st.rx+1][st.sx]== 'X')||(render[st.rx+1][st.sx]== '.'))&&((render[st.rx-1][st.sx]== '1')||(render[st.rx-1][st.sx]== 'x')||(render[st.rx-1][st.sx]== 'X')||(render[st.rx-1][st.sx]== '.'))) {g1 = s.top(); s.pop(); g2 = s.top(); s.pop(); render[g1][g2] = '.' ; st.rx = g1; st.sx = g2;  }// dok su svi putevi necime zablokirani    
   Sleep(TIME); 
   }
///////////////////////////////////////
    fin:
    render[st.rx][st.sx] = 'X';
    Sleep(TIME); 
    system("CLS");
    for(int r=0;r<5;r++) { printf("\n");
       for(int s=0;s<5;s++) {printf("%c ",render[r][s]);}}
       printf("\n");
       printf("\nstart cordinates: x:%d y:%d",st.rx,st.sx);


               
    Sleep(3000);
return 0 ; }


And here are the examples of matrix:

2 1 1 1 1
0 0 1 0 1
0 1 1 0 0 // runs good , but wrong animations
0 0 0 0 1
0 1 2 1 1

2 0 0 1 2
1 0 1 1 0
0 0 0 1 0 // never runs till end
1 0 1 1 0
0 0 0 0 0

0 0 2 1 2
0 0 1 0 0
0 1 0 0 1 // works perfectly :)
0 0 1 0 0
1 0 0 0 1

Plz if you can tell me what runtime error couse first two problems , please subsmit.. (i know i did so much mess in the code ).

Last edited on
Topic archived. No new replies allowed.