UDP消息的发送和接收需要UDP连接,所以,上面的TCP连接已经不适用了,具体的区别主要有:
创建Socket时参数不同创建服务端时不需要listen和accept操作创建客户端时不需要connect操作服务器需要bind操作,客户端不需要。
发送和接收UDP消息要用到sendto和recvfrom两个函数。
先来看看接收消息,当执行recvfrom时,线程会进入等待,直到接收到UDP消息为止。
// ConsoleApplication3.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#define _WINSOCK_DEPRECATED_NO_WARNINGS //这个声明要在stdafx.h的后面,但要在其他头文件的前面
#include <winsock2.h>
#include <stdio.h>
#include <windows.h>
// 需要声明Winsock库
#pragma comment(lib, "Ws2_32.lib")
int _tmain(int argc, _TCHAR* argv[])
{
int iResult = 0;
WSADATA wsaData;
SOCKET RecvSocket;
sockaddr_in RecvAddr;
unsigned short Port = 27015;
char RecvBuf[1024];
int BufLen = 1024;
sockaddr_in SenderAddr;
int SenderAddrSize = sizeof(SenderAddr);
iResult = WSAStartup(MAKEWORD(2, 2), wsaData);
if (iResult != NO_ERROR) {
wprintf(L"WSAStartup failed with error %d\n", iResult);
return 1;
}
RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (RecvSocket == INVALID_SOCKET) {
wprintf(L"socket failed with error %d\n", WSAGetLastError());
return 1;
}
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(Port);
RecvAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
iResult = bind(RecvSocket, (SOCKADDR *)RecvAddr, sizeof(RecvAddr));
if (iResult != 0) {
wprintf(L"bind failed with error %d\n", WSAGetLastError());
return 1;
}
wprintf(L"Receiving datagrams...\n");
iResult = recvfrom(RecvSocket,RecvBuf, BufLen, 0, (SOCKADDR *)&SenderAddr, &SenderAddrSize);
if (iResult == SOCKET_ERROR) {
wprintf(L"recvfrom failed with error %d\n", WSAGetLastError());
}
wprintf(L"Finished receiving. Closing socket.\n");
iResult = closesocket(RecvSocket);
if (iResult == SOCKET_ERROR) {
wprintf(L"closesocket failed with error %d\n", WSAGetLastError());
return 1;
}
wprintf(L"Exiting.\n");
WSACleanup();
return 0;
}
再来看看发送UDP消息
#define _WINSOCK_DEPRECATED_NO_WARNINGS //使用被弃用函数不警告,位置必须在stdafx.h的后面,在其他头文件的前面
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
// 需要声明Winsock库
#pragma comment(lib, "ws2_32.lib")
int wmain()
{
int iResult;
WSADATA wsaData;
SOCKET SendSocket = INVALID_SOCKET;
sockaddr_in RecvAddr;
unsigned short Port = 27015;
char SendBuf[1024];
int BufLen = 1024;
iResult = WSAStartup(MAKEWORD(2, 2),&wsaData);
if (iResult != NO_ERROR) {
wprintf(L"WSAStartup failed with error: %d\n", iResult);
return 1;
}
SendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (SendSocket == INVALID_SOCKET) {
wprintf(L"socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(Port);
RecvAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
wprintf(L"Sending a datagram to the receiver...\n");
iResult = sendto(SendSocket, SendBuf, BufLen, 0, (SOCKADDR *)&RecvAddr, sizeof(RecvAddr));
if (iResult == SOCKET_ERROR) {
wprintf(L"sendto failed with error: %d\n", WSAGetLastError());
closesocket(SendSocket);
WSACleanup();
return 1;
}
wprintf(L"Finished sending. Closing socket.\n");
iResult = closesocket(SendSocket);
if (iResult == SOCKET_ERROR) {
wprintf(L"closesocket failed with error: %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
wprintf(L"Exiting.\n");
WSACleanup();
return 0;
}