问题描述
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
/*#include <windows.h>
*/
#include<winsock2.h>
#include <windows.h> /* Note: winsock2.h has included windows.h */
#include "udpserverclient.h"
int main(void)
{
WSADATA wsa_data; /* type defined in winsock2.h */
SOCKET Udplisten,UDPClient; /* type defined in winsock2.h */
struct sockaddr_in local,client; /* struct defined in winsock2.h */
int ipAddrSize,recvstatus,iterationStep;
char recvBuffer[128];
double time_old,time_new,time_interval;
printf("******** UDP Server ********\n\n");
/* Step 1: startup winsocket - this is for Windows only */
/* in pair with WSACleanup() */
if(WSAStartup(MAKEWORD(2,2),&wsa_data) != 0)
{
puts("WSAStartup Failed!");
exit(1);
}
/* Step 2: Create socket and check it is successful */
/* in pair with closesocket() */
Udplisten = socket(AF_INET,SOCK_DGRAM,0);
if(Udplisten == SOCKET_ERROR)
{
printf("Failed to create scoket(): %d\n",WSAGetLastError());
exit(1);
}
/* Step 3.1: Setup Parameters for local UDP Server*/
local.sin_family = AF_INET;
local.sin_addr.s_addr = inet_addr(MY_SERVER_IP_ADDRESS); /* defined in udpserverclient.h */
local.sin_port = htons(MY_SERVER_PORT_NUMBER); /* defined in udpserverclient.h */
/* Step 3.2: Bind to the local UDP Server */
if(bind(Udplisten,(struct sockaddr *)&local,sizeof(local)) == SOCKET_ERROR)
{
printf("Failed to bind(): %d\n",WSAGetLastError());
exit(1);
}
/* Step 4: Listen */
listen(Udplisten,8);
/* Step 5: Accept in Loops*/
for (;;)
{
/* Step 5.1: Accept */
ipAddrSize = sizeof(client);
UDPClient = accept(Udplisten,(struct sockaddr *)&client,&ipAddrSize);
if(UDPClient == INVALID_SOCKET)
{
printf("Failed to accept(): %d\n",WSAGetLastError());
exit(1);
}
我尝试使用 UDP 协议创建套接字。我收到 10045 错误。我之前使用 TCP 协议尝试过它,它在 TCP 协议上运行良好。我想尝试UDP协议。我更改了 UDP 协议所需的所有参数,但仍然出现 10045 错误。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)