C++实现简单UDP通信
生活随笔
收集整理的這篇文章主要介紹了
C++实现简单UDP通信
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
發(fā)送端向接收端發(fā)送數(shù)據(jù),ip地址和端口號(hào)要與接收端一致
?
?cpp_UDP客戶(hù)端,發(fā)送端
#include <winsock2.h> #include <WS2tcpip.h> #include <iostream> using namespace std;//添加動(dòng)態(tài)庫(kù)的lib #pragma comment(lib, "ws2_32.lib")int main() {SOCKET m_Socket;SOCKADDR_IN m_RemoteAddress; //遠(yuǎn)程地址int m_RemoteAddressLen;// socket環(huán)境WSADATA wsaData;if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {cout << "WSAStartup error:" << GetLastError() << endl;return false;}// socket對(duì)象m_Socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);if (m_Socket == INVALID_SOCKET){closesocket(m_Socket);m_Socket = INVALID_SOCKET;return false;}// 遠(yuǎn)端地址const char* ip = "172.22.64.148";int port = 9800;m_RemoteAddress.sin_family = AF_INET;m_RemoteAddress.sin_port = htons(port);m_RemoteAddressLen = sizeof(m_RemoteAddress);inet_pton(AF_INET, ip, &m_RemoteAddress.sin_addr);// 接收和發(fā)送char recvBuf[1024] = { 0 };char sendBuf[1024] = "Nice to meet you!我的";while (1) {int sendLen = sendto(m_Socket, sendBuf, strlen(sendBuf), 0, (sockaddr*)&m_RemoteAddress, m_RemoteAddressLen);if (sendLen > 0) {std::printf("發(fā)送到遠(yuǎn)程端連接, 其ip: %s, port: %d\n", inet_ntoa(m_RemoteAddress.sin_addr), ntohs(m_RemoteAddress.sin_port));cout << "發(fā)送到遠(yuǎn)程端的信息: " << sendBuf << endl;}int recvLen = recvfrom(m_Socket, recvBuf, 1024, 0, NULL, NULL);if (recvLen > 0) {std::printf("接收到一個(gè)連接, 其ip: %s, port: %d\n", inet_ntoa(m_RemoteAddress.sin_addr), ntohs(m_RemoteAddress.sin_port));cout << "接收到一個(gè)信息: " << recvBuf << endl;}}closesocket(m_Socket);WSACleanup();return true; }cpp_UDP服務(wù)端,接收端
#include <winsock2.h> #include <WS2tcpip.h> #include <iostream>using namespace std;//添加動(dòng)態(tài)庫(kù)的lib #pragma comment(lib, "ws2_32.lib")int main() {SOCKET m_Socket;SOCKADDR_IN m_BindAddress; //綁定地址SOCKADDR_IN m_RemoteAddress; //遠(yuǎn)程地址int m_RemoteAddressLen;// socket環(huán)境WSADATA wsaData;if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {cout << "WSAStartup error:" << GetLastError() << endl;return false;}// socket對(duì)象m_Socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);if (m_Socket == INVALID_SOCKET){closesocket(m_Socket);m_Socket = INVALID_SOCKET;return false;}// 綁定占用<ip, port>const char* ip = "172.22.64.42";int port = 9800;m_BindAddress.sin_family = AF_INET;m_BindAddress.sin_addr.S_un.S_addr = inet_addr(ip);m_BindAddress.sin_port = htons(port);auto ret = bind(m_Socket, (sockaddr*)&m_BindAddress, sizeof(SOCKADDR));if (ret == SOCKET_ERROR){closesocket(m_Socket);m_Socket = INVALID_SOCKET;return false;}// 接收和發(fā)送char recvBuf[1024] = { 0 };char sendBuf[1024] = "Nice to meet you, too!";m_RemoteAddressLen = sizeof(m_RemoteAddress);std::printf("已設(shè)置綁定占用的連接, 其ip: %s, port: %d\n", inet_ntoa(m_BindAddress.sin_addr), ntohs(m_BindAddress.sin_port));while (1) {int recvLen = recvfrom(m_Socket, recvBuf, 1024, 0, (sockaddr*)&m_RemoteAddress, &m_RemoteAddressLen);if (recvLen > 0) {std::printf("接收到一個(gè)連接, 其ip: %s, port: %d\n", inet_ntoa(m_RemoteAddress.sin_addr), ntohs(m_RemoteAddress.sin_port));cout << "接收到一個(gè)信息: " << recvBuf << endl;}int sendLen = sendto(m_Socket, sendBuf, strlen(sendBuf), 0, (sockaddr*)&m_RemoteAddress, m_RemoteAddressLen);if (sendLen > 0) {cout << "發(fā)送到遠(yuǎn)程端的信息: " << sendBuf << endl;}}closesocket(m_Socket);WSACleanup();return true; }總結(jié)
以上是生活随笔為你收集整理的C++实现简单UDP通信的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: c++ 控制台游戏打砖块
- 下一篇: win10使用自带的Hyper-V安装w