• Advertisement
Sign in to follow this  

cant get to work the beej's UDP example on winsock

This topic is 1557 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I get error 10014 when doing sendto()

is getaddrinfo() first parameter correct, can you put there the ip number like that?

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>


// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")

int __cdecl main(int argc, char **argv) 
{
    WSADATA wsaData;
    SOCKET ConnectSocket = INVALID_SOCKET;
    struct addrinfo *result,*ptr,hints;
    int iResult;

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) 
	{
		printf("WSAStartup failed with error: %d\n", iResult);
		return 1;
	}

    ZeroMemory( &hints, sizeof(hints) );
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_DGRAM;
    hints.ai_protocol = IPPROTO_UDP;

    // Resolve the server address and port
    iResult = getaddrinfo("192.168.1.2", "2222", &hints, &result);
    if ( iResult != 0 ) 
	{
        printf("getaddrinfo failed with error: %d\n", iResult);
        WSACleanup();
        return 1;
    }

    // Attempt to connect to an address until one succeeds
    for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) 
	{
        // Create a SOCKET for connecting to server
        ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
        if (ConnectSocket == INVALID_SOCKET) 
		{
            printf("socket failed with error: %ld\n", WSAGetLastError());
            continue;
        }
        break;
    }

    freeaddrinfo(result);

    if (ptr == NULL) 
	{
        printf("Unable to connect to server!\n");
        WSACleanup();
        return 1;
    }

	char sendbuf[15] = "this is a test";
    // Send an initial buffer
    iResult = sendto( ConnectSocket, sendbuf, 15, 0 ,ptr->ai_addr, ptr->ai_addrlen );
    if (iResult == SOCKET_ERROR) 
	{
        printf("send failed with error: %d\n", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }

    // cleanup
    closesocket(ConnectSocket);
    WSACleanup();

    return 0;
}
Edited by lomateron

Share this post


Link to post
Share on other sites
Advertisement

You probably shouldn't do freeaddrinfo() and then use ptr again after already freeing it.

 

You can find error codes in WinError.h:

//
// MessageId: WSAEFAULT
//
// MessageText:
//
// The system detected an invalid pointer address in attempting to use a pointer argument in a call.
//
#define WSAEFAULT                        10014L

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement