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 3484930
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:50:47+00:00 2026-05-18T10:50:47+00:00

New to C++ express, and after follow the winsock tutorial, getting countless of error.

  • 0

New to C++ express, and after follow the winsock tutorial, getting countless of error.
Already link the include Windows SDK 7.1 to the properties. What am I missing?

#include <winsock2.h>
#include <stdio.h>
#include <stdafx.h>

#pragma comment(lib, "Ws2_32.lib")
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

int main(int argc, char **argv)
{
     WSADATA wsaData;

     SOCKET SendingSocket;

     // Server/receiver address

     SOCKADDR_IN          ServerAddr, ThisSenderInfo;

     // Server/receiver port to connect to

     unsigned int         Port = 7171;

     int  RetCode;

     // Be careful with the array bound, provide some checking mechanism...

     char sendbuf[1024] = "This is a test string from sender";

     int BytesSent, nlen;



     // Initialize Winsock version 2.2

     WSAStartup(MAKEWORD(2,2), &wsaData);

     printf("Client: Winsock DLL status is %s.\n", wsaData.szSystemStatus);



     // Create a new socket to make a client connection.

     // AF_INET = 2, The Internet Protocol version 4 (IPv4) address family, TCP protocol

     SendingSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

     if(SendingSocket == INVALID_SOCKET)

     {

          printf("Client: socket() failed! Error code: %ld\n", WSAGetLastError());

          // Do the clean up

          WSACleanup();

          // Exit with error

          return -1;

     }

     else

          printf("Client: socket() is OK!\n"); 



     // Set up a SOCKADDR_IN structure that will be used to connect

     // to a listening server on port 5150. For demonstration

     // purposes, let's assume our server's IP address is 127.0.0.1 or localhost



     // IPv4

     ServerAddr.sin_family = AF_INET;

     // Port no.

     ServerAddr.sin_port = htons(Port);

     // The IP address

     ServerAddr.sin_addr.s_addr = inet_addr("127.0.0.1");



     // Make a connection to the server with socket SendingSocket.

     RetCode = connect(SendingSocket, (SOCKADDR *) &ServerAddr, sizeof(ServerAddr));

     if(RetCode != 0)

     {

          printf("Client: connect() failed! Error code: %ld\n", WSAGetLastError());

          // Close the socket

          closesocket(SendingSocket);

          // Do the clean up

          WSACleanup();

          // Exit with error

          return -1;

     }

     else

     {

          printf("Client: connect() is OK, got connected...\n");

          printf("Client: Ready for sending and/or receiving data...\n");

     }



     // At this point you can start sending or receiving data on

     // the socket SendingSocket.



     // Some info on the receiver side...

     getsockname(SendingSocket, (SOCKADDR *)&ServerAddr, (int *)sizeof(ServerAddr));

     printf("Client: Receiver IP(s) used: %s\n", inet_ntoa(ServerAddr.sin_addr));

     printf("Client: Receiver port used: %d\n", htons(ServerAddr.sin_port));



     // Sends some data to server/receiver...

     BytesSent = send(SendingSocket, sendbuf, strlen(sendbuf), 0);



     if(BytesSent == SOCKET_ERROR)

          printf("Client: send() error %ld.\n", WSAGetLastError());

     else

     {

          printf("Client: send() is OK - bytes sent: %ld\n", BytesSent);

          // Some info on this sender side...

          // Allocate the required resources

          memset(&ThisSenderInfo, 0, sizeof(ThisSenderInfo));

          nlen = sizeof(ThisSenderInfo);



          getsockname(SendingSocket, (SOCKADDR *)&ThisSenderInfo, &nlen);

          printf("Client: Sender IP(s) used: %s\n", inet_ntoa(ThisSenderInfo.sin_addr));

          printf("Client: Sender port used: %d\n", htons(ThisSenderInfo.sin_port));

          printf("Client: Those bytes represent: \"%s\"\n", sendbuf);

     }



     if( shutdown(SendingSocket, SD_SEND) != 0)

          printf("Client: Well, there is something wrong with the shutdown().

                    The error code: %ld\n", WSAGetLastError());

     else

          printf("Client: shutdown() looks OK...\n");

     // When you are finished sending and receiving data on socket SendingSocket,

     // you should close the socket using the closesocket API. We will

     // describe socket closure later in the chapter.

     if(closesocket(SendingSocket) != 0)

          printf("Client: Cannot close \"SendingSocket\" socket. Error code: %ld\n", WSAGetLastError());

     else

          printf("Client: Closing \"SendingSocket\" socket...\n");



     // When your application is finished handling the connection, call WSACleanup.

     if(WSACleanup() != 0)

          printf("Client: WSACleanup() failed!...\n");

     else

          printf("Client: WSACleanup() is OK...\n");

     return 0;

}

Error log:

    1>------ Build started: Project: ws, Configuration: Debug Win32 ------
1>Compiling...
1>ws.cpp
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(1) : warning C4627: '#include <winsock2.h>': skipped when looking for precompiled header use
1>        Add directive to 'stdafx.h' or rebuild precompiled header
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(7) : error C2065: 'WSADATA' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(7) : error C2146: syntax error : missing ';' before identifier 'wsaData'
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(7) : error C2065: 'wsaData' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(9) : error C2065: 'SOCKET' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(9) : error C2146: syntax error : missing ';' before identifier 'SendingSocket'
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(9) : error C2065: 'SendingSocket' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(13) : error C2065: 'SOCKADDR_IN' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(13) : error C2146: syntax error : missing ';' before identifier 'ServerAddr'
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(13) : error C2065: 'ServerAddr' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(13) : error C2065: 'ThisSenderInfo' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(31) : error C2065: 'wsaData' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(31) : error C3861: 'WSAStartup': identifier not found
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(31) : error C3861: 'MAKEWORD': identifier not found
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(33) : error C2065: 'wsaData' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(33) : error C2228: left of '.szSystemStatus' must have class/struct/union
1>        type is ''unknown-type''
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(41) : error C2065: 'SendingSocket' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(41) : error C2065: 'AF_INET' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(41) : error C2065: 'SOCK_STREAM' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(41) : error C2065: 'IPPROTO_TCP' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(41) : error C3861: 'socket': identifier not found
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(43) : error C2065: 'SendingSocket' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(43) : error C2065: 'INVALID_SOCKET' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(47) : error C3861: 'WSAGetLastError': identifier not found
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(51) : error C3861: 'WSACleanup': identifier not found
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(75) : error C2065: 'ServerAddr' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(75) : error C2228: left of '.sin_family' must have class/struct/union
1>        type is ''unknown-type''
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(75) : error C2065: 'AF_INET' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(79) : error C2065: 'ServerAddr' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(79) : error C2228: left of '.sin_port' must have class/struct/union
1>        type is ''unknown-type''
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(79) : error C3861: 'htons': identifier not found
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(83) : error C2065: 'ServerAddr' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(83) : error C2228: left of '.sin_addr' must have class/struct/union
1>        type is ''unknown-type''
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(83) : error C2228: left of '.s_addr' must have class/struct/union
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(83) : error C3861: 'inet_addr': identifier not found
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(89) : error C2065: 'SendingSocket' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(89) : error C2065: 'SOCKADDR' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(89) : error C2059: syntax error : ')'
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(89) : error C3861: 'connect': identifier not found
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(95) : error C3861: 'WSAGetLastError': identifier not found
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(99) : error C2065: 'SendingSocket' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(99) : error C3861: 'closesocket': identifier not found
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(103) : error C3861: 'WSACleanup': identifier not found
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(131) : error C2065: 'SendingSocket' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(131) : error C2065: 'SOCKADDR' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(131) : error C2059: syntax error : ')'
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(133) : error C2065: 'ServerAddr' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(133) : error C2228: left of '.sin_addr' must have class/struct/union
1>        type is ''unknown-type''
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(131) : error C3861: 'getsockname': identifier not found
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(133) : error C3861: 'inet_ntoa': identifier not found
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(135) : error C2065: 'ServerAddr' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(135) : error C2228: left of '.sin_port' must have class/struct/union
1>        type is ''unknown-type''
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(135) : error C3861: 'htons': identifier not found
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(141) : error C2065: 'SendingSocket' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(141) : error C3861: 'send': identifier not found
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(141) : error C3861: 'strlen': identifier not found
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(145) : error C2065: 'SOCKET_ERROR' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(147) : error C3861: 'WSAGetLastError': identifier not found
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(159) : error C2065: 'ThisSenderInfo' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(159) : error C2065: 'ThisSenderInfo' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(159) : error C2070: ''unknown-type'': illegal sizeof operand
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(159) : error C3861: 'memset': identifier not found
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(161) : error C2065: 'ThisSenderInfo' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(161) : error C2070: ''unknown-type'': illegal sizeof operand
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(165) : error C2065: 'SendingSocket' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(165) : error C2065: 'SOCKADDR' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(165) : error C2059: syntax error : ')'
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(167) : error C2065: 'ThisSenderInfo' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(167) : error C2228: left of '.sin_addr' must have class/struct/union
1>        type is ''unknown-type''
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(165) : error C3861: 'getsockname': identifier not found
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(167) : error C3861: 'inet_ntoa': identifier not found
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(169) : error C2065: 'ThisSenderInfo' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(169) : error C2228: left of '.sin_port' must have class/struct/union
1>        type is ''unknown-type''
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(169) : error C3861: 'htons': identifier not found
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(177) : error C2065: 'SendingSocket' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(177) : error C2065: 'SD_SEND' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(177) : error C3861: 'shutdown': identifier not found
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(179) : error C2001: newline in constant
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(181) : error C2146: syntax error : missing ')' before identifier 'The'
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(181) : error C2017: illegal escape sequence
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(181) : error C2001: newline in constant
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(193) : error C2065: 'SendingSocket' : undeclared identifier
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(193) : error C3861: 'closesocket': identifier not found
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(195) : error C3861: 'WSAGetLastError': identifier not found
1>c:\users\wildfire\documents\visual studio 2008\projects\ws\ws\ws.cpp(205) : error C3861: 'WSACleanup': identifier not found
1>Build log was saved at "file://c:\Users\Wildfire\Documents\Visual Studio 2008\Projects\ws\ws\Debug\BuildLog.htm"
1>ws - 84 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
  • 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-18T10:50:48+00:00Added an answer on May 18, 2026 at 10:50 am

    Move #include "stdafx.h" to the top. It’s failing to find precompiled header file stdafx.h. It’s
    the first error.

    When you use precompiled headers, the header file you precompiling them through (in this case stdafx.h) should be always first line in the file (comments are ok).

    Also this

    #ifndef WIN32_LEAN_AND_MEAN
    #define WIN32_LEAN_AND_MEAN
    #endif
    

    should go before the winsock2.h inclusion. This tells compiler not to include too many Windows SDK files to speed up compilation. Putting this after #include <winsock2.h> has no effect.

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

Sidebar

Related Questions

In the new login dialog of the SQL Server 2005 Management Studio Express, what
When I try to install a new instance of SQL Server 2008 Express on
Hi all I've just started a new project using Visual Web Developer 2008 Express
New class is a subclass of the original object It needs to be php4
New to javascript/jquery and having a hard time with using this or $(this) to
New to xml. Looking for XPath to search a xml file with python ElementTree
New to silverlight. Traditionally if I were to design a wizard-like process where a
New to both Ruby and Rails but I'm book educated by now (which apparently
New to WCF, but familiar with COM+ - can I wrap a WCF service
New to Linux programming in general. I am trying to communicate with a kernel

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.