C ++ TCP套接字客户端无法发送数据

时间:2014-01-22 00:30:39

标签: c++ macos sockets tcp osx-mavericks

我正在用c ++编写一个简单的套接字客户端。这是代码:

main.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;
}

communications.h

#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__) */

communications.cpp

#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;
}

我尝试使用netcat来测试客户端:

$ nc -lv 8888

但它收到的数据似乎不正确:

$ nc -lv 8888
??_?

我的程序在运行时没有给我任何错误。这些数据来自哪里?

我正在运行Mac OS X Mavericks。 About this mac screenshot

1 个答案:

答案 0 :(得分:3)

你没有把任何数据放入send_str

中的缓冲区

我也怀疑sizeof(缓冲区)没有达到预期效果。我的猜测是它将是sizeof(char *)