分享vc++的udp客户端程序代码,编译器使用vs2015社区版本。
下面是udp client 主程序,udp_control.cpp
#include "udp_control.h"
#include
#include
#pragma comment(lib, "ws2_32.lib") //add ws2_32.lib
int udp_client_init(UDPClient_t *client, const char *remoteIP, WORD remotePort, WORD LocalPort)
{
WORD wVersionRequested;
int err;
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &client->wsaData);
if (err != 0) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return -1;
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if (LOBYTE(client->wsaData.wVersion) != 2 ||
HIBYTE(client->wsaData.wVersion) != 2) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup();
return -1;
}
client->sockClient = socket(AF_INET, SOCK_DGRAM, 0);//IPPROTO_UDP
//for send
client->SendAddr.sin_family = AF_INET;
client->SendAddr.sin_port = htons(remotePort);
#if 1
WCHAR wszClassName[256];
memset(wszClassName, 0, sizeof(wszClassName));
MultiByteToWideChar(CP_ACP, 0, remoteIP, strlen(remoteIP) + 1, wszClassName,
sizeof(wszClassName) / sizeof(wszClassName[0]));
err = InetPton(AF_INET, wszClassName, &client->SendAddr.sin_addr);
if (err < 0 || client->sockClient < 0)
{
std::cout << "socket error" << std::endl;
WSACleanup();
return -1;
}
#else
client->SendAddr.sin_addr.S_un.S_addr = inet_addr(remoteIP);
#endif
//for bind
client->RecvAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
client->RecvAddr.sin_family = AF_INET;
client->RecvAddr.sin_port = htons(LocalPort);
client->LocalPort = LocalPort;
client->RemotePort = remotePort;
bind(client->sockClient, (SOCKADDR*)&client->RecvAddr, sizeof(SOCKADDR));
return 0;
}
int udp_client_deinit(UDPClient_t *client)
{
closesocket(client->sockClient);
WSACleanup();
return 0;
}
int udp_client_send(UDPClient_t *client, const char *buf, WORD bufSize)
{
int ret;
ret = sendto(client->sockClient, buf, bufSize, 0, (SOCKADDR*)&client->SendAddr, sizeof(SOCKADDR));
if (SOCKET_ERROR == ret)
{
std::cout << "Remote Port:" << client->RemotePort << " send udp error!" << std::endl;
}
return ret;
}
int udp_client_receive(UDPClient_t *client, char *buf, WORD bufSize)
{
int ret, fromAddrSize;
struct sockaddr_in fromIP;
fromAddrSize = sizeof(SOCKADDR_IN);
ret = recvfrom(client->sockClient, buf, bufSize, 0, (struct sockaddr *)&fromIP, &fromAddrSize);
if (SOCKET_ERROR == ret)
{
std::cout <<"Local Port:"<< client->LocalPort <<" receive udp error!" << std::endl;
}
return ret;
}
下面是头文件udp_control.h
#pragma once
#ifndef __UDP_CONTROL_H__
#define __UDP_CONTROL_H__
#include
#include
#include
#include
#include
#include
typedef struct UDPClient_
{
SOCKET sockClient;
WSADATA wsaData;
SOCKADDR_IN RecvAddr;
SOCKADDR_IN SendAddr;
DWORD LocalPort;
DWORD RemotePort;
}UDPClient_t;
int udp_client_init(UDPClient_t *client, const char *remoteIP, WORD remotePort, WORD LocalPort);
int udp_client_deinit(UDPClient_t *client);
int udp_client_send(UDPClient_t *client, const char *buf, WORD bufSize);
int udp_client_receive(UDPClient_t *client, char *buf, WORD bufSize)
;
#endif // !__UDP_CONTROL_H__