Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 775047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:13:38+00:00 2026-05-14T19:13:38+00:00

So, I have this source: #include <winsock2.h> #include <windows.h> #include <iostream> #pragma comment(lib,"ws2_32.lib") using

  • 0

So, I have this source:

#include <winsock2.h>
#include <windows.h>
#include <iostream>
#pragma comment(lib,"ws2_32.lib")
 
using namespace std;
 
int main (){
        WSADATA wsaData;
 
    if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
                cout << "WSAStartup failed.\n";
        system("pause");
                return 1;
    }
 
        SOCKET Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
 
        struct hostent *host;
        host = gethostbyname("www.newegg.com");
 
        SOCKADDR_IN SockAddr;
        SockAddr.sin_port=htons(80);
        SockAddr.sin_family=AF_INET;
        SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
 
        cout << "Connecting...\n";
        if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr)) != 0){
                cout << "Could not connect";
                system("pause");
                return 1;
        }
        cout << "Connected.\n";
 
        send(Socket,"GET / HTTP/1.1\r\nHost: www.newegg.com\r\nConnection: close\r\n\r\n", strlen("GET / HTTP/1.1\r\nHost: www.newegg.com\r\nConnection: close\r\n\r\n"),0);
        char buffer[10000];
 
        int nDataLength;
        while ((nDataLength = recv(Socket,buffer,10000,0)) > 0){               
                int i = 0;
                while (buffer[i] || buffer[i] == '\n' || buffer[i] == '\r') {
                        cout << buffer[i];
                        i += 1;
                }
        }
 
        closesocket(Socket);
        WSACleanup();
 
        system("pause");
        return 0;
}

Now, it works getting the source, however, it keeps repeating afterwards. For example:

Notice how it says , then continues? I was wondering how I can avoid that. I know I can limit the buffer, however is there a better solution? Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T19:13:38+00:00Added an answer on May 14, 2026 at 7:13 pm

    Try changing

        while (buffer[i] || buffer[i] == '\n' || buffer[i] == '\r') {
                cout << buffer[i];
                i += 1;
        }
    

    to

        for(; i != nDataLength; ++i)
                cout << buffer[i];
    

    It looks like you are reading past the end of the buffer into garbage data used by recv and friends. Nobody said the data returned had to be null terminated which is what the while loop assumes for correct behavior.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 492k
  • Answers 492k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer SELECT id, DATE(A.Inspection_datetime) AS Date, A.Model, COUNT(A.Serial_number) AS Qty, B.name… May 16, 2026 at 10:25 am
  • Editorial Team
    Editorial Team added an answer Looks like it's just Application.version -- it returns a String… May 16, 2026 at 10:25 am
  • Editorial Team
    Editorial Team added an answer Please consider what you are broadly trying to do, and… May 16, 2026 at 10:25 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have the following class #ifndef Container_H #define Container_H #include <iostream> using namespace std;
I have this class header //header for class. #ifndef Container_H #define Container_H #include <iostream>
I have this WxWidgets test source code that compiles, and when run, it shows
I have this path and it is correct however, the browser will not include
Let's say I have this directory structure: SConstruct src/ a.cpp b.cpp include/ a.h b.h
I'm using libarchive and I have included the source and headers in my iphone
I have this regex: ^\/\* to check and see if a file contains those
this is my first time using Eclipse, and my first time programming in a
We build software using Hudson and Maven. We have C#, java and last, but
Problem: I have a large Visual C++ project that I'm trying to migrate to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.