UDP客户端未接收UDP服务器消息

时间:2015-09-09 04:39:01

标签: c udp winsock winsock2

Hai朋友我是新手学习winsock2。以下是我的udp服务器和客户端程序。

我这个程序客户端不知道服务器的IP地址,但只知道端口。但是服务器在整个网络上广播消息。

当客户端收到消息时,它会追溯服务器的ip并连接到它。

我的服务器和客户端在编译时都没有显示错误。

但执行客户端的recvfrom()语句时显示错误 以下是我的代码的一部分。

服务器代码:

#include "stdafx.h"
#include<stdio.h>
#include<WinSock2.h>
#pragma comment (lib,"ws2_32.lib")

DWORD WINAPI UDPCONN(LPVOID x)
{
    SOCKET s=(SOCKET)x;
    char bro[200]="I am SERVER";
    struct sockaddr_in hum;
    hum.sin_family=AF_INET;
    hum.sin_addr.s_addr=INADDR_BROADCAST;
    hum.sin_port=htons(8888);
    while (1)
    {
        if(sendto(s,bro,sizeof(bro),0,(struct sockaddr *)&hum,sizeof(hum))==SOCKET_ERROR)
        {
            printf("\nBroadcast failed ERROR CODE : %d\n",WSAGetLastError());
            exit(1);
        }
    }
 }

int _tmain(int argc, _TCHAR* argv[])
{
    SOCKET s;
    struct sockaddr_in hum;
    int opt=1;

    //Initializing winsock2
    WSADATA wsa;
    if((WSAStartup(MAKEWORD(2,2),&wsa))!=0)
    {
        printf("\nWinsock Not initialized ERROR CODE : %d\n",WSAGetLastError());
        return 1;
    }

    //UDP Socket Creation
    if((s=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP))==INVALID_SOCKET)
    {
        printf("\nUDP Socket not created ERROR CODE : %d\n",WSAGetLastError());
        WSACleanup();
        return 1;
    }

    //socket defenition
    hum.sin_family=AF_INET;
    hum.sin_addr.s_addr=INADDR_ANY;
    hum.sin_port=htons(8888);

    //Broadcast permission
    if((setsockopt(s,SOL_SOCKET,SO_BROADCAST,(char *)&opt,sizeof(opt)))<0)
    {
        printf("\nBroadcast permissions failed ERROR CODE : %d\n",WSAGetLastError());
        WSACleanup();
        return 1;
    }

    //UDP SOCKET Binding 
    if((bind(s,(sockaddr *)&hum,sizeof(hum)))==SOCKET_ERROR)
    {
        printf("\nUDP socket binding failed ERROR CODE :%d\n",WSAGetLastError());
        WSACleanup();
        return 1;
    }

    //UDP connection thread
    DWORD th;
    CreateThread(NULL,0,UDPCONN,(LPVOID)s,0,&th);

客户代码:

#include "stdafx.h"
#include <stdio.h>
#include <WinSock2.h>
#pragma comment(lib,"ws2_32.lib")

int _tmain(int argc, _TCHAR* argv[])
{
    //UDP Data
    int slen;
    char message[300];
    int opt=1;
    SOCKET s;
    struct sockaddr_in sent;

    //Initializing winsock
    WSADATA wsa;
    if((WSAStartup(MAKEWORD(2,2),&wsa))!=0)
    {
        printf("\nFailed Initializing Winsock EROR CODE : %d\n",WSAGetLastError());
        return 1;
    }

    //UDP Socket creation
    if((s=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP))== SOCKET_ERROR)
    {
        printf("\nUDP socket creation failed ERROR CODE :%d\n",WSAGetLastError());
        WSACleanup();
        return 1;
    }

    //UDP Broadcast permissions
    if((setsockopt(s,SOL_SOCKET,SO_BROADCAST,(char *)&opt,sizeof(opt)))<0)
    {
        printf("\nERROR in broadcasting ERROR CODE : %d \n",WSAGetLastError());
        WSACleanup();
        return 1;
    }

    //UDP socket definition
    sent.sin_family=AF_INET;
    sent.sin_addr.s_addr=INADDR_ANY;
    sent.sin_port=htons(8888);

    //UDP Receiving broadcasted data
    slen=sizeof(sent);
    //fflush(stdout);
    memset(message,'\0',300);
    if((recvfrom(s,message,sizeof(message),0,(struct sockaddr *)&sent,&slen))<0)
    {
        printf("\nUDP Broadcast not received ERROR CODE : %d\n",WSAGetLastError());
        WSACleanup();
        return 1;
    }
    puts("\nGot server broadcast\n");
    puts("\nTracing server ip\n");
    getpeername(s,(sockaddr *)&sent,&slen);

    ...
}

这里我的客户端抛出错误

UDP Broadcast not received ERROR CODE : 10022

错误代码10022的描述:参数无效。

提供了一些无效参数(例如,为setsockopt函数指定了无效级别)。在某些情况下,它还引用套接字的当前状态 - 例如,在未侦听的套接字上调用accept。

根据错误描述,recvfrom()函数中的一个参数无效。但我无法找出无效的论点。

请帮我清除错误。

0 个答案:

没有答案