#I'm trying to create a simple local server and to connect with c++ code but it fails...

5 messages · Page 1 of 1 (latest)

cunning belfryBOT
#

When your question is answered use !solved or the button below to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

compact anchor
#

Client side:

#define _WINSOCK_DEPRECATED_NO_WARNINGS //Avoids me from warnings
#include <WinSock2.h>
#include <Windows.h>
#pragma comment(lib, "ws2_32.lib")

#include <iostream>

class Connect2 {

};

WSADATA wsaData;
int main() {

    // address family (The Internet Protocol version 4),
    // type specification for the socket (SOCK_STREAM),
    // protocol (TCP)
    SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    // connect
    struct sockaddr_in sa = { 0 };
    sa.sin_family = AF_INET;
    sa.sin_addr.s_addr = inet_addr("127.0.0.1");    //Server IP
    sa.sin_port = htons(8080); //converts the unsigned integer hostlong from host byte order to network ... ??
    connect(s, (struct sockaddr*)&sa, sizeof(sa));

    send(s, "Hello from client!", 18, 0);

    std::cout << "message from server: " << std::endl;
    char msg[1024] = { 0 };
    recv(s, msg, 1024, 0);
    std::cout << msg << std::endl;

    closesocket(s);

    return 0;
}

summer pike
#

you don't check the return value of either bind or listen for errors

#

bind could fail if there is already a process listening to the port

cunning belfryBOT
# cunning belfry

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity