-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_socket.c~
More file actions
69 lines (59 loc) · 1.85 KB
/
test_socket.c~
File metadata and controls
69 lines (59 loc) · 1.85 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h> /* close */
#include <netdb.h> /* gethostbyname */
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(s) close(s)
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
typedef struct in_addr IN_ADDR;
/* Sa mère ! ce truc marche !
*/
int main (int argc, char *argv[]) {
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock == INVALID_SOCKET)
{
perror("socket()");
exit(errno);
}
if (argc < 2)
{
fprintf (stderr, "Too few arguments : %s fqdn \nBitch !\n", argv[0]);
exit(errno);
}
struct hostent *hostinfo = NULL;
SOCKADDR_IN sin = { 0 }; /* initialise la structure avec des 0 */
const char *hostname = argv[1];
hostinfo = gethostbyname(hostname); /* on récupère les informations de l'hôte auquel on veut se connecter */
fprintf (stdout, "Résolution DNS dans ta face!\n");
if (hostinfo == NULL) /* l'hôte n'existe pas */
{
fprintf (stderr, "Unknown host %s.\n", hostname);
exit(EXIT_FAILURE);
} else {
fprintf (stdout, "Say my number, bitch : %d.%d.%d.%d\n",
hostinfo->h_addr_list[0][0],
hostinfo->h_addr_list[0][1],
hostinfo->h_addr_list[0][2],
hostinfo->h_addr_list[0][3]
);
}
sin.sin_addr = *(IN_ADDR *) hostinfo->h_addr; /* l'adresse se trouve dans le champ h_addr de la structure hostinfo */
sin.sin_port = htons(80); /* on utilise htons pour le port */
sin.sin_family = AF_INET;
if(connect(sock,(SOCKADDR *) &sin, sizeof(SOCKADDR)) == SOCKET_ERROR)
{
perror("connect()");
fprintf (stdout, "Ping!\n");
exit(errno);
}
fprintf (stdout, "Ping!\n");
closesocket(sock);
}