c++ - Getting 400 Bad Request when trying to connect to website via winsock socket -
i made socket i've been adding on week , i've come across problem. set port on local 127.0.0.1 ip address allow me connect own computer , when recieve response computer says "400 bad request. request badly formed.". think has http header information send via send();
function. sendbuf
contains header information sent. here's code:
#include <windows.h> #include <winsock2.h> #include <conio.h> #include <stdio.h> #include <iostream> using namespace std; #define sck_version2 0x0202 #define default_buflen 2000 #define default_port 27015 namespace globals{ extern string input = ""; } using namespace globals; void username() { { printf("username: \t"); getline(cin, input); if ( input == "user name" ) { break; } } while(true); } void password() { { printf("password: \t"); getline(cin, input); if ( input == "password" ) { break; } username(); } while(true); } int whole() { username(); password(); //---------------------- // declare , initialize variables. wsadata wsadata; int iresult; socket connectsocket = invalid_socket; struct sockaddr_in clientservice; char name[500] = ""; char ipaddress[500] = ""; char sport[500] = ""; sockaddr_in sname; int snamesize = sizeof(sname); char *sendbuf = "get /tr http/1.1 \nhost: net.tutsplus.com \nuser-agent: mozilla/5.0 (windows; u; windows nt 6.1; en-us; rv:1.9.1.5) gecko/20091102 firefox/3.5.5 (.net clr 3.5.30729) \naccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 \naccept-language: en-us,en;q=0.5 \naccept-encoding: gzip,deflate \naccept-charset: iso-8859-1,utf-8;q=0.7,*;q=0.7 \nkeep-alive: 300 \nconnection: keep-alive \ncookie: phpsessid=r2t5uvjq435r4q7ib3vtdjq120 \npragma: no-cache \ncache-control: no-cache"; char recvbuf[default_buflen]; int recvbuflen = default_buflen; //208.109.181.178 int wsaerror = wsagetlasterror(); //system("color 04"); //---------------------- // initialize winsock iresult = wsastartup(makeword(2,2), &wsadata); if (iresult != no_error) { printf("wsastartup failed: %d\n", iresult); return 1; } //---------------------- // create socket connecting server connectsocket = socket(af_inet, sock_stream, ipproto_tcp); if (connectsocket == invalid_socket) { printf("error @ socket(): %i\n", wsagetlasterror() ); wsacleanup(); return 1; } //---------------------- // sockaddr_in structure specifies address family, // ip address, , port of server connected to. printf("ip address: \n"); cin >> ipaddress; printf("port: \n"); cin >> sport; u_short port = strtoul(sport, null, 0); clientservice.sin_family = af_inet; clientservice.sin_addr.s_addr = inet_addr(ipaddress); //74.125.196.191 clientservice.sin_port = htons(port); //---------------------- // connect server. iresult = connect( connectsocket, (sockaddr*) &clientservice, sizeof(clientservice) ); if ( iresult == socket_error) { closesocket (connectsocket); printf("unable connect server: %i\n", wsagetlasterror()); wsacleanup(); return 1; } //---------------------- //get local host name iresult = gethostname(name, sizeof(name)); if (iresult == no_error) { printf("host name: %s\n", name); } else if (iresult == socket_error) { printf("could not resolve host name: %i", wsagetlasterror()); } //------------------------ //get peer name iresult = getpeername(connectsocket, (struct sockaddr*)&sname, &snamesize); if (iresult == no_error) printf("peer name: %s\n", inet_ntoa(sname.sin_addr)); else if (iresult == socket_error) printf("could not peer name: %i\n", wsagetlasterror()); //------------------------- // send initial buffer iresult = send( connectsocket, sendbuf, (int)strlen(sendbuf), 0 ); if (iresult == socket_error) { printf("send failed: %d\n", wsagetlasterror()); closesocket(connectsocket); wsacleanup(); return 1; } else printf("bytes sent: %i\n", iresult); //----------------------------- // shutdown connection since no more data sent iresult = shutdown(connectsocket, sd_send); if (iresult == socket_error) { printf("shutdown failed: %d\n", wsagetlasterror()); closesocket(connectsocket); wsacleanup(); return 1; } // receive until peer closes connection { iresult = recv(connectsocket, recvbuf, recvbuflen, 0); if ( iresult > 0 ) { printf("bytes received: %d\n", iresult); //printf("bytes received: %d\n", iresult); printf("from server: %s\n", recvbuf); } else if ( iresult == 0 ) printf("connection closed\n"); else if (wsaerror == wsaetimedout) printf("recv failed: wsaetimedout\n"); printf("do want disconnect? (y/n) \n"); cin >> input; if ( input == "y"||"y" ) { break; } else if ( input == "n"||"n" ) { break; } } while( iresult > 0 ); // cleanup closesocket(connectsocket); wsacleanup(); system("pause"); return 0; } int main() { { whole(); } while( input != "n"||"n" ); }
i figured out, in sendbuf
putting invalid host name. xd
Comments
Post a Comment