《Windows API巡礼》---send和recv

send函数用于在一个已经连接成功的socket上发送数据:

int send(

  __in  SOCKET s, //已连接的套接字

  __in  const char *buf,//指向将要发送数据的缓冲区

  __in  int len, //缓冲区数据的字节长度

  __in  int flags //指定数据传输方式:0---无特殊行为;MSG_DONTROUTE---传输层不要将它发出的包

                   //路由出去;MSG_OOB---数据应该被带外发送

);

其中,所谓带外数据,也可称为TCP紧急数据,它是在两个相连的每一对套接字间逻辑上独立的传输通道。带外数据是独立于普通数据传送给用户的。

返回值:

成功时,返回发送的字节数;

失败时,返回SOCKET_ERROR,调用WSAGetLastError函数查看进一步错误信息。

 

send函数实例如下:

#include <windows.h>

#include <winsock2.h>

#include <stdio.h>

 

#define DEFAULT_BUFLEN 512

#define DEFAULT_PORT 27015

 

int main() {

 

    //----------------------

    // Declare and initialize variables.

    int iResult;

    WSADATA wsaData;

 

    SOCKET ConnectSocket;

    struct sockaddr_in clientService;

 

    int recvbuflen = DEFAULT_BUFLEN;

    char *sendbuf = "Client: sending data test";

    char recvbuf[DEFAULT_BUFLEN] = "";

 

    //----------------------

    // Initialize Winsock

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

    if (iResult != NO_ERROR) {

        printf("WSAStartup failed with error: %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("socket failed with error: %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( DEFAULT_PORT );

 

    //----------------------

    // Connect to server.

    iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );

    if (iResult == SOCKET_ERROR) {

        printf( "connect failed with error: %d/n", WSAGetLastError() );

        closesocket(ConnectSocket);

        WSACleanup();

        return 1;

  }

 

    //----------------------

    // Send an initial buffer

    iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );

    if (iResult == SOCKET_ERROR) {

        printf("send() failed with error: %d/n", WSAGetLastError());

        closesocket(ConnectSocket);

        WSACleanup();

        return 1;

    }

 

    printf("Bytes Sent: %d/n", iResult);

 

    // shutdown the connection since no more data will be sent

    iResult = shutdown(ConnectSocket, SD_SEND);

    if (iResult == SOCKET_ERROR) {

        printf("shutdown failed with error: %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 with error: %d/n", WSAGetLastError());

 

    } while( iResult > 0 );

 

 

    // cleanup

    closesocket(ConnectSocket);

    WSACleanup();

 

    return 0;

}

 

 

revc函数用于从一个已经建立连接的socket出接收数据:

int recv(

  __in   SOCKET s, //准备接收数据的套接字

  __out  char *buf, //指向即将接收数据的字符缓冲区的指针

  __in   int len, //缓冲区的大小

  __in   int flags //指定传输控制方式:0---无特殊行为;MSG_PKKE---表示数据从接收端口复制到接收

                   //缓冲区中,并且没有从系统缓冲区中将数据删除;MSG_OOB---表示数据是带外发送的

);

返回值:

成功时,返回接收的字节数;

失败时,返回SOCKET_ERROR,调用WSAGetLastError函数查看进一步错误信息。

 

recv函数实例代码如下:

#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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值