Wrote a socket client in C ++. It connects to servers normally, but when I send a request, I receive either the error code "400 Bad Request". For example, url = 77.120.111.7 and port = 80 I send the request "GET / HTTP / 1.0" I receive, or "400 Bad Request", or "emptiness".

int main(){ char url[100]; int port; char name[30]; std::cout << "Url: "; cin >> url; std::cout << "Port: "; cin >> port; WSADATA WsaData; if (int err = WSAStartup (MAKEWORD(2, 0), &WsaData) != 0) { std::cout << "Socket not Loaded!\n"; } gethostname(name, 30); int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock == -1){ std::cout << "Error! Socket no created.\n" ; } hostent *hp=NULL; hp = gethostbyname("localhost"); sockaddr_in addr; addr.sin_family=AF_INET; addr.sin_port=htons(port); addr.sin_addr.s_addr=inet_addr(url); int locate; locate = connect(sock, (sockaddr *)&addr, sizeof(addr)); if (locate < 0){ std::cout << "Fatal Error!\n"; system("pause"); }else{ char cut[30]; char get[100000]; string hosted; std::cout << "Connected... " << url << "\n"; do{ std::cout << "Output: "; cin >> cut; send(sock, cut, 30, 0); recv(sock, get, 512, 0); std::cout << "Command: " << get << "\n"; }while(locate == 0); } } 

    1 answer 1

    Oh ... Task: send the string GET / HTTP/1.0\r\n\r\n to the server and get an answer. Those. There is no need to arrange the send / recv carousel - send once, and how much it will turn out - recv. Detect end of page - return 0 from recv. Here it is not even analyzed, but a cycle is in locate==0 , while locate==0 (And who would change this locate?) There is still clearly not the way the construction cin >> cut; . And be careful with the ends of the lines <CR> <LF> Here is the text with corrections, if there are questions, I will try to answer.

     #include <iostream> #include <winsock.h> #include <ipexport.h> #include <windows.h> #include <string> using namespace std; int main(){ char url[100]; int port; char name[30]; std::cout << "Url: "; cin >> url; std::cout << "Port: "; cin >> port; cin.get(); /* !!!! */ WSADATA WsaData; if (int err = WSAStartup (MAKEWORD(2, 0), &WsaData) != 0) { std::cout << "Socket not Loaded!\n"; } gethostname(name, 30); int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock == -1){ std::cout << "Error! Socket no created.n" ; } hostent *hp=NULL; hp = gethostbyname("localhost"); sockaddr_in addr; addr.sin_family=AF_INET; addr.sin_port=htons(port); addr.sin_addr.s_addr=inet_addr(url); int locate; locate = connect(sock, (sockaddr *)&addr, sizeof(addr)); if (locate < 0) { std::cout << "Fatal Error!\n"; system("pause"); } else { string cut; char get[100000]; int l; string hosted; std::cout << "Connected... " << url << endl; std::cout << "Output: " << flush; getline(cin, cut); cut += "\r\n\r\n"; send(sock, cut.data(), cut.length(), 0); do{ l = recv(sock, get, 512, 0); std::cout << "Command: " << get << endl; } while (l); } } 

    Edits will be noticed. And also, where does the style of writing << "\n" come from?

    • what the data (), getline (), length () functions mean I just want to know the detailed description, tell me the literature! - pro
    • I roughly understand the syntax of what they mean, but I want to fully explore the possibilities and meanings, as well as tell me the literature on the topic "String Processing" and "Data Transfer". Thanks in advance. "And from where is the manner of writing <<" \ n "?" - I don’t know before I wrote "endl", but when I saw that it was possible "\ n", from there and manner. I just learned PHP first and then C ++ and it seemed to me more convenient. - pro
    • And unless "getline (cin, cut)" is correct, it can be like cin.getline (cut, sizeof (cut)); And about the locate all right, it gives the opportunity to send more than one request! - pro
    • data () and length () are string class methods. getline () is a function from the string library. cin.getline is a method from istream, but there cut d.b. type char * . As for cout << "\n" , endl, besides outputting the end of the line, it also makes flush, i.e. output buffer to stream. And the literature - so on a C ++. I use www.cplusplus.com for reference, but with helpers in C ++, others will probably be better advised. - alexlz