简体   繁体   English

在Win32 UDP套接字应用程序中无法从UDP服务器接收数据

[英]Not able to receive data from UDP server in win32 UDP socket application

I have an hardware ARM control as UDP server and I am communicating to it via the code below written in C#. 我有一个硬件ARM控件作为UDP服务器,并且正在通过下面用C#编写的代码与之通信。 PC is UDP Client. PC是UDP客户端。 Server simply echoes the data. 服务器只是回显数据。

This works fine without any issue and this is stable. 这可以正常工作,没有任何问题,而且很稳定。

using System.Net.Sockets;
using System.Net;
using System.Text;
using System;

namespace UDPSocket
{
    class UDPSender
    {
        static void Main(string[] args)
        {
            UInt32 i=0;
            Int32 PORT = 45555;
            for (; i < 15; )
            {
                Console.WriteLine(i++);
                Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
                    ProtocolType.Udp);

                IPAddress broadcast = IPAddress.Parse("192.168.0.10");

                byte[] sendbuf = Encoding.ASCII.GetBytes("Shriganesh Damle, Infineon Techonologies India Pvt Ltd, Bangalore");
                IPEndPoint ep = new IPEndPoint(broadcast, PORT);

                //Creates a UdpClient for reading incoming data.
                UdpClient receivingUdpClient = new UdpClient(PORT);

                s.SendTo(sendbuf, ep);

                Console.WriteLine("Message sent to the broadcast address");

                //Creates an IPEndPoint to record the IP Address and port number of the sender.  
                // The IPEndPoint will allow you to read datagrams sent from any source.
                IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

                // Blocks until a message returns on this socket from a remote host.
                Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);

                string returnData = Encoding.ASCII.GetString(receiveBytes);

                Console.WriteLine("This is the message you received " +
                                            returnData.ToString());
                Console.WriteLine("This message was sent from " +
                                            RemoteIpEndPoint.Address.ToString() +
                                            " on their port number " +
                                            RemoteIpEndPoint.Port.ToString());
                //Console.Read();
                receivingUdpClient.Close();
            }
            Console.Read();
        }
    }
}

Now, I want the same PC code in Win32 C application. 现在,我要在Win32 C应用程序中使用相同的PC代码。 I tried the code below. 我尝试了下面的代码。

/*
    Simple udp client    
*/
#include<stdio.h>
#include<winsock2.h>

#pragma comment(lib,"ws2_32.lib") //Winsock Library

#define SERVER "192.168.0.10"  //ip address of udp server
#define BUFLEN 512  //Max length of buffer
#define PORT 45555   //The port on which to listen for incoming data

int main(void)
{
    struct sockaddr_in si_other;
    int s, slen=sizeof(si_other);
    char buf[BUFLEN];
    char message[BUFLEN];
    WSADATA wsa;

    //Initialise winsock
    printf("\nInitialising Winsock...");
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
    {
        printf("Failed. Error Code : %d",WSAGetLastError());
        exit(EXIT_FAILURE);
    }
    printf("Initialised.\n");

    //create socket
    if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR)
    {
        printf("socket() failed with error code : %d" , WSAGetLastError());
        exit(EXIT_FAILURE);
    }

    //setup address structure
    memset((char *) &si_other, 0, sizeof(si_other));
    si_other.sin_family = AF_INET;
    si_other.sin_port = htons(PORT);
    si_other.sin_addr.S_un.S_addr = inet_addr(SERVER);

    //start communication
    while(1)
    {
        printf("Enter message : ");
        gets(message);

        //send the message
        if (sendto(s, message, strlen(message) , 0 , (struct sockaddr *) &si_other, slen) == SOCKET_ERROR)
        {
            printf("sendto() failed with error code : %d" , WSAGetLastError());
            exit(EXIT_FAILURE);
        }

        //receive a reply and print it
        //clear the buffer by filling null, it might have previously received data
        memset(buf,'\0', BUFLEN);
        //try to receive some data, this is a blocking call
        if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen) == SOCKET_ERROR)
        {
            printf("recvfrom() failed with error code : %d" , WSAGetLastError());
            exit(EXIT_FAILURE);
        }

        puts(buf);
    }

    closesocket(s);
    WSACleanup();

    return 0;
}

This code is able to send to the UDP server and it echoes back. 此代码能够发送到UDP服务器,并回显。 But recvfrom call will be waiting for data till infinite time. 但是recvfrom调用将一直等待数据直到无限时间。 recvfrom is blocking call. recvfrom正在阻止通话。 I am giving correct IP address and PORT. 我提供了正确的IP地址和端口。 Still I am not able to receive data from server in Win32 app. 仍然无法从Win32应用程序中的服务器接收数据。 Can you help? 你能帮我吗?

If you want to receive data on a socket, you need to bind it to one or more local addresses and a local port. 如果要在套接字上接收数据,则需要将其绑定到一个或多个本地地址和一个本地端口。 Your C# version achieves this via new UdpClient(PORT) (note the PORT argument), but your C version does not do anything comparable. 您的C#版本通过new UdpClient(PORT) (请注意PORT参数)实现了此目的,但是您的C版本没有做任何可比的事情。

After creating the socket, do 创建套接字后,执行

sockaddr_in localAddr;

localAddr.sin_family = AF_INET;
localAddr.sin_port = htons(PORT);
localAddr.sin_addr.S_un.S_addr = INADDR_ANY;

bind(s, &localAddr, sizeof(sockaddr_in));

and verify that it returns 0 to indicate success. 并确认它返回0表示成功。

Alternatively, the C version would be more parallel to the C# version if you created a second socket for receiving the server response. 另外,如果您创建了用于接收服务器响应的第二个套接字,则C版本将与C#版本更相似。 I don't think it's necessary to do that in C (in Winsock2, to be more precise), but it is possible that using the same socket to both send and receive would require you to bind to a specific local address instead of to INADDR_ANY . 我认为没有必要在C中执行此操作(更确切地说,在Winsock2中),但是使用相同的套接字发送和接收都可能需要您绑定到特定的本地地址,而不是INADDR_ANY

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM