Windows Socket Programming in C

后端 未结 3 1498
后悔当初
后悔当初 2020-12-16 06:06

I am taking a networking class where the Professor is literally reading the book to the class. Needless to say I have no Idea what I am doing. Our semester project is to cop

相关标签:
3条回答
  • 2020-12-16 06:29

    At first I will try to help using your last comment: Let us assume you are using Visual Studio (I think it is best option to start winsock for windows programming as Microsoft cares about Windows basic libraries being up to date and they are compatible with helpful msdn support).

    If you are getting error such as this one: 1>asdf.obj : error LNK2001: unresolved external symbol _imp_WSAStartup@8 it means that ws2_32.lib is not linked correctly. To do that right click on your project in solution explorer, go to linker -> input and add ws2_32.lib to additional dependencies. This library is part of windows SDK (I guess it is installed together with most versions of Visual Studio), so make sure the file exists on your computer.

    And now how to make correct project in modern style without following ancient tutorials:

    The library you need to add is Winsock2.h. Winsock.h is old (deprecated) version and there is no need to use it in new applications. To start using sockets you need to call function WSAStartup, to do that you must initialize struct WSADATA at the beginning. The basic piece of code looks like this:

    #include <Winsock2.h>
    int main()
    {
    WSADATA mywsadata; //your wsadata struct, it will be filled by WSAStartup
    WSAStartup(0x0202,&mywsadata); //0x0202 refers to version of sockets we want to use.
    //here goes your code with socket related things
    return 0;
    }
    

    For more help you can visit here

    A note: since question is old and I am not sure its author will ever find my answer helpful I want to help another users looking at this question

    0 讨论(0)
  • 2020-12-16 06:33

    You need to link the library Ws2_32.lib to use winsock. You also must call WSAStartup before using any other winsock functions (this isn't causing your current error, but will cause you problems once you fix the missing library issue).

    0 讨论(0)
  • 2020-12-16 06:35

    The following is a simple socket program (simple http client) that will run on both Windows and Linux. If you are using "gcc on windows" then you need to compile using the following command:

    gcc prog_name.c -lws2_32
    

    Code:

    #include <stdio.h>
    #include <errno.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    
    #if defined(_WIN32) || defined(_WIN64)
        #include <winsock2.h>
    #else
        #include <sys/socket.h>
        #include <arpa/inet.h>
    #endif
    
    #define MSG_SIZE 1024
    #define REPLY_SIZE 65536
    
    int main(int argc, char *argv[])
    {
        int s = -1;
        struct sockaddr_in server;
        char message[MSG_SIZE] = {0}, server_reply[REPLY_SIZE] = {0};
        int recv_size = 0;
    
    #if defined(_WIN32) || defined(_WIN64)    
        WSADATA wsa;
        if (WSAStartup(MAKEWORD(2,2),&wsa) != 0) {
            printf("\nError: Windows socket subsytsem could not be initialized. Error Code: %d. Exiting..\n", WSAGetLastError());
            exit(1);
        }
    #endif
        
        //Create a socket
        if((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)    {
            printf("Error: Could not create socket: %s. Exiting..\n", strerror(errno));
            exit(1);
        }
    
        // Fill in server's address
        memset(&server, 0, sizeof(server));
        server.sin_addr.s_addr = inet_addr("172.217.160.238"); // google.com
        server.sin_family = AF_INET;
        server.sin_port = htons(80);
    
        // Connect to server
        if (connect(s, (struct sockaddr *)(&server), sizeof(server)) < 0) {
            printf("Error: Could not connect to server: %s. Exiting..\n", strerror(errno));
            exit(1);
        }
        
        // Send HTTP request
        strcpy(message, "GET / HTTP/1.1\r\n\r\n");
        if(send(s, message, strlen(message), 0) < 0) {
            printf("Error: Could not send http request to server: %s. Exiting..\n", strerror(errno));
            exit(1);
        }
        
        // Receive a reply from the server
        printf("\nWaiting for server reply..\n");
        if((recv_size = recv(s, server_reply, REPLY_SIZE, 0)) < 0) {
            printf("Error: Something wrong happened while getting reply from server: %s. Exiting..\n", strerror(errno));
            exit(1);
        }
    
        server_reply[REPLY_SIZE - 1] = 0;
    
        printf("\nServer Reply:\n\n");
        printf("%s\n", server_reply);
    
        // Close the socket
    #if defined(_WIN32) || defined(_WIN64)  
        closesocket(s);
        WSACleanup();
    #else
        close(s);
    #endif
    
        exit(0);
    } // end of main
    
    0 讨论(0)
提交回复
热议问题