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

  • SEARCH
  • Home
  • 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 6856129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:50:45+00:00 2026-05-27T01:50:45+00:00

I am following a tutorial that teaches me how to use win32 sockets(winsock2). I

  • 0

I am following a tutorial that teaches me how to use win32 sockets(winsock2). I am attempting to create a simple socket that connects to the “localhost” but my program is failing when I attempt to connect to the local host(at the function connect()).

Do I need admin privileges to connect to the localhost? Maybe thats why it fails? Maybe theres a problem with my code? I have tried the ports 8888 & 8000 & they both fail.

Also if I change the port to 80 & connect to http://www.google.com I can connect BUT I get no response back. Is that because I haven’t sent a HTTP request or am I meant to get some response back?

Here’s my code (with the includes removed):

// Constants & Globals //
typedef unsigned long IPNumber;    // IP number typedef for IPv4
const int SOCK_VER    = 2;
const int SERVER_PORT = 8888;  // 8888
SOCKET mSocket        = INVALID_SOCKET;
SOCKADDR_IN sockAddr  = {0};
WSADATA wsaData;
HOSTENT* hostent;


int _tmain(int argc, _TCHAR* argv[])
{
    // Initialise winsock version 2.2
    if (WSAStartup(MAKEWORD(SOCK_VER,2), &wsaData) != 0)
    {
        printf("Failed to initialise winsock\n");
        WSACleanup();
        system("PAUSE");
        return 0;
    }

    if (LOBYTE(wsaData.wVersion) != SOCK_VER || HIBYTE(wsaData.wVersion) != 2)
    {
        printf("Failed to load the correct winsock version\n");
        WSACleanup();
        system("PAUSE");
        return 0;
    }

    // Create socket
    mSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (mSocket == INVALID_SOCKET)
    {
        printf("Failed to create TCP socket\n");
        WSACleanup();
        system("PAUSE");
        return 0;
    }


    // Get IP Address of website by the domain name, we do this by contacting(??) the Domain Name Server
    if ((hostent = gethostbyname("localhost")) == NULL)  // "localhost"  www.google.com
    {
        printf("Failed to resolve website name to an ip address\n");
        WSACleanup();
        system("PAUSE");
        return 0;
    }

    sockAddr.sin_port             = htons(SERVER_PORT);
    sockAddr.sin_family           = AF_INET;
    sockAddr.sin_addr.S_un.S_addr = (*reinterpret_cast <IPNumber*> (hostent->h_addr_list[0]));
    // sockAddr.sin_addr.s_addr=*((unsigned long*)hostent->h_addr);  // Can also do this

    // ERROR OCCURS ON NEXT LINE: Connect to server
    if (connect(mSocket, (SOCKADDR*)(&sockAddr), sizeof(sockAddr)) != 0)
    {
        printf("Failed to connect to server\n");
        WSACleanup();
        system("PAUSE");
        return 0;
    }

    printf("Got to here\r\n");
    // Display message from server
    char buffer[1000];
    memset(buffer,0,999);
    int inDataLength=recv(mSocket,buffer,1000,0);
    printf("Response: %s\r\n", buffer);

    // Shutdown our socket
    shutdown(mSocket, SD_SEND);

    // Close our socket entirely
    closesocket(mSocket);

    // Cleanup Winsock
    WSACleanup();

    system("pause");
    return 0;
}
  • 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-27T01:50:46+00:00Added an answer on May 27, 2026 at 1:50 am

    Someone should be listening in the port you are connecting to. Try writing a small server program that will listen for incoming connections. If connect succeeds obviously you are connected to google but you need to send a HTTP request (from C? – that would be difficult).

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

Sidebar

Related Questions

I'm following a php pagination tutorial that uses MYSQL but I use MYSQLI object
I've been following a tutorial that explains how to use background for a button
I'm following a simple tutorial that creates a class which extends from SQLiteOpenHelper and
i am following a tutorial on a simple cocos2d game . however on that
The following is a simple bit of code that can be used to create
I am going through a tutorial that mentions the following method: -(void)layoutSubviews located in
I'm using the following tutorial http://developer.android.com/resources/tutorials/views/hello-mapview.html in order to create a map view and
I am following this tutorial that is like the hello world for post-commit I
I'm following along a tutorial (Ruby) that uses a regex to remove all html
I ran across a tutorial that had the following syntax: rails generate model User

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.