Home > Back-end >  The following program finally, why didn't only output the first line and second line output
The following program finally, why didn't only output the first line and second line output

Time:10-04

# include & lt; String>
# include & lt; Iostream>
# include & lt; Fstream>
# include "winsock2. H"
# include & lt; Time. H>
# pragma comment (lib, "ws2_32. Lib")
using namespace std;
# define DEFAULT_PAGE_BUF_SIZE 1048576
Void main () {
WSADATA WSADATA;
Int err;
Err=WSAStartup (MAKEWORD (2, 2), & amp; WsaData);
If (err!=0) return;

Char host []="hq. Sinajs. Cn";
/*
Char * request [2]={
"POST/list=HTTP/1.0 sz000401 \ r \ nHost: hq. Sinajs. Cn \ r \ nConnection: keep - the alive \ r \ n \ r \ n",
"POST/list=HTTP/1.0 sz002233 \ r \ nHost: hq. Sinajs. Cn \ r \ nConnection: keep - the alive \ r \ n \ r \ n"
};
*/
Char * request [2]={
"GeT/list=HTTP/1.0 sz000401 \ r \ nHost: hq. Sinajs. Cn \ r \ nConnection: Close \ r \ n \ r \ n",
"GeT/list=HTTP/1.0 sz002233 \ r \ nHost: hq. Sinajs. Cn \ r \ nConnection: Close \ r \ n \ r \ n"
};

Struct hostent * HP;
HP=gethostbyname (host);

If (HP==NULL) {
Cout & lt; <"Gethostbyname () error in GetIpByHost:" & lt; return;
}

//get the domain name corresponding IP
Struct in_addr inAddr.
LPSTR lpAddr;
LpAddr=HP - & gt; H_addr;
Memmove (& amp; InAddr lpAddr, 4);

Int the sock, ret=0, optval=1;
Struct sockaddr_in sa;
Sa. Sin_family=AF_INET;
Sa. Sin_port=htons (80);
Sa. Sin_addr. S_addr=inet_addr (inet_ntoa (inAddr));

The sock=socket (AF_INET SOCK_STREAM, IPPROTO_TCP);
The connect (the sock, (SOCKADDR *) & amp; Sa, sizeof (sa));
If (the sock==1) return;
If (the sock==2) return;
//send the "GET" data
//ret=send (the sock, request, strlen (request), 0).
Int m_nContentLength;
Char * pageBuf;
//int bytesRead;
M_nContentLength=DEFAULT_PAGE_BUF_SIZE;
PageBuf=(char *) malloc (m_nContentLength);

Ret=send (the sock, request [0], strlen (request [0]), 0).
//web Content, Length can be obtained from the HTTP header data "Content - Length:
"Memset (pageBuf, 0, m_nContentLength);
While (ret & gt; 0 {
Ret=recv (sock, pageBuf m_nContentLength, 0).
If (ret & gt; 0) pageBuf [ret]='\ 0';
}
cout
Ret=send (the sock, request [1], strlen (request [1]), 0).
//web Content, Length can be obtained from the HTTP header data "Content - Length:
"Memset (pageBuf, 0, m_nContentLength);
While (ret & gt; 0 {
Ret=recv (sock, pageBuf m_nContentLength, 0).
If (ret & gt; 0) pageBuf [ret]='\ 0';
}
cout
Free (pageBuf);
Closesocket (sock);
WSACleanup ();

return;
}

CodePudding user response:

Receive returned data processing not
Accept the while loop, assuming that the last returns eof character or a carriage return character, ret is 1, the pageBuf is the string contains only eof or a carriage return (pageBuf [1]='\ 0'), that is not no output?
Why use while to receive the data, it is because we can not guarantee a accept just receiving continuously, until ret is negative, the last time that the received data is what you can't guarantee, so you have to keep every time the received data to another all the (receive all data) an array of characters, rather than the last time this value preserved the received data,

CodePudding user response:

reference 1st floor qybao response:
receive returned data processing not
Accept the while loop, assuming that the last returns eof character or a carriage return character, ret is 1, the pageBuf is the string contains only eof or a carriage return (pageBuf [1]='\ 0'), that is not no output?
Why use while to receive the data, it is because we can not guarantee a accept just receiving continuously, until ret is negative, the last time that the received data is what you can't guarantee, so you have to keep every time the received data to another all the (receive all data) an array of characters, rather than the last time this value preserved the received data,


Still didn't understand how to modify, can give some hints?
  • Related