#include <windows.h>
#include <winsock2.h>
#include <stdio.h>#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"int __cdecl main() {    //----------------------
    // Declare and initialize variables.
    WSADATA wsaData;
    int iResult;    SOCKET ConnectSocket;
    struct sockaddr_in clientService;     char *sendbuf = "this is a test";
    char recvbuf[DEFAULT_BUFLEN];
    int recvbuflen = DEFAULT_BUFLEN;
  
    //----------------------
    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != NO_ERROR) {
      printf("WSAStartup failed: %d\n", iResult);
      return 1;
    }    //----------------------
    // Create a SOCKET for connecting to server
    ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (ConnectSocket == INVALID_SOCKET) {
        printf("Error at socket(): %ld\n", WSAGetLastError() );
        WSACleanup();
        return 1;
    }    //----------------------
    // The sockaddr_in structure specifies the address family,
    // IP address, and port of the server to be connected to.
    clientService.sin_family = AF_INET;
    clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
    clientService.sin_port = htons( 27015 );    //----------------------
    // Connect to server.
    iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );
    if ( iResult == SOCKET_ERROR) {
        closesocket (ConnectSocket);
        printf("Unable to connect to server: %ld\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }    // Send an initial buffer
    iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
    if (iResult == SOCKET_ERROR) {
        printf("send failed: %d\n", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }    printf("Bytes Sent: %ld\n", iResult);    // shutdown the connection since no more data will be sent
    iResult = shutdown(ConnectSocket, SD_SEND);
    if (iResult == SOCKET_ERROR) {
        printf("shutdown failed: %d\n", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }    // Receive until the peer closes the connection
    do {        iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
        if ( iResult > 0 )
            printf("Bytes received: %d\n", iResult);
        else if ( iResult == 0 )
            printf("Connection closed\n");
        else
            printf("recv failed: %d\n", WSAGetLastError());    } while( iResult > 0 );    // cleanup
    closesocket(ConnectSocket);
    WSACleanup();    return 0;
}
很多msdn列子用,win32 console application,直接运行,运行不了,添加了#include "stdafx.h",又报其他错误是为甚么呢?
请问如何运行上面代码

解决方案 »

  1.   

    把 
    #include "stdafx.h"加到最上面
    Error 4 error C2011: 'sockaddr' : 'struct' type redefinition c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h 206 fsda报错sockaddr重定义了
      

  2.   

    #pragma comment(lib, "WS2_32.lib")
      

  3.   

    #include <windows.h>
    #include <winsock2.h>
    #include <stdio.h>
    #pragma comment(lib, "WS2_32.lib")加一句这个
      

  4.   

    我经常出现这种情况
    多弄几次就知道了,看最下需要哪个DLL,再仔细看下csdn上面怎么使用。
      

  5.   


    #include "stdafx.h"#include <windows.h>
    #include <winsock2.h>
    #include <stdio.h>
    #pragma comment(lib, "WS2_32.lib")#define DEFAULT_BUFLEN 512
    #define DEFAULT_PORT "27015"int __cdecl main() {

    //----------------------
    // Declare and initialize variables.
    WSADATA wsaData;
    int iResult;

    SOCKET ConnectSocket;
    struct sockaddr_in clientService;  

    char *sendbuf = "this is a test";
    char recvbuf[DEFAULT_BUFLEN];
    int recvbuflen = DEFAULT_BUFLEN;

    //----------------------
    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != NO_ERROR) {
    printf("WSAStartup failed: %d\n", iResult);
    return 1;
    }

    //----------------------
    // Create a SOCKET for connecting to server
    ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (ConnectSocket == INVALID_SOCKET) {
    printf("Error at socket(): %ld\n", WSAGetLastError() );
    WSACleanup();
    return 1;
    }

    //----------------------
    // The sockaddr_in structure specifies the address family,
    // IP address, and port of the server to be connected to.
    clientService.sin_family = AF_INET;
    clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
    clientService.sin_port = htons( 27015 );

    //----------------------
    // Connect to server.
    iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );
    if ( iResult == SOCKET_ERROR) {
    closesocket (ConnectSocket);
    printf("Unable to connect to server: %ld\n", WSAGetLastError());
    WSACleanup();
    return 1;
    }

    // Send an initial buffer
    iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
    if (iResult == SOCKET_ERROR) {
    printf("send failed: %d\n", WSAGetLastError());
    closesocket(ConnectSocket);
    WSACleanup();
    return 1;
    }

    printf("Bytes Sent: %ld\n", iResult);

    // shutdown the connection since no more data will be sent
    iResult = shutdown(ConnectSocket, SD_SEND);
    if (iResult == SOCKET_ERROR) {
    printf("shutdown failed: %d\n", WSAGetLastError());
    closesocket(ConnectSocket);
    WSACleanup();
    return 1;
    }

    // Receive until the peer closes the connection
    do {

    iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
    if ( iResult > 0 )
    printf("Bytes received: %d\n", iResult);
    else if ( iResult == 0 )
    printf("Connection closed\n");
    else
    printf("recv failed: %d\n", WSAGetLastError());

    } while( iResult > 0 );

    // cleanup
    closesocket(ConnectSocket);
    WSACleanup();

    return 0;
    }
      

  6.   

    新建 控制台程序选择非MFC支持,空的工程确定然后新建一个文件,把代码复制进去然后保存为main.cpp这样然后右键点鼠标弹出的菜单中选择 加入到project中编译、执行。
      

  7.   

    Error 4 error C2011: 'sockaddr' : 'struct' type redefinition c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h 206 fsda
      

  8.   

    #include <windows.h>
    #include <winsock2.h>->>
    #include <winsock2.h>
    #include <windows.h>注意包含的顺序
      

  9.   

    谢谢兔子哥,对了,
    msdn就是那个顺序的吗,请问下是什么原因呢?
      

  10.   

    是因为<windows.h>中包含了<winsock.h>头文件,而你下面有包含了Winsock2.h,从而导致由于其版
    本的不同,导致出现错误。
      

  11.   

    This problem arises because windows.h (at least, that version of it) includes not winsock2.h but winsock.h ; sadly when Microsoft wrote winsock2.h they chose neither to change windows.h to include winsock2.h , which replaces winsock.h , nor to include windows.h from winsock2.h and then add the definitions for the new Winsock 2 API methods & structures (this might seem reasonable since Winsock 2 does, strictly speaking, replace Winsock 1, but since the API must be fully backwards-compatible the distinction is somewhat meaningless and there's no real benefit to making winsock2.h standalone).The fix is thankfully simple: always "#include <winsock2.h> " before windows.h .However, you must remember that if windows.h has been included by (for example) a higher-level header file that is subsequently including your header file, it's too late - so you must make sure that the higher-level header files respect this convention also.It is however rarely necessary to modify the header files of libraries or other code modules you are using just because you include their header files, and their header files include windows.h - you can just include winsock2.h before you include the library's header files.