I'm trying to implement sctp client-server model using boost.asio framework.
Currently I have this for server:
#include <boost/asio.hpp>
#include <iostream>
#include <netinet/in.h>
#include <netinet/sctp.h>
#include <arpa/inet.h>
using namespace boost;
int main(int argc,char * argv[]){
if (argc < 2){
exit(0);
}
unsigned short MY_PORT_NUM = atoi(argv[1]);
struct sockaddr_in servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(MY_PORT_NUM);
try {
asio::io_context io_service;
boost::asio::generic::seq_packet_protocol::endpoint endpoint((struct sockaddr *)&servaddr, sizeof(servaddr),IPPROTO_SCTP);
boost::asio::basic_socket_acceptor<asio::generic::seq_packet_protocol> acceptor(io_service,endpoint);
boost::system::error_code ec;
asio::generic::seq_packet_protocol::socket socket(io_service);
acceptor.accept(socket,ec);
if (ec){
std::cout << ec.message() << "\n";
}
} catch ( boost::system::error_code& e){
std::cerr << e.message() << std::endl;
}
return 0;
}
I got an error message "Operation not supported".
I've also tried to make it this way:
try {
asio::io_context io_service;
boost::asio::generic::seq_packet_protocol p(AF_INET,IPPROTO_SCTP);
boost::asio::generic::seq_packet_protocol::endpoint endpoint((struct sockaddr *)&servaddr, sizeof(servaddr),IPPROTO_SCTP);
boost::asio::basic_socket_acceptor<asio::generic::seq_packet_protocol> acceptor(io_service);
acceptor.open(p);
acceptor.bind(endpoint);
acceptor.listen();
boost::system::error_code ec;
asio::generic::seq_packet_protocol::socket socket(io_service);
acceptor.accept(socket,ec);
if (ec){
std::cout << ec.message() << "\n";
}
} catch ( boost::system::error_code& e){
std::cerr << e.message() << std::endl;
}
And I've got exactly the same result.
You can compile this code with:
g++ sctp_server.cc -o sctp_server -lboost_thread -lboost_coroutine
I have C-version of sctp server, that's works fine:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/sctp.h>
#include <arpa/inet.h>
static void die(const char *s) {
perror(s);
exit(1);
}
static void server(unsigned short port){
struct sockaddr_in servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(port);
struct sctp_initmsg initmsg;
initmsg.sinit_max_attempts = 4;
initmsg.sinit_num_ostreams = 5;
initmsg.sinit_num_ostreams = 5;
int listen_fd = socket(AF_INET, SOCK_STREAM,IPPROTO_SCTP);
if (listen_fd < 0)
die("socket()");
if ( bind(listen_fd, (struct sockaddr *)&servaddr,sizeof(servaddr)) < 0)
die("bind()");
if ( setsockopt(listen_fd,IPPROTO_SCTP,SCTP_INITMSG,&initmsg,sizeof(initmsg)) < 0)
die("setsockopt()");
if ( listen(listen_fd,initmsg.sinit_max_instreams) < 0)
die("listen()");
for (;;){
char buffer[1024];
fputs("Waiting for connection\n",stdout);
int connect_fd = accept(listen_fd, (struct sockaddr *)NULL, NULL);
if (connect_fd < 0)
die("accept()");
fputs("New client connected\n",stdout);
struct sctp_sndrcvinfo sndrcvinfo;
int flags;
size_t bytesRcvd = sctp_recvmsg(connect_fd,buffer,sizeof(buffer),NULL,0,&sndrcvinfo,&flags);
if (bytesRcvd > 0){
printf("Received data : %s\n", buffer);
}
close(connect_fd);
}
}
int main(int argc, char * argv[]){
if (argc < 2)
die("Usage ./c_sctp_server <Port>");
unsigned short port = atoi(argv[1]);
server(port);
return 0;
}
You can compile this with: gcc c_sctp_server.c -o c_sctp_server -lsctp
The question is what is wrong with my boost.asio version?
Firstly, error_code is never raised. You probbably meant to catch system_error:
} catch (boost::system::system_error const& e) {
std::cerr << e.code().message() << " from " << e.code().location() << std::endl;
}
Note that you can often learn about the source location of the error as shown. In your case (slightly redacted):
Operation not supported from boost/asio/detail/reactive_socket_service.hpp:505:5 in function 'error_code reactive_socket_service<Protocol>::accept(implementation_type&, Socket&, endpoint_type*, error_code&)'
As the message indicates, accept is not supported by seq_packet_protocol. I think you are going to have more leverage starting from generic::stream_protocol which does know about connections ("associations" for SCTP).
You might also consider going from an existing POSIX SCTP example and gradually replacing bits by Asio.
Disclaimer: I'm not versed with SCTP. A casual glance at RFC 9260 makes me think it's not trivial to adapt, as the behaviors are pretty different (e.g. changing associations or peer addresses), even assuming you will be using POSIX API directly instead of also wrapping SCTP-specific socket options.
Using Error Code
Live On Coliru
#include <boost/asio.hpp>
#include <arpa/inet.h>
#include <iostream>
#include <linux/sctp.h>
#include <netinet/in.h>
namespace asio = boost::asio;
using boost::system::error_code;
int main(int argc, char* argv[]) {
std::vector args(argv + 1, argv + argc);
// if (args.size() != 2) exit(0);
uint16_t MY_PORT_NUM = atoi(args.at(0));
struct sockaddr_in servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(MY_PORT_NUM);
//using protocol = asio::generic::seq_packet_protocol;
using protocol = asio::generic::stream_protocol;
protocol::endpoint ep{
reinterpret_cast<struct sockaddr*>(&servaddr),
sizeof(servaddr),
IPPROTO_SCTP,
};
asio::io_context ioc;
asio::basic_socket_acceptor<protocol> acceptor(ioc);
error_code ec;
if (!ec)
acceptor.open({AF_INET, IPPROTO_SCTP}, ec);
if (!ec)
acceptor.bind(ep, ec);
if (!ec)
acceptor.listen(protocol::socket::max_listen_connections, ec);
protocol::socket socket(ioc);
if (!ec)
acceptor.accept(socket, ec);
std::cout << ec.message() << " from " << ec.location() << "\n";
}
Using Exceptions
Live On Coliru
#include <boost/asio.hpp>
#include <arpa/inet.h>
#include <iostream>
#include <linux/sctp.h>
#include <netinet/in.h>
namespace asio = boost::asio;
int main(int argc, char* argv[]) try {
std::vector args(argv + 1, argv + argc);
// if (args.size() != 2) exit(0);
uint16_t MY_PORT_NUM = atoi(args.front());
struct sockaddr_in servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(MY_PORT_NUM);
// using protocol = asio::generic::seq_packet_protocol;
using protocol = asio::generic::stream_protocol;
protocol::endpoint ep{
reinterpret_cast<struct sockaddr*>(&servaddr),
sizeof(servaddr),
IPPROTO_SCTP,
};
asio::io_context ioc;
asio::basic_socket_acceptor<protocol> acceptor(ioc);
acceptor.open({AF_INET, IPPROTO_SCTP});
acceptor.bind(ep);
acceptor.listen(protocol::socket::max_listen_connections);
protocol::socket socket(ioc);
acceptor.accept(socket);
} catch (boost::system::system_error const& se) {
std::cout << se.code().message() << " from " << se.code().location() << "\n";
}
Related
At first, here is my source code :
#include "server.h"
#include <netinet/in.h>
#include <unistd.h>
#include <thread>
#include <iostream>
#include <arpa/inet.h>
#include <sys/socket.h>
Server::Server(int port)
{
m_Port = port;
int server_socket = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(port);
server_address.sin_addr.s_addr = INADDR_ANY;
bind(server_socket, (sockaddr *)&server_address, sizeof(server_address));
listen(server_socket, SOMAXCONN);
m_Socket = server_socket;
}
Server::~Server()
{
close(m_Socket);
}
void Server::Process_Connection(const std::string message) const
{
std::cout << message << "\n";
}
void Server::Start() const
{
constexpr size_t BUFFER_SIZE = 4096;
for (;;)
{
char buffer[BUFFER_SIZE] = "";
sockaddr_in their_add;
int connection = accept(this->m_Socket, (sockaddr *)&their_add, NULL);
read(connection, buffer, 1024);
std::cout << "Received: " << buffer << "\n";
// std::cout << "Number of bytes read: " << val_read << "\n";
std::thread temp(&Server::Process_Connection, this, buffer);
temp.join();
}
}
The problem is that in the line 57, the connection
int connection = accept(this->m_Socket, (sockaddr*)&their_add, NULL);
gets a value of -1, which is an invalid connection.
Do you have any suggestions? I'm quite new to the socket programming.
Thank you in advance for your help
Instead of this:
int connection = accept(this->m_Socket, (sockaddr*)&their_add, NULL);
This:
socklen_t size = sizeof(their_add);
int connection = accept(this->m_Socket, (sockaddr*)&their_add, &size);
I am creating my own proxy server. Howerver, i don't know how to listen to browser. Moreover, i have some questions:
Do i need to listen to request from browser?
Do i have to display the received content on browser or just print all the html tags in console screen?
I think as every proxy server works, my program would get request from the client (browser), forward it to Web Server then receive the content from server, finally forward to the client.
#pragma comment(lib, "Ws2_32.lib")
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <sys/types.h>
#include <iostream>
#include <fcntl.h>
#include <WS2tcpip.h>
#include <sys/stat.h>
#include <io.h>
#include <process.h> /* for getpid() and the exec..() family */
#include <direct.h>
using namespace std;
bool checkSubString(char *str, char*substr)
{
if (strstr(str, substr) != NULL)
{
return true;
}
return false;
}
int main()
{
WSADATA wsData;
WORD ver = MAKEWORD(2, 2);
int wsOK = WSAStartup(ver, &wsData);
if (wsOK != 0)
{
cerr << "cant init winsock" << endl; return 0;
}
char message[1024];
int sockcheck = 0;
sockaddr_in server_input_addr;
memset(&server_input_addr, '0', sizeof(server_input_addr));
memset(&message, '0', sizeof(message));
sockcheck = socket(AF_INET, SOCK_STREAM, 0);
if (sockcheck < 0)
{
cerr << "Error while creating socket!!!\n";
return 0;
}
server_input_addr.sin_family = AF_INET;
server_input_addr.sin_addr.S_un.S_addr = INADDR_ANY;
server_input_addr.sin_port = htons(8888);
bind(sockcheck, (sockaddr*)&server_input_addr, sizeof(server_input_addr));
listen(sockcheck, 5);
int connFd = 0;
int n = 0;
int client_length = sizeof(server_input_addr);
while (1) {
connFd = accept(sockcheck, (sockaddr*)&server_input_addr, &client_length);
if (connFd < 0)
{
cerr << "\nError in accepting message from browser"; return 0;
}
n = _read(connFd, message, 1023);
if (n > 0)
{
cerr << "ERROR reading from socket\n";
}
_write(connFd, "Message received", 15);
_close(connFd);
}
cout << "\nSuccess!!!\n";
}
i wrote an script, who can create an connection to an HTTP Server and shows the content of the website in the console.
Very easy.
But i want to connect to an https server and do the same procedures.
I searched at google and didn't found what i searched.
Please help me and give me an tutorial who can i use the openssl library.
I tried myself on the openssl library, but the library is very complicated and difficult to understand.
Here is my code of the http client:
#include <iostream>
#include <ctype.h>
#include <cstring>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <sstream>
#include <fstream>
#include <string>
#include <arpa/inet.h>
#include <openssl/ssl.h>
using namespace std;
int sock;
struct sockaddr_in client;
int PORT = 80;
int main(int argc, char const *argv[])
{
bzero(&client, sizeof(client));
client.sin_family = AF_INET;
client.sin_port = htons( PORT );
client.sin_addr.s_addr = inet_addr("172.16.0.6");
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
cout << "Error creating socket." << endl;
exit(1);
}
if ( connect(sock, (struct sockaddr *)&client, sizeof(client)) < 0 ) {
close(sock);
cout << "Could not connect" << endl;
exit(1);
}
stringstream ss;
ss << "GET /" << "\r\n"
<< "Host: 172.16.1.4\r\n"
<< "Accept: application/json\r\n"
<< "Connection: close"
<< "\r\n\r\n";
string request = ss.str();
if (send(sock, request.c_str(), request.length(), 0) != (int)request.length()) {
cout << "Error sending request." << endl;
exit(1);
}
char cur;
while ( read(sock, &cur, 1) > 0 ) {
cout << cur;
}
return 0;
}
Here is a sample SSL client that connects to https://about.google/intl/en/ and prints downloaded a page : SSLClient.cpp
//============================================================================
// Name : SSLClient.cpp
// Compiling : g++ -c -o SSLClient.o SSLClient.cpp
// g++ -o SSLClient SSLClient.o -lssl -lcrypto
//============================================================================
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <string.h>
using namespace std;
SSL *ssl;
int sock;
int RecvPacket()
{
int len=100;
char buf[1000000];
do {
len=SSL_read(ssl, buf, 100);
buf[len]=0;
printf("%s\n",buf);
// fprintf(fp, "%s",buf);
} while (len > 0);
if (len < 0) {
int err = SSL_get_error(ssl, len);
if (err == SSL_ERROR_WANT_READ)
return 0;
if (err == SSL_ERROR_WANT_WRITE)
return 0;
if (err == SSL_ERROR_ZERO_RETURN || err == SSL_ERROR_SYSCALL || err == SSL_ERROR_SSL)
return -1;
}
}
int SendPacket(const char *buf)
{
int len = SSL_write(ssl, buf, strlen(buf));
if (len < 0) {
int err = SSL_get_error(ssl, len);
switch (err) {
case SSL_ERROR_WANT_WRITE:
return 0;
case SSL_ERROR_WANT_READ:
return 0;
case SSL_ERROR_ZERO_RETURN:
case SSL_ERROR_SYSCALL:
case SSL_ERROR_SSL:
default:
return -1;
}
}
}
void log_ssl()
{
int err;
while (err = ERR_get_error()) {
char *str = ERR_error_string(err, 0);
if (!str)
return;
printf(str);
printf("\n");
fflush(stdout);
}
}
int main(int argc, char *argv[])
{
int s;
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0) {
printf("Error creating socket.\n");
return -1;
}
struct sockaddr_in sa;
memset (&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr("173.194.222.139"); // address of google.ru
sa.sin_port = htons (443);
socklen_t socklen = sizeof(sa);
if (connect(s, (struct sockaddr *)&sa, socklen)) {
printf("Error connecting to server.\n");
return -1;
}
SSL_library_init();
SSLeay_add_ssl_algorithms();
SSL_load_error_strings();
const SSL_METHOD *meth = TLSv1_2_client_method();
SSL_CTX *ctx = SSL_CTX_new (meth);
ssl = SSL_new (ctx);
if (!ssl) {
printf("Error creating SSL.\n");
log_ssl();
return -1;
}
sock = SSL_get_fd(ssl);
SSL_set_fd(ssl, s);
int err = SSL_connect(ssl);
if (err <= 0) {
printf("Error creating SSL connection. err=%x\n", err);
log_ssl();
fflush(stdout);
return -1;
}
printf ("SSL connection using %s\n", SSL_get_cipher (ssl));
char *request = "GET https://about.google/intl/en/ HTTP/1.1\r\n\r\n";
SendPacket(request);
RecvPacket();
return 0;
}
Note that if you want to exchange data between client and server with openssl, you might need to process error codes SSL_ERROR_WANT_READ and SSL_ERROR_WANT_WRITE as described in documentation. But it's not necessary here as HTTPS protocol is serial.
I would like to send a string: "Jane Doe" to intranet ip 192.168.0.4 to port 9000 over UDP. I have done this many times via UDP and TCP by Java, but now I have to do it with standard C++ libraries and I can't find any samples only topics where people just can't make it work.
I know that I have to encode "Jane Doe" as array of bytes then just open socket, pack it in datagram and send it.
C++ is not my first language and this is small part of code I can't figure out, I've chosen UDP because it is always much simpler than TCP.
A good source for network programming is Beej's Guide to Network Programming. Below is some sample Unix code.
If this is Windows programming:
"sock" should be of type SOCKET instead of int.
Use closesocket instead of close
#include <winsock2.h> instead of all those unix headers
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <memory.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <errno.h>
#include <stdlib.h>
#include <iostream>
int resolvehelper(const char* hostname, int family, const char* service, sockaddr_storage* pAddr)
{
int result;
addrinfo* result_list = NULL;
addrinfo hints = {};
hints.ai_family = family;
hints.ai_socktype = SOCK_DGRAM; // without this flag, getaddrinfo will return 3x the number of addresses (one for each socket type).
result = getaddrinfo(hostname, service, &hints, &result_list);
if (result == 0)
{
//ASSERT(result_list->ai_addrlen <= sizeof(sockaddr_in));
memcpy(pAddr, result_list->ai_addr, result_list->ai_addrlen);
freeaddrinfo(result_list);
}
return result;
}
int main()
{
int result = 0;
int sock = socket(AF_INET, SOCK_DGRAM, 0);
char szIP[100];
sockaddr_in addrListen = {}; // zero-int, sin_port is 0, which picks a random port for bind.
addrListen.sin_family = AF_INET;
result = bind(sock, (sockaddr*)&addrListen, sizeof(addrListen));
if (result == -1)
{
int lasterror = errno;
std::cout << "error: " << lasterror;
exit(1);
}
sockaddr_storage addrDest = {};
result = resolvehelper("192.168.0.4", AF_INET, "9000", &addrDest);
if (result != 0)
{
int lasterror = errno;
std::cout << "error: " << lasterror;
exit(1);
}
const char* msg = "Jane Doe";
size_t msg_length = strlen(msg);
result = sendto(sock, msg, msg_length, 0, (sockaddr*)&addrDest, sizeof(addrDest));
std::cout << result << " bytes sent" << std::endl;
return 0;
}
This is very easy to do if you are willing to use the boost library.
Here is the code snippit
#include "boost/asio.hpp"
using namespace boost::asio;
...
io_service io_service;
ip::udp::socket socket(io_service);
ip::udp::endpoint remote_endpoint;
socket.open(ip::udp::v4());
remote_endpoint = ip::udp::endpoint(ip::address::from_string("192.168.0.4"), 9000);
boost::system::error_code err;
socket.send_to(buffer("Jane Doe", 8), remote_endpoint, 0, err);
socket.close();
I rewrote selbie's code to make it more C++-like and I minimized it a bit.
#include <iostream>
#include <string>
#include <arpa/inet.h> // htons, inet_addr
#include <netinet/in.h> // sockaddr_in
#include <sys/types.h> // uint16_t
#include <sys/socket.h> // socket, sendto
#include <unistd.h> // close
int main(int argc, char const *argv[])
{
std::string hostname{"192.168.0.4"};
uint16_t port = 9000;
int sock = ::socket(AF_INET, SOCK_DGRAM, 0);
sockaddr_in destination;
destination.sin_family = AF_INET;
destination.sin_port = htons(port);
destination.sin_addr.s_addr = inet_addr(hostname.c_str());
std::string msg = "Jane Doe";
int n_bytes = ::sendto(sock, msg.c_str(), msg.length(), 0, reinterpret_cast<sockaddr*>(&destination), sizeof(destination));
std::cout << n_bytes << " bytes sent" << std::endl;
::close(sock);
return 0;
}
For Windows, I took Mikolasan's minimised version of selbie's code and modified according to https://beej.us/guide/bgnet/html/#windows to get a small standalone example.
To get this to compile, you'll need to link the Winsock library.
#include <iostream>
#include <string>
#include <winsock2.h>
int main()
{
// Initialise Winsock DLL
// See https://beej.us/guide/bgnet/html/#windows
WSADATA wsaData;
// MAKEWORD(1,1) for Winsock 1.1, MAKEWORD(2,0) for Winsock 2.0
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
fprintf(stderr, "WSAStartup failed.\n");
exit(1);
}
// Set up connection and send
std::string hostname{ "192.168.0.4" };
uint16_t port = 9000;
SOCKET sock = ::socket(AF_INET, SOCK_DGRAM, 0);
sockaddr_in destination;
destination.sin_family = AF_INET;
destination.sin_port = htons(port);
destination.sin_addr.s_addr = inet_addr(hostname.c_str());
std::string msg = "Jane Doe";
int n_bytes = ::sendto(sock, msg.c_str(), msg.length(), 0, reinterpret_cast<sockaddr*>(&destination), sizeof(destination));
std::cout << n_bytes << " bytes sent" << std::endl;
::closesocket(sock);
// Clean up sockets library
WSACleanup();
return 0;
}
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*)