C ++客户端/套接字问题

时间:2015-03-05 20:31:05

标签: c++ sockets

我刚开始学习一些C ++,并希望使用套接字建立一个简单的客户端/服务器连接。我或多或少地遵循了本教程:https://www.youtube.com/watch?v=WaLQ7s1X6wo

无论如何,这是客户端和服务器代码。

// Client.cpp : Defines the entry point for the console application.
//

#define SCK_VERSION2 0x0202;

#include <sdkddkver.h>
#include <conio.h>
#include "stdafx.h"
#include <winsock2.h>
#include <iostream>
#include <Windows.h>
#include <WS2tcpip.h>
#include <string>

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

int _tmain(int argc, _TCHAR* argv[])
{
    WSAData WinSockData;
    WORD DLLVERSION = MAKEWORD(2, 1);

    if (long checkWSA = WSAStartup(DLLVERSION, &WinSockData) == 0){
        std::cout << "\nWinSock started up jolly and good.";
    }
    else{
        std::cout << "\nWinsock failed for reason such as : " << checkWSA;
    }
    char* buffer = (char*)malloc(sizeof(IN_ADDR)); //ignore this

    SOCKADDR_IN ADDRESS;
    InetPton(AF_INET, (PCTSTR)"127.0.0.1", &ADDRESS.sin_addr);
    ADDRESS.sin_family = AF_INET;
    ADDRESS.sin_port = htons(444);

    std::cout << "\nAttempting to connect";

    SOCKET sock;
    sock = socket(AF_INET, SOCK_STREAM, NULL);



    if (int connectTry = connect(sock, (SOCKADDR*)&ADDRESS, sizeof(ADDRESS)) == 0){
        std::cout << " Connection successful? : " << connectTry;
    }
    else{
        std::cout << " Connection unsuccessful? : " << connectTry;
    }

    char MESSAGE[200];

    if (recv(sock, MESSAGE, sizeof(MESSAGE), NULL)){
        std::string str(MESSAGE);
        std::cout << "\nReceived from server: " << str;
    }
    else{
        std::cout << "\nNope.";
    }

    std::cout << "\nEnd";
    std::cin >> buffer;

    return 0;
}

服务器

    #define SCK_VERSION2 0x0202;

    #include <sdkddkver.h>
    #include <conio.h>
    #include "stdafx.h"
    #include <winsock2.h>
    #include <iostream>
    #include <Windows.h>
    #include <WS2tcpip.h>

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

    int _tmain(int argc, _TCHAR* argv[])
    {
        WSAData WinSockData;
        WORD DLLVERSION = MAKEWORD(2, 1);

        //Starts winsock dll
        if (long checkWSA = WSAStartup(DLLVERSION, &WinSockData) == 0){
            std::cout << "\nWinSock started up jolly and good.";
        }
        else{
            std::cout << "\nWinsock failed for reason such as : " << checkWSA;
        }

        //Make socket structure
        char* buffer = (char*) malloc(sizeof(IN_ADDR)); //ignore this

        SOCKADDR_IN ADDRESS;
        int AddressSize = sizeof(ADDRESS);
        InetPton(AF_INET, (PCTSTR)"127.0.0.1", &ADDRESS.sin_addr);
        ADDRESS.sin_family = AF_INET;
        ADDRESS.sin_port = htons(444);

        SOCKET 

sock_CONNECTION = socket(AF_INET, SOCK_STREAM, NULL);

    SOCKET sock_LISTEN = socket(AF_INET, SOCK_STREAM, NULL);
    bind(sock_LISTEN, (SOCKADDR*)&ADDRESS, sizeof(ADDRESS));
    listen(sock_LISTEN, SOMAXCONN);

    std::cout << "\nWaiting for connection...";

    if (sock_CONNECTION = accept(sock_LISTEN, (SOCKADDR*)&ADDRESS, &AddressSize)){
        std::cout << "\nConnected to: " << ADDRESS.sin_addr.S_un.S_addr;
        std::cout << "\n\n" << WSAGetLastError();
        send(sock_CONNECTION, "Welcome to the Wonderland!", 46, NULL);
    }
    else{
        std::cout << "\nNope";
    }

    std::cout << "\nEnd";
    std::cin >> buffer;

    return 0;
}

这是输出:

Output

我猜这里的问题就是这一行:

ADDRESS.sin_addr.s_addr = InetPton(AF_INET, (PCTSTR)"127.0.0.1", &ADDRESS.sin_addr);

我无法使用inet_addr,因为编译器告诉我使用InetPton()或#define _WINDOWS_IGNORE或类似的东西以忽略错误。毋庸置疑,我无法正确定义并忽略错误,所以我选择了InetPton()。

任何人都知道什么是错的?感谢。

1 个答案:

答案 0 :(得分:0)

或多或少看起来正确...请注意,您的服务器端永远不会向句柄sock_CONNECTION中的已接受连接发送任何内容,并且最终会将您创建的套接字泄漏到{{1}之前的变量中}。在您的客户端,您的接收不会终止消息... accept调用返回的长度应该为零,因为您从未向其发送过任何内容。因此,您只是打印一个未初始化的数组。

编辑1 :成功recv返回零,因此您的逻辑向后,所以看起来它连接好但是没有,这导致:

编辑2 :您对connect的来电覆盖了回复中的地址,成功时只有InetPton ...所以它正在尝试连接到了错误的地方。