C++;TCP套接字客户端无法发送数据 我正在C++中编写一个简单的Socket客户端。代码如下:

C++;TCP套接字客户端无法发送数据 我正在C++中编写一个简单的Socket客户端。代码如下:,c++,macos,sockets,tcp,osx-mavericks,C++,Macos,Sockets,Tcp,Osx Mavericks,主要条款h: #ifndef CC_Client_main_h #define CC_Client_main_h void error(std::string msg); #endif main.cpp #include <iostream> #include "communications.h" #include "main.h" void error(std::string msg) { std::cerr << msg; exit(-1);

主要条款h:

#ifndef CC_Client_main_h
#define CC_Client_main_h

void error(std::string msg);


#endif
main.cpp

#include <iostream>
#include "communications.h"
#include "main.h"
void error(std::string msg) {
    std::cerr << msg;
    exit(-1);
}


int main(int argc, char **argv) {
    Communication communication = Communication("localhost", 8888);
    communication.print_hosts();
    int success = communication.send_str("hello!\n");
    if (success<0) {
        std::cerr << "Error writing data.\n";
    }
    return 0;
}
我的程序在运行时不会给我任何错误。这些数据来自哪里

我正在运行MacOSX小牛。

您没有在send\u str中将任何数据放入缓冲区


我还怀疑sizeof(buffer)并没有达到您期望的效果。我猜是sizeof(char*)

您在send\u str中没有将任何数据放入缓冲区


我还怀疑sizeof(buffer)并没有达到您期望的效果。我猜它会是sizeof(char*)

对不起。我很累,这很尴尬,所以我要删除这个问题。再次抱歉。你能帮我标记一下吗,这样我就可以删除它了?哦,很公平。我将永远生活在耻辱中:)而且,我应该使用strlen(buffer)+1而不是
sizeof()
吗?不!除非有一把猎枪指着你的头,否则不要使用strlen()(即使这样,也要试着用你自己的方式说出来)。对不起。我很累,这很尴尬,所以我要删除这个问题。再次抱歉。你能帮我标记一下吗,这样我就可以删除它了?哦,很公平。我将永远生活在耻辱中:)而且,我应该使用strlen(buffer)+1而不是
sizeof()
吗?不!除非有一把猎枪指着你的头,否则不要使用strlen()(即使这样,也要试着用你的方式摆脱它)。
#ifndef __CC_Client__communications__
#define __CC_Client__communications__
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <time.h>
#include <netdb.h>
#include <string>
#include <iostream>
#include main.h

class Communication {
private:
    int sock;
    struct hostent *host;
    struct sockaddr_in server_address;
    char *host_str;
    int port;
public:
    Communication(char *host, int port);
    ~Communication(void);
    hostent &get_host();
    void print_hosts(void);
    int send_str(char *send_string);
};

#endif /* defined(__CC_Client__communications__) */
#include "communications.h"
#include "main.h"
void print_addr(unsigned char *address) {
    printf("%d.%d.%d.%d\n", address[0], address[1], address[2], address[3]);
}

Communication::Communication(char *host, int port) {
    this->port = port;
    this->host_str = host;
    this->sock = socket(AF_INET, SOCK_STREAM, 0);
    if (this->sock<0) {
        error("Failed to build socker object.\n");
    }
    this->host = gethostbyname(host);
    if (!this->host) {
        error("Failed to resolve host.\n");
    }
    memset((char*)&this->server_address, 0, sizeof(this->server_address));
    server_address.sin_family = AF_INET;
    server_address.sin_port = htons(port);

    memcpy((void *)&this->server_address.sin_addr, this->host->h_addr_list[0], this->host->h_length);

    if (connect(this->sock, (struct sockaddr*)&server_address, sizeof(this->server_address))<0) {
        error("Failed to connect socket.\n");
    }
}

Communication::~Communication() {
    std::cout << "Closing connection. . .\n";
    shutdown(this->sock, SHUT_RDWR);
    std::cout << "Communication object at " << this << " being destroyed\n";
}

void Communication::print_hosts() {
    for (int i=0; this->host->h_addr_list[i]!=0; i++) {
        print_addr((unsigned char*) this->host->h_addr_list[i]);
    }
}

int Communication::send_str(char *send_string) {
    char buffer[strlen(send_string)];
    int num_bytes = write(this->sock, buffer, sizeof(buffer));
    return num_bytes;
}
$ nc -lv 8888
??_?