Windows網路程式設計(四):建立UDP連線和收發訊息
阿新 • • 發佈:2018-11-27
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;
}