Search code examples
csocketspermission-deniedwinsock2

Winsock Error 1013- Permission Denied


I'm trying to run the simple program below using C:

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

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

int main(int argc, char **argv)
{
    int iResult;
    WSADATA wsa;
    SOCKET s;
    struct sockaddr_in server;

    printf("Initialising Winsock...\n");
    if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
    {
        printf("Failed. Error Code : %d", WSAGetLastError());
        getchar();
        return 1;
    }

    printf("Initialised.\n");

    //Create a socket
    if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
    {
        printf("Could not create socket : %d", WSAGetLastError());
        getchar();
    }

    printf("Socket created.\n");


    server.sin_addr.s_addr = inet_addr("74.125.235.20");
    server.sin_family = AF_INET;
    server.sin_port = htons(80);

    //Connect to remote server
    iResult = connect(s, (struct sockaddr *)&server, sizeof (server));
    if (iResult == SOCKET_ERROR)
    {
        printf("Connect function failed with error: %ld\n", WSAGetLastError());
        iResult = closesocket(s);
        if (iResult == SOCKET_ERROR)
            printf("closesocket function failed with error: %ld\n", WSAGetLastError());
        WSACleanup();
        getchar();
        return 1;
    }
    printf("Connected");
    getchar();
    return 0;
}

This is basically a C program that create a socket and makes a connection to Google. (I'm just following the tutorial: http://www.binarytides.com/winsock-socket-programming-tutorial as I'm completly new to socket programming).

Now my program outputs:

Initialising Winsock...
Initialised.
Socket Created.
Connect function failed with error: 10013

After some research I found that this means that this is a permissions denied error. I tried looking for some fixes such as running Visual Studio Express as Administrator and running these commands on my command prompt:

netsh winsock reset catalog
netsh int ip reset reset.log hit

and restart my computer but it still does not work.

Its worth mentioning when I ran the 2nd command: netsh int ip reset reset.log hit I got the following error message:

Resetting , failed.
Access is denied.
There's no user specified settings to be reset.

Even though I was running the command prompt as admin.

I also temporarily deactivated my Kaspersky Internet Security but still no fix. I am completely new to C and socket programming.


Solution

  • I've tried your code and I can use it to connect to some other machine. I made a little modification since I do not have a machine running a HTTP server at hand right now. Therefore I used www.google.com:

    struct sockaddr_in server;
    remoteHost = gethostbyname("www.google.com");   // get IP of www.google.com
    server.sin_addr.s_addr = *((unsigned long *)remoteHost->h_addr);  // inet_addr("74.125.235.20");
    server.sin_family = AF_INET;
    server.sin_port = htons(80);
    

    The output of your application is:

    Initialising Winsock...
    Initialised.
    Socket created.
    Connected
    

    So basically your implementation is correct.

    This means that there is some other gremlin around! The MSDN description of error 10013 is:

    WSAEACCES
    10013 (0x271D)
    An attempt was made to access a socket in a way forbidden by its access permissions.
    

    This can be caused by antivirus or firewall software. Therefore try to disable your firewall and anti virus and run your application again.

    If that works try to find the firewall/anti virus setting(s) which may block your application from connecting. This can be quite an ordeal (I know what I'm talking about)...