这是我第一次在 windows 中使用 UDP 套接字,所以请多多包涵! 我正在尝试在 Windows 中的 C++ 控制台应用程序和 C# 中的 UWP(通用 Windows 应用程序)之间设置 UDP 套接字。当我从控制台向 UWP 发送数据时,一切正常。另一方面,当我尝试将数据从 C# UWP 发送到控制台时,我没有收到任何信息。 现在我用 Wireshark 进行了一些监控,似乎控制台 IP 可以很好地发送和接收数据包。有人知道我在这里错过了什么吗?
请找到随附的 C# UWP 客户端代码:
static void Main()
{
Console.WriteLine("App Started...");
while (true)
{
SendData("test");
}
Console.ReadKey();
}
static IPAddress addr = IPAddress.Parse("192.168.1.40");
static IPEndPoint ep = new IPEndPoint(addr, 27000);
static UdpClient udpClient = new UdpClient(27000);
static void SendData(string text)
{
while (true)
{
byte[] data = Encoding.ASCII.GetBytes(text);
int sendBytes = udpClient.Send(data, data.Length, ep);
Console.WriteLine("Data sending..." + text);
}
}
这是我的 C++ 控制台应用程序代码:
#include <iostream>
// UDP
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <thread>
#include <chrono>
#include <string>
#pragma comment (lib, "ws2_32.lib") // Authomatically include the lib
int main()
{
// Local Variable definitions
std::cout << "\t\ --------------- UDP Server --------------- " << std::endl;
std::cout << std::endl;
WSADATA WinSockData;
int iWsaStartup;
int iWsaCleanup;
SOCKET UDPSocketServer;
struct sockaddr_in UDPClient;
char Buffer[512];
int iBufferLen = strlen(Buffer) + 1;
int iBind;
int iReceiveForm;
int iUDPClientLen = sizeof(UDPClient);
int iCloseSocket;
// STEP-1 Startup Winsock
iWsaStartup = WSAStartup(MAKEWORD(2, 2), &WinSockData);
if (iWsaStartup != 0)
{
std::cout << "Can't start Winsock: " << iWsaStartup << std::endl;
}
std::cout << "WSAStartUp Success" << std::endl;
// STEP-2 Fill the UDPClient(SOCKET ADDRESS) Structure
UDPClient.sin_family = AF_INET;
UDPClient.sin_addr.s_addr = inet_addr("192.168.1.40");
UDPClient.sin_port = htons(27000);
// STEP-3 Socket creation
UDPSocketServer = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (UDPSocketServer == INVALID_SOCKET)
{
std::cout << "Socket Creation Failed, ERROR No: " << WSAGetLastError() << std::endl;
}
std::cout << "Socket Success" << std::endl;
// STEP-4 Bind the server
iBind = bind(UDPSocketServer,
(SOCKADDR*)&UDPClient,
sizeof(UDPClient));
if (iBind == SOCKET_ERROR)
{
std::cout << "Binding Failed, ERROR No: " << WSAGetLastError() << std::endl;
}
std::cout << "Binding Success" << std::endl;
// STEP-5 RecvFrom Fun from receive data from client
while(true)
{
iReceiveForm = recvfrom(UDPSocketServer,
Buffer,
iBufferLen,
MSG_PEEK,
(SOCKADDR*)&UDPClient,
&iUDPClientLen);
if (iReceiveForm == SOCKET_ERROR)
{
std::cout << "Receiving failed, ERROR No: " << WSAGetLastError() << std::endl;
}
std::cout << "Receiving Success" << std::endl;
std::cout << "Received data --> " << Buffer << std::endl;
}
// STEP-6 CloseSocket Function
iCloseSocket = closesocket(UDPSocketServer);
if (iCloseSocket == SOCKET_ERROR)
{
std::cout << "Socket Closing Failed, ERROR No: " << WSAGetLastError() << std::endl;
}
std::cout << "WSASocket closing success" << std::endl;
// STEP-7 WSACleanUp Fun for terminating from DLL
iWsaCleanup = WSACleanup();
if (iWsaCleanup == SOCKET_ERROR)
{
std::cout << "WSA CleanUp Failed, ERROR No: " << WSAGetLastError() << std::endl;
}
std::cout << "WSA CleanUp Success" << std::endl;
system("PAUSE");
return 0;