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