1

I have a simple socket server written in c++ 10.0 and I need it to use SSL.

After dig into this for awhile, I was not able to find a solution.

My server now looks like this:

int connect_socket(int port) {
    WSADATA wsa;
    SOCKET s, new_socket;
    struct sockaddr_in server, client;
    int c;

    if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
        std::cout << "Error Code:" << WSAGetLastError() << '\n';
        return 1;
    }

    // Create a socket
    if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
        std::cout << "Could not create sockete:" << WSAGetLastError() << '\n';
        return 1;
    }

    // Prepare the sockaddr_in structure
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(port);

    // Bind
    if (bind(s, (struct sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) {
        std::cout << "Bind failed with error code:" << WSAGetLastError() << '\n';
        exit(EXIT_FAILURE);
    }

    // Listen to incoming connections
    listen(s, 3);

    std::cout << "Waiting for incoming connections..." << '\n';

    c = sizeof(struct sockaddr_in);

    while ((new_socket = accept(s, (struct sockaddr*)&client, &c)) != INVALID_SOCKET) {
        _beginthread(connection_handler, 0, &new_socket);
    }

    if (new_socket < 0) {
        std::cout << "recv failed with error: " << '\n';
        return 1;
    }

    return 0;
}

I try to implement OpenSSL following the example from here https://stackoverflow.com/a/16328115/1394578 but without success.

I don't have a client, just use telnet to connect with the server, when the server uses ssl I will just use openSSL to connect.

Because I'm not really comfortable with c this is a complicated task for me, if anyone can help with an example I will be grateful.

Community
  • 1
  • 1
Santos
  • 436
  • 1
  • 5
  • 11
  • First you need to have _OpenSSL_ installed on your machine. Then to incorporate _OpenSSL_ into the _VStudio_ project, you can follow the instructions that I posted [here](http://stackoverflow.com/questions/32156336/how-to-include-openssl-in-visual-studio-expres-2012-windows-7-x64/32158521#32158521). Finally, you can write the SSL code as shown in the link you mentioned. – CristiFati Sep 13 '16 at 17:25
  • Thank you CristiFati for your help. – Santos Sep 16 '16 at 12:18

0 Answers0