简体   繁体   中英

Listen/output bytes from port 80 on Linux

I am teaching myself some Linux network programming, just getting a feel for how it works. I found this tutorial .

which will print out the first X bytes returned from the google server. I have tried this example and it works. However, I was wondering how would I modify the code so that I could just output whatever bytes came through on a particular port number?

I am working with C/C++ on Linux Mint.

From the link that you are reading, the code has been written to read the first 1000 bytes of information and stop with that.

bytes_received = recv(socketfd, incoming_data_buffer,1000, 0);

If you want to read all the bytes returned from your request you would need to use a while loop like the one below:

while(bytes_received = recv(socketfd, incoming_data_buffer,1000, 0)>=0){
    if (bytes_received == 0) 
        break;
    std::cout<<incoming_data_buffer<<endl; //Output the data
    std::cout<<bytes_received<<endl; //Output the number of bytes recieved
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM