Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 lomateron   Members   -  Reputation: 363

Like
0Likes
Like

Posted 18 October 2013 - 05:17 AM

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, 18 October 2013 - 05:19 AM.


Sponsor:

#2 Erik Rufelt   Crossbones+   -  Reputation: 3634

Like
0Likes
Like

Posted 18 October 2013 - 05:48 AM

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


#3 lomateron   Members   -  Reputation: 363

Like
0Likes
Like

Posted 18 October 2013 - 06:38 AM

ooohaa thanks wasted a whole day because of that error



#4 hplus0603   Moderators   -  Reputation: 5712

Like
0Likes
Like

Posted 18 October 2013 - 09:51 AM

Also, you don't want to use sendto() when the socket is already connected. You should use send() on connected sockets.


enum Bool { True, False, FileNotFound };




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS