Is UDP Sockets working on Win8, Win8.1 ? RRS feed

  • Question

  • Hi

    I need to use UDP sockets to connect to udp server. As server I have used a desktop VLC player with streaming video through UDP on . I have used next code for Win8, Win8.1 to receive data from VLC :

    #include <ppl.h> #include <ppltasks.h> #include <future> using namespace Windows::Networking; using namespace Windows::Networking::Sockets; DatagramSocket ^udp; // helper to not mess with writing code for CompletedHandler's static bool PerformSync(Windows::Foundation::IAsyncAction ^op) { std::promise<bool> prom; auto fut = prom.get_future(); op->Completed = ref new Windows::Foundation::AsyncActionCompletedHandler( [&](Windows::Foundation::IAsyncAction ^op, Windows::Foundation::AsyncStatus status) { if (status == Windows::Foundation::AsyncStatus::Completed) { prom.set_value(true); } else { prom.set_value(false); } }); auto result = fut.get(); return result; } void Class1::DoTest() { HostName ^hostName = ref new HostName("localhost"); this->udp = ref new DatagramSocket(); udp->MessageReceived += ref new Windows::Foundation::TypedEventHandler<Windows::Networking::Sockets::DatagramSocket ^, Windows::Networking::Sockets::DatagramSocketMessageReceivedEventArgs ^>(this, &Class1::OnMessageReceived);

    // also tried BindEndpointAsync, same result auto op = udp->ConnectAsync(hostName, "1234"); auto res = PerformSync(op); } void Class1::OnMessageReceived(Windows::Networking::Sockets::DatagramSocket ^sender, Windows::Networking::Sockets::DatagramSocketMessageReceivedEventArgs ^args) { // breakpoint on this line are never hit int stop = 234; }

    Also I have tried next code on WP8.1 emulator (on WP8.0 emulator it not works), wich works perfectly:

    #include <winsock2.h>
    #include <Ws2tcpip.h>
    // code inside any C++ method on wp8.1 :
            WSADATA wsaData;
    	auto wVersionRequested = MAKEWORD(2, 2);
    	auto err = WSAStartup(wVersionRequested, &wsaData);
    	const int DefaultBufLen = 1024 * 4;
    	char recvbuf[DefaultBufLen];
    	SOCKET connectSocket = INVALID_SOCKET;
    	sockaddr_in clientService;
    	int iResult;
    	memset(&clientService, 0, sizeof(clientService));
    	clientService.sin_addr.S_un.S_un_b.s_b1 = 127;
    	clientService.sin_addr.S_un.S_un_b.s_b2 = 0;
    	clientService.sin_addr.S_un.S_un_b.s_b3 = 0;
    	clientService.sin_addr.S_un.S_un_b.s_b4 = 1;
    	clientService.sin_family = AF_INET;
    	clientService.sin_port = htons(1234);
    	connectSocket = socket(AF_INET, SOCK_DGRAM, 0);
    	iResult = bind(connectSocket, (SOCKADDR*) &clientService, sizeof(clientService));
    	if (iResult == SOCKET_ERROR)
    		int stop = 234;
    	iResult = recv(connectSocket, recvbuf, DefaultBufLen, 0);

    But for some reason winsock2 available only on WP8, WP8.1 .

    So is there any way to get UDP sockets to work properly on Win8, Win8.1 or it is possible only on WP8, WP8.1 ?

    Thursday, July 31, 2014 10:35 AM

All replies

  • Hi Alexander - UDP networking is supported in the DatagramSocket class:


    Are you not able to use that class?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, July 31, 2014 1:27 PM
  • Sorry for posting not full question :) .

    The code inside 1st code block have OnMessageReceived() method and it's never gets called but recv() function inside 2nd code block returns readed bytes from socket. Maybe I missed something in 1st code block ?

    Thursday, July 31, 2014 2:54 PM