Di seguito è riportato il codice seguente presa di base mi si avvicinò con:Creazione di una base C/C++ socket TCP scrittore
//General includes:
#include <iostream>
#include <stdio.h>
#include <string>
//Network related includes:
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
//Target host details:
#define PORT 1234
#define HOST "74.74.74.74"
using namespace std;
//Function prototypes:
string MessageFormat(int, char**);
void MessageSend(string);
int main(int argc, char *argv[])
{
//Parse arguments and format message:
string message = MessageFormat(argc, argv);
//Send the message out:
MessageSend(message);
return 0;
}
string MessageFormat(int argc, char *argv[])
{
//Massage the command line parameters
// into my desired payload format.
return message;
}
void MessageSend(string message)
{
int sd, ret;
struct sockaddr_in server;
struct in_addr ipv4addr;
struct hostent *hp;
sd = socket(AF_INET,SOCK_DGRAM,0);
server.sin_family = AF_INET;
inet_pton(AF_INET, HOST, &ipv4addr);
hp = gethostbyaddr(&ipv4addr, sizeof ipv4addr, AF_INET);
//hp = gethostbyname(HOST);
bcopy(hp->h_addr, &(server.sin_addr.s_addr), hp->h_length);
server.sin_port = htons(PORT);
connect(sd, (const sockaddr *)&server, sizeof(server));
send(sd, (char *)message.c_str(), strlen((char *)message.c_str()), 0);
}
questo è abbastanza semplice, e lo fa nel lavoro fatto. TUTTAVIA, invia pacchetti UDP invece di pacchetti TCP, quindi l'host di destinazione che si aspetta TCP li rifiuta. Inoltre, controllando i valori di connessione/invio e guardando le mie interfacce con ngrep, posso verificare al 100% che il pacchetto stia uscendo, quindi non è questo il problema.
Mi interessa solo modificare ciò che ho, non creare un server completo con boost asio. Come posso modificare questo in modo che funzioni in termini di TCP anziché UDP?
Cambia 'SOCK_DGRAM' in' SOCK_STREAM '. (Indizio: UDP = Utente ** Protocollo ** Protocollo). – BoBTFish
Naturalmente. Grazie. – kmarks2