May 21, 2020 at 5:15am UTC
When you say "contains," do you mean that its value actually "GIMME"? Or is "GIMME" among other characters, e.g., "FOOBARGIMME"?
May 21, 2020 at 5:17am UTC
The value holds "GIMME"... No other characters...
May 21, 2020 at 5:22am UTC
When does the variable receive that value? If you use
buf = "GIMME" ;
right before the conditional statement, does it work?
May 21, 2020 at 5:25am UTC
Does buf contain a c-string? If so, use strcmp(), not ==.
But you would be better with std::string
Last edited on May 21, 2020 at 5:26am UTC
May 21, 2020 at 5:38am UTC
My code is basically as follows:
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
#include <ws2tcpip.h>
#include <stdio.h>
#include<windows.h>
#include<fstream>
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"wininet.lib")
#include<wininet.h>
#pragma warning(disable : 4996)
const char * x = "127.0.0.1" ;
int main() {
WSADATA wsa;
SOCKET s;
struct sockaddr_in server;
int recv_size;
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
printf("\n" );
printf("You Have An Error!" );
printf("\n" );
Sleep(20000);
exit(0);
}
if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
printf("\n" );
printf("You Have An Error!" );
printf("\n" );
Sleep(20000);
exit(0);
}
server.sin_addr.s_addr = inet_addr(x);
server.sin_family = AF_INET;
server.sin_port = htons(80);
if (connect(s, (struct sockaddr*) & server, sizeof (server)) < 0)
{
printf("\n" );
printf("You Have An Error!" );
printf("\n" );
Sleep(20000);
exit(0);
}
printf("\n" );
printf("You Are CONNECTED!!!" );
printf("\n" );
for (;;)
{
char buf[3000] = "" ;
if ((recv_size = recv(s, buf, 2000, 0)) == SOCKET_ERROR)
{
break ;
}
buf[recv_size] = '\0' ;
if (buf == "GIMME" ) {
printf("\n" );
printf("Its A Match!" );
Sleep(5000);
exit(0);
}
else
{
printf("\n" );
printf("Its Not A Match!!!" );
Sleep(5000);
exit(0);
}
}
Sleep(3000);
}
Last edited on May 21, 2020 at 5:45am UTC
May 21, 2020 at 5:47am UTC
char buf[3000] ... recv(s, buf, 2000 , 0)
Why do you leave almost a kB of memory untouched?
Last edited on May 21, 2020 at 5:57am UTC
May 21, 2020 at 6:45am UTC
[EDIT belatedly saw lastchance's response about C-strings and strcmp...]
Line 67
if (buf == "GIMME" ) {
is comparing the
memory address of the buffer
buf
against the address of the string constant "Its A Match!" These will never be equal, obviously.
To check to see if the buffer contains the string use something like:
if (0 == strcmp(buf, "GIMME" )) {
Andy
PS If you haven't already done so, you should configure your compiler to use verbose warnings, e.g. Level 4 for Microsoft Visual C++ There should have been a warning about line 67:
C4130 : '==' : logical operation on address of string constant
Last edited on May 21, 2020 at 7:00am UTC
May 21, 2020 at 1:57pm UTC
this is one of many reasons to use c++ string. Those support the == comparison which raw arrays do not.