Windows网络编程(四):建立UDP连接和收发消息

本文详细介绍了UDP消息的发送和接收过程,包括创建Socket、绑定地址、使用sendto和recvfrom函数进行数据交换的具体实现。通过示例代码展示了如何在Windows环境下使用Winsock库完成UDP通信。
摘要由CSDN通过智能技术生成

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;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值