VS2012之MFC带Windows socket下的阻塞式TCP范例

    从《TCP/IP Sockets编程》官网上下载的例程并修改到MFC程序,选择带Windows socket编程选项后,不用添加任何头文件即可直接运行,

注意需要用户自定义一个MFC选择带Windows socket编程选项的项目后,添加调用如下函数,已测试可运行

代码如下 

//自定义函数如下
void DieWithError(char *errorMessage)
{
    fprintf(stderr,"%s: %d\n", errorMessage, WSAGetLastError());
    exit(1);
}
#define TCPS0_RxBufSizeMacro 100 //为S0的接收缓存数组长度
void uWinsock(void)
{
int sock;                        /* Socket descriptor */
    struct sockaddr_in echoServAddr; /* Echo server address */
    unsigned short echoServPort=5000;     /* Echo server port */
char *servIP="211.168.3.158";                    /* Server IP address (dotted quad) */
char TCPS0_TxBufChar[100]={0xEB,0x90,0xEB,0x90,0xEB,0xC1,0x00,0x00,0x01,0x00,0xD4,0xB6,'\0'};                /* String to send to echo server */
    char TCPS0_RxBufChar[TCPS0_RxBufSizeMacro];     /* Buffer for echo string */
    int TCPS0_TxBufCharLen;               /* Length of string to echo */
    int bytesRcvd, totalBytesRcvd;   /* Bytes read in single recv() and total bytes read */
    WSADATA wsaData;                 /* Structure for WinSock setup communication */


    //servIP = argv[1];             /* First arg: server IP address (dotted quad) */
    //TCPS0_TxBufChar = argv[2];         /* Second arg: string to echo */


   
    if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) //协议库的版本信息//Init Windows Socket /* Load Winsock 2.0 DLL */
    {
        fprintf(stderr, "WSAStartup() failed");
        exit(1);
    }


    /* Create a reliable, stream socket using TCP */
    if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
        DieWithError("socket() failed");


    /* Construct the server address structure */
    memset(&echoServAddr, 0, sizeof(echoServAddr));     /* Zero out structure */
    echoServAddr.sin_family      = AF_INET;             /* Internet address family */
    echoServAddr.sin_addr.s_addr = inet_addr(servIP);//inet_addr将点分十进制字符串转换成32位的IPv4地址   /* Server IP address */
    //echoServAddr.sin_port        =echoServPort;//服务器端端口号
echoServAddr.sin_port        = htons(echoServPort); /* Server port *///htons函数用来将输入的16位之高低8位交换后输出。
    /* Establish the connection to the echo server */
    if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
{ closesocket(sock);
 //DieWithError("connect() failed");
}



    //TCPS0_TxBufCharLen = strlen(TCPS0_TxBufChar);          /* Determine input length */
TCPS0_TxBufCharLen =12;


    /* Send the string, including the null terminator, to the server */
    if (send(sock, TCPS0_TxBufChar, TCPS0_TxBufCharLen, 0) != TCPS0_TxBufCharLen)
        DieWithError("send() sent a different number of bytes than expected");


    /* Receive the same string back from the server */
    totalBytesRcvd = 0;
    printf("Received: ");                /* Setup to print the echoed string */
    while (totalBytesRcvd < TCPS0_TxBufCharLen)
    {
        /* Receive up to the buffer size (minus 1 to leave space for 
           a null terminator) bytes from the sender */
        if ((bytesRcvd = recv(sock, TCPS0_RxBufChar, TCPS0_RxBufSizeMacro - 1, 0)) <= 0)
            DieWithError("recv() failed or connection closed prematurely");
        totalBytesRcvd += bytesRcvd;   /* Keep tally of total bytes */
        TCPS0_RxBufChar[bytesRcvd] = '\0';  /* Add \0 so printf knows where to stop */
        printf("%s", TCPS0_RxBufChar);            /* Print the echo buffer */
    }


    printf("\n");    /* Print a final linefeed */


    closesocket(sock);
    WSACleanup();  /* Cleanup Winsock */


}