I am writing a simple socket client in c++. Here is the code:
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;
}
I tried to use netcat to test the client like this:
$ nc -lv 8888
But the data it receives seems is incorrect:
$ nc -lv 8888
??_?
My program does not give me any errors when I run it. Where is this data coming from?
I am running Mac OS X Mavericks.
you didnt put any data into buffer in send_str
also i suspect that sizeof(buffer) doesn't do what you expect. My guess is that it will be sizeof(char*)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.