gpt4 book ai didi

C++ TCP Socket客户端发送数据失败

转载 作者:可可西里 更新时间:2023-11-01 02:45:26 26 4
gpt4 key购买 nike

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

主要.h:

#ifndef CC_Client_main_h
#define CC_Client_main_h

void error(std::string msg);


#endif

主要.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;
}

通讯.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__) */

通讯.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

最佳答案

你没有将任何数据放入 send_str 的缓冲区

我还怀疑 sizeof(buffer) 没有达到您的预期。我的猜测是它将是 sizeof(char*)

关于C++ TCP Socket客户端发送数据失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21271928/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com