《Windows API巡礼》---sendto和recvfrom

sendto函数用于将数据发送到指定的地址:

int sendto(

  __in  SOCKET s, //指定套接字(可能已连接)

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

  __in  int len, //缓冲区大小

  __in  int flags, //指定数据传输方式

  __in  const struct sockaddr *to, //可选的指针,指向存储目标套接字地址信息的sockaddr结构

  __in  int tolen //to参数指向结构的字节大小

);

返回值:

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

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

sendto函数实例如下:

#include <windows.h>

#include <stdio.h>

#include "winsock2.h"

 

// Link with Ws3_32.lib

 

void main() {

 

  WSADATA wsaData;

  SOCKET SendSocket;

  sockaddr_in RecvAddr;

  int Port = 27015;

  char SendBuf[1024];

  int BufLen = 1024;

 

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

  // Initialize Winsock

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

 

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

  // Create a socket for sending data

  SendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

 

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

  // Set up the RecvAddr structure with the IP address of

  // the receiver (in this example case "192.168.1.1")

  // and the specified port number.

  RecvAddr.sin_family = AF_INET;

  RecvAddr.sin_port = htons(Port);

  RecvAddr.sin_addr.s_addr = inet_addr("192.168.1.1");

 

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

  // Send a datagram to the receiver

  printf("Sending a datagram to the receiver.../n");

  sendto(SendSocket,

    SendBuf,

    BufLen,

    0,

    (SOCKADDR *) &RecvAddr,

    sizeof(RecvAddr));

 

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

  // When the application is finished sending, close the socket.

  printf("Finished sending. Closing socket./n");

  closesocket(SendSocket);

 

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

  // Clean up and quit.

  printf("Exiting./n");

  WSACleanup();

  return;

}

 

recvfrom函数用于接收数据报并存储发送方源地址信息:

int recvfrom(

  __in         SOCKET s, //指定一个绑定的socket

  __out        char *buf, //存放接到的数据的缓冲区

  __in         int len, //缓冲区字节大小

  __in         int flags, //指定传输控制方式

  __out        struct sockaddr *from, //可选的指针,函数成功返回后,

//指向存储源套接字地址信息的sockaddr结构

  __inout_opt  int *fromlen //from的字节大小

);

返回值:

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

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

recvfrom函数实例如下:

#include <windows.h>

#include <stdio.h>

#include "winsock2.h"

 

void main() {

 

  WSADATA wsaData;

  SOCKET RecvSocket;

  sockaddr_in RecvAddr;

  int Port = 27015;

  char RecvBuf[1024];

  int  BufLen = 1024;

  sockaddr_in SenderAddr;

  int SenderAddrSize = sizeof(SenderAddr);

 

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

  // Initialize Winsock

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

 

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

  // Create a receiver socket to receive datagrams

  RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

 

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

  // Bind the socket to any address and the specified port.

  RecvAddr.sin_family = AF_INET;

  RecvAddr.sin_port = htons(Port);

  RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);

 

  bind(RecvSocket, (SOCKADDR *) &RecvAddr, sizeof(RecvAddr));

 

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

  // Call the recvfrom function to receive datagrams

  // on the bound socket.

  printf("Receiving datagrams.../n");

  recvfrom(RecvSocket,

    RecvBuf,

    BufLen,

    0,

    (SOCKADDR *)&SenderAddr,

    &SenderAddrSize);

 

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

  // Close the socket when finished receiving datagrams

  printf("Finished receiving. Closing socket./n");

  closesocket(RecvSocket);

 

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

  // Clean up and exit.

  printf("Exiting./n");

  WSACleanup();

  return;

}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值