I saw it possible. But I can not. I see what this perchance is. But for me this difficulry.

struct addrinfo addr; struct addrinfo *urlAddr; struct sockaddr_in addrList; char buff[4000]; // addrList = (sockaddr_in *)&((sockaddr_in *)&urlAddr->ai_addr)->sin_addr.s_addr; WSADATA wsaData; int connSock = WSAStartup(MAKEWORD(2, 2), &wsaData); if (0 != connSock) { cout << "Error 1."; } memset(&addr, 0, sizeof(addr)); addr.ai_family = AF_INET; addr.ai_socktype = SOCK_STREAM; addr.ai_protocol = IPPROTO_TCP; int iResult = getaddrinfo((const char *)&"goatghosts.000a.biz", 0, (struct addrinfo *)&addr, &urlAddr); if (iResult != 0) { cout << "Error " << iResult << "\n"; } addrList.sin_family = AF_INET; addrList.sin_addr.s_addr = ((sockaddr_in *)&urlAddr->ai_addr)->sin_addr.s_addr; addrList.sin_port = htons(80); SOCKET so = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP); if (connect(so, (const sockaddr *)&addrList, sizeof(addrList)) != 0) { cout << "Error connect " << WSAGetLastError() << "\n"; } int colbyte = recv(so, buff, sizeof(buff), 0); cout << colbyte; return 0; 

If you enter ya.ru, then we wait two minutes and give error 10060. And if there is any other address, it instantly gives 10049. Tell me, please, what am I doing wrong?

  • Why sockets? There are a lot of more convenient and simple ways to work with HTTP-servers. - Vladimir Martyanov
  • 10060 - timed out 10049 - can not be bogged down (the opposite side does not open a socket) - KoVadim
  • And the opposite side will not open the socket at all if I address through connect? I just want to put an end once and for all. For me, using sockets would be much more beneficial than using HTTPRequest and HTTPResponse. Or are there other methods of standard Visual C ++ features? - Vyacheslav
  • Tell me, did I use the addrinfo and getaddrinfo structure correctly? - Vyacheslav
  • At first glance, you have become too smart (there’s no time to disassemble in detail). An example from man getaddrinfo for the client program after replacing SOCK_DGRAM with SOCK_STREAM for the mail.ru address worked fine. - avp

1 answer 1

Actually, firstly, it would be more correct to do what you have already been indicated in the comments - to work with http in other more convenient ways. Still, in this case, to go down to a lower level, you need good reasons. For example, to study the structure of a bicycle (but not to reinvent it in any way).

Secondly, as was also noted in the comments, you really are too smart (especially with addressing).

Thirdly, even if you deal with errors in addressing, you will not wait for anything. Why? Yes, because you do not ask anything from the server.

If to summarize everything, then we have about the following output:

  struct addrinfo *urlAddr, addr; char buff[4000]; //обратите внимание, что здесь указывается также порт. //Он в последствие записывается в urlAddr и будет //использован при подключении. Указывать отдельно его //не придется. int iResult = getaddrinfo("goatghosts.000a.biz", "80", &addr, &urlAddr); if (iResult != 0) std::cout << "Error " << iResult << "\n"; int so = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //Вы уже получили структуру с адресом и портом для //подключения. Соответственно в коннекте ее и используете. //Нет необходимости еще в каких-то дополнительных //преобразованиях и телодвижениях. if (connect(so, urlAddr->ai_addr, urlAddr->ai_addrlen) != 0) std::cout << "Error connect " << "\n"; //Если хотите что-то получить от сервера, то сформируйте //хоть какой-то минимальный запрос. Когда Вы обращаетесь //по указанному адресу в браузере, то он за Вас отсылает //его - таковы уж требования протокола. А уж коль Вы //опускаетесь на уровень ниже и решили сами реализовывать //обмен по протоколу HTTP, то следуйте его "традициям" //и требованиям. std::string str = "GET / HTTP/1.1\n"; str+="Host: goatghosts.000a.biz\n\n"; send(so, (void*)str.c_str(), str.size(),0); int colbyte = recv(so, buff, sizeof(buff), 0); std::cout << colbyte; 
  • How sad for me to see it now. Because I came to this forum to share my healthy code, which I shoveled during the day of oppression. I was very depressed that even find () does not want to work with me and because of this, such bad code came out. But in the evening I took my mind. Firstly, I received a response from recv "0 bytes received", for this I read an article from the comments above, many thanks to the person who left us this. Answer 0 made me understand that the socket on the other side does not open. - Vyacheslav
  • I decided to use HTTP as a class. It turned out that such a built-in does not exist. What made me understand that all the same, I was right, that I started digging in sockets. I do not like to use someone else's code, I like to write my own, in C ++ it turns out badly. Therefore, we google the "http request device" the first line gives us a theory. And with this theory, if we are able to use sockets at the byte level, the response from the server will be 326 bytes. "ya.ru". - Vyacheslav
  • one
    @ Vyacheslav In principle, I myself belong to those people who do not like to use someone else's code and like to write their own. But unfortunately, this is not the case. I myself use the curl library for working with http — it’s time tested, cross platform, there is an extensive manual and many examples on the web. And work with sockets needs to be studied. Their use is very, very wide. - Max ZS