-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.c
More file actions
67 lines (56 loc) · 1.63 KB
/
socket.c
File metadata and controls
67 lines (56 loc) · 1.63 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
// I/O
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//
/*
* Functions:
* getaddrinfo,socket,bind,connect
* Structs:
* addrinfo,sockaddr_in(6),bind
*
*/
#include<netdb.h>
#include<sys/types.h>
#include<sys/socket.h>
// Addictional
#include<errno.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<netinet/in.h>
//
#define MYPORT "3490"
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_RESET "\x1b[0m"
void ERROR(int out){
printf(ANSI_COLOR_RED "ERROR(): %s\n\n" ANSI_COLOR_RESET,strerror(errno));
exit(out);
}
int main(int argc,char**argv){
printf("\n");
struct addrinfo*relay,hints;
memset(&hints,0,sizeof(hints));
hints.ai_family=AF_UNSPEC;
hints.ai_socktype=SOCK_STREAM;
hints.ai_flags=AI_PASSIVE;
for(size_t i=0;i<argc-1;++i){
printf(ANSI_COLOR_YELLOW "Try connexion with %s\n" ANSI_COLOR_RESET,argv[i+1]);
printf("getaddrinfo(): ");
if(getaddrinfo(argv[i+1],MYPORT,&hints,&relay))ERROR(-1);
printf(ANSI_COLOR_GREEN "Ok\nEstabilish the connexion...\n" ANSI_COLOR_RESET);
printf("socket(): ");
int sock=socket(relay->ai_family,relay->ai_socktype,0);
if(sock==-1)ERROR(-1);
printf(ANSI_COLOR_GREEN "Ok\n" ANSI_COLOR_RESET);
printf("connect(): ");
relay->ai_addr->sa_family=0;//For some reason the value defualt is brojen ¯\_(ツ)_/¯¯
if(connect(sock,relay->ai_addr,relay->ai_addrlen)==-1)ERROR(-1);
printf(ANSI_COLOR_GREEN "Ok\n");
printf(ANSI_COLOR_MAGENTA "Yeap, you are connect now!\n\n" ANSI_COLOR_RESET);
}
return 0;
}