1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
| #include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <string.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(param) close(param)
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
using namespace std;
void recept(SOCKET sock, int taille = 1024)
{
char reponse[taille];
int retval;
while(1)
{
retval = recv(sock, reponse, sizeof(reponse), 0);
if(retval == 0)
break;
else if(retval == -1)
{
cout << "ERREUR : Reception reponse!" << endl;
break;
}
else
cout << reponse << endl;
}
}
void recept_S(SOCKET sock, int taille = 1024)
{
char reponse[taille];
int retval;
retval = recv(sock, reponse, sizeof(reponse), 0);
if(retval == -1)
cout << "ERREUR : Reception reponse!" << endl;
else if(retval != 0)
cout << reponse << endl;
}
int main()
{
cout << "Test socket" << endl << endl;
// ===== Creation socket =====
SOCKET sock;
sock = socket(AF_INET, SOCK_STREAM, 0); //Création de la socket
if(sock != INVALID_SOCKET)
{cout << "Socket : Creee" << endl;}
else
{
cout << "Erreur : Creation socket!" << endl;
return 1;
}
// ===== Parametrage socket =====
SOCKADDR_IN target;
hostent *server_adress = gethostbyname("smtp.bbox.fr");
target.sin_addr.s_addr = *((unsigned long *)server_adress->h_addr_list[0]);
target.sin_family = AF_INET;
target.sin_port = htons(25);
cout << "Socket : Parametree" << endl;
// ===== Connexion =====
cout << "Socket : Connection en cour..." << endl;
if(connect(sock, (SOCKADDR*)&target, sizeof(target)) == 0)
{cout << "Socket : Connectee" << endl;}
else
{
cout << "Erreur : Connection socket!" << endl;
return 1;
}
// ===== Envois donnees =====
cout << "Envois en cour..." << endl;
char HELO[] = "HELO smtp.gmail.com\n";
send(sock, HELO, sizeof(HELO), 0);
sleep(1);
recept_S(sock);
char FROM[] = "MAIL FROM:test@yahoo.com\n";
send(sock, FROM, sizeof(FROM), 0);
sleep(1);
recept_S(sock);
char TO[] = "RCPT TO:<exemple@gmail.com>\n";
send(sock, TO, sizeof(TO), 0);
sleep(1);
recept_S(sock);
char DATA[] = "DATA\n";
send(sock, DATA, sizeof(DATA), 0);
sleep(1);
recept_S(sock);
char DATA1[] = "Corp du message\n";
send(sock, DATA1, sizeof(DATA1), 0);
sleep(1);
recept_S(sock);
char FIN[] = ".\n";
send(sock, FIN, sizeof(FIN), 0);
sleep(1);
recept_S(sock);
char QUIT[] = "QUIT\n";
send(sock, QUIT, sizeof(QUIT), 0);
sleep(1);
recept_S(sock);
// ===== Fermeture socket =====
if(closesocket(sock) == 0)
{cout << "Socket : Fermee" << endl;}
else
{
cout << "Erreur : Fermeture socket!" << endl;
return 1;
}
cout << endl << "Fin!" << endl;
return 0;
} |