Error on client-server connection via socket

-1

I'm a beginner in c ++ and I'm having trouble getting my client code to establish a connection to my server in Python, the server is running, but the client always goes into error 4 and does not complete the connection, which I should to do?

#include<winsock.h>
#include<string>
#include<iostream>
#include<vector>
#include<fstream>

SOCKET s; 
int contador = TRUE;

int main()
{
    while ( contador )
    {
        int minhaPorta = 5000;
        char meuHost = '127.0.0.1';
        WSADATA wsadata;
        int error = WSAStartup(0x0202, &wsadata);

        if (error)
        {
            std::cout<< "erro 1";
            contador=FALSE;
        }

        if (wsadata.wVersion != 0x0202)
        {
            WSACleanup();
            std::cout<<"erro 2";
            contador=FALSE;
        }

        SOCKADDR_IN target;
        target.sin_family = AF_INET;
        target.sin_port = htons (minhaPorta);
        target.sin_addr.s_addr = inet_addr (&meuHost);
        s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);

        if (s == INVALID_SOCKET)
        {
            std::cout<<"erro 3";
            contador=FALSE;
        }

        //Try connecting...
        if (connect(s, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR)
        {
            std::cout<<"erro 4";
            contador=FALSE;
        }
        else
        {
            std::cout<<"acerto 1";
            contador=TRUE;
        }
    }
}
void CloseConnection ()
{
    if (s)
    std::cout<<"entrou aqui ";
    closesocket(s);
    WSACleanup();
}
    
asked by anonymous 25.05.2017 / 02:42

1 answer

2

When testing your code, the compiler gives the following error:

program.cpp:23:16: warning: character constant too long for its type
 char meuHost = '127.0.0.1';
  

Everything indicates that you are trying to pass a string using a char.

Change this line to:

const char *meuHost = "127.0.0.1";

And this:

target.sin_addr.s_addr = inet_addr (&meuHost);

To:

target.sin_addr.s_addr = inet_addr(meuHost);
    
25.05.2017 / 10:29