Advertisement

This doesn't work (anymore) and I have no clue why(Simple winsock)

Started by April 01, 2004 07:19 AM
0 comments, last by Nerusai 20 years, 10 months ago
The following does not work, I have this nice Norton Firewall installed, and it doesn''t even blink, not when listening on a port, not when sending data to the internet - nothing! I''m getting no errors at all, but I also ran some local data send/receive tests and nothing happened at all. It''s probably some silly mistake I forgot again, I''d appreciate it if someone could look at this and spank me for it.

#pragma comment ( lib, "ws2_32.lib" )

#define WIN32_LEAN_AND_MEAN

#include <Conio.h>
#include <Memory.h>
#include <Stdio.h>
#include <Windows.h>
#include <Winsock2.h>

void main ( )
{
    WSAData wsaData;
    if ( WSAStartup ( MAKEWORD ( 2, 2 ), &wsaData ) != 0 )
    {
        printf ( "Error: WSAStartup\n" );
    }

    SOCKET Socket;
    if ( ( Socket = socket ( PF_INET, IPPROTO_IP, SOCK_DGRAM ) ) == INVALID_SOCKET )
    {
        printf ( "Error: socket\n" );
    }

    sockaddr_in la;
    memset ( &la, 0, sizeof ( sockaddr ) );
    la.sin_addr.s_addr = htonl ( INADDR_ANY );
    la.sin_family      = AF_INET;
    la.sin_port        = htons ( 20000 );
    if ( bind ( Socket, ( sockaddr * ) &la, sizeof ( sockaddr ) ) == SOCKET_ERROR )
    {
        printf ( "Error: bind\n" );
    }

    printf ( "Press ''x'' to exit\n" );
    bool bExit;
    bExit = false;
    while ( !bExit )
    {
        if ( kbhit ( ) )
        {
            switch ( getch ( ) )
            {
            /* Send some random junk */
            case ''s'':
            case ''S'':
                {
                    printf ( "Send\n" );

                    char        buffer [256];
                    int         bytessend;
                    int         ralen;
                    sockaddr_in ra;
                    ralen = sizeof ( sockaddr );
                    memset ( &ra, 0, sizeof ( sockaddr ) );
                    ra.sin_addr.s_addr = inet_addr ( "66.218.71.198" );
                    ra.sin_family      = AF_INET;
                    ra.sin_port        = htons ( 80 );
                    memset ( &buffer, ''A'', 256 );
                    if ( ( bytessend = sendto ( Socket, buffer, 256, 0, ( sockaddr * ) &ra, ralen ) ) == SOCKET_ERROR )
                    {
                        printf ( "Error: sendto\n" );
                    }
                }
                break;

            /* Exit */
            case ''x'':
            case ''X'':
                {
                    printf ( "Exit\n" );

                    bExit = true;
                }
                break;
            }
        }
    }

    closesocket ( Socket );

    WSACleanup ( );
}
www.boycott-riaa.com
---Yesterday is history, tomorrow is a mystery, today is a gift and that's why it's called the present.
Hmmm, am I by any chance swapping IPPROTO_IP and SOCK_DGRAM >.<

Edit: Yup, mystery solved, conclusion: Studying messes with your head, don't code and study at once.

[edited by - Siaon on April 1, 2004 8:31:55 AM]
---Yesterday is history, tomorrow is a mystery, today is a gift and that's why it's called the present.

This topic is closed to new replies.

Advertisement