windows tcp ip socket编程?

时间:2015-04-17 19:16:31

标签: c++ c windows sockets tcp

我想在过去两天找到一个很好的例子,但我总能找到unix的例子..

这是我得到的示例代码

#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
#include <stdlib.h> /* for atoi() */
#include <string.h> /* for memset() */
#include <unistd.h> /* for close() */

#define RCVBUFSIZE 32 /* Size of receive buffer */

void DieWithError(char *errorMessage); /* Error handling function */

int main(int argc, char *argv[]) {

    int sock; /* Socket descriptor */
    struct sockaddr_in echoServAddr; /* Echo server address */
    unsigned short echoServPort; /* Echo server port */
    char *servIP; /* Server IP address (dotted quad) */
    char *echoString; /* String to send to echo server */
    char echoBuffer[RCVBUFSIZE]; /* Buffer for echo string */
    unsigned int echoStringLen; /* Bytes read in single recv()*/
    int bytesRcvd, totalBytesRcvd; /*total bytes read */

    if ((argc<3) || (argc>4)) {
        fprintf(stderr, "Usage: %s <Server IP> <Echo Word> [<Echo Port>]\n", argv[0]);
        exit(l);
    }
    servIP = argv[1] ; /* First arg' server IP address (dotted quad) */
    echoString = argv[2] ;/* Second arg' string to echo */


    if (argc == 4)
        echoServPort = atoi(argv[3]); /* Use given port, if any */
    else
        echoServPort = 7; /* 7 is the well-known port for the echo service */

    /* 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); /* Server IP address */
    echoServAddr.sin_port = htons(echoServPort); /* Server port */

    /* Establish the connection to the echo server */
    if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
        DieWithError(" connect () failed") ;

    echoStringLen = strlen(echoString) ; /* Determine input length */

    /* Send the string to the server */
    if (send(sock, echoString, echoStringLen, 0) != echoStringLen)
        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 < echoStringLen) {
    /* Receive up to the buffer size (minus 1 to leave space for
        a null terminator) bytes from the sender */
        if ((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE - 1, 0)) <= 0)
            DieWithError("recv() failed or connection closed prematurely");
        totalBytesRcvd += bytesRcvd; /* Keep tally of total bytes */
        echoBuffer[bytesRcvd] = '\0'; /* Terminate the string! */
        printf(echoBuffer); /* Print the echo buffer */
    }
    printf("\n"); /* Print a final linefeed */

    close(sock);
    exit(0);
}

void DieWithError(char *errorMessage){

    perror(errorMessage);
    exit(1);
}

Windows是否有类似的例子? 我真的很感激有关的信息!!

1 个答案:

答案 0 :(得分:0)

我上次使用Windows时很多年前使用的是Windows XP / NT。我记得管理TCP / IP套接字的功能与Linux / UNIX相同,但Windows XP需要下面的初始化代码:

WSADATA wsad;
int wsok;

WSACleanup(); Sleep(2);
wsok=WSAStartup(MAKEWORD(2,1),&wsad);
if ( wsok ) {
    perror("Error");
    return errno;
}

然后,在代码结束时,关闭时:

if ( !wsok ) 
    WSACleanup();

请记住包括:

#include <winsock2.h>