-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_socket.c
146 lines (114 loc) · 3.48 KB
/
common_socket.c
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
146
#define _POSIX_C_SOURCE 200112L
#include "common_socket.h"
#include <stdio.h>
#define ERROR -1
#define SUCCESS 0
#define SOCKET_CERRADO 0
int socket_inicializar(socket_t* skt, const char* ip, const char* puerto,
int flag) {
memset(skt, 0, sizeof(socket_t));
skt->hints.ai_family = AF_INET;
skt->hints.ai_socktype = SOCK_STREAM;
skt->hints.ai_flags = flag;
int estado = getaddrinfo(ip, puerto, &(skt->hints), &(skt->resultado));
if (estado != SUCCESS) {
freeaddrinfo(skt->resultado);
return ERROR;
}
return SUCCESS;
}
int socket_conectar(socket_t* skt) {
bool esta_conectado = false;
struct addrinfo* resultado_aux = skt->resultado;
while (resultado_aux && !esta_conectado) {
skt->file_descriptor = socket(resultado_aux->ai_family,
resultado_aux->ai_socktype,
resultado_aux->ai_protocol);
if (skt->file_descriptor != ERROR) {
if (connect(skt->file_descriptor, resultado_aux->ai_addr,
resultado_aux->ai_addrlen) == SUCCESS) {
esta_conectado = true;
} else {
close(skt->file_descriptor);
}
}
resultado_aux = resultado_aux->ai_next;
}
if (!esta_conectado) return ERROR;
return SUCCESS;
}
int socket_enlazar(socket_t* skt) {
bool esta_conectado = false;
struct addrinfo* resultado_aux = skt->resultado;
while (resultado_aux && !esta_conectado) {
skt->file_descriptor = socket(resultado_aux->ai_family,
resultado_aux->ai_socktype,
resultado_aux->ai_protocol);
int opcion = 1;
setsockopt(skt->file_descriptor, SOL_SOCKET, SO_REUSEADDR,
&opcion, sizeof(opcion));
if (skt->file_descriptor != ERROR) {
if (bind(skt->file_descriptor, resultado_aux->ai_addr,
resultado_aux->ai_addrlen) == SUCCESS) {
esta_conectado = true;
} else {
close(skt->file_descriptor);
}
}
resultado_aux = resultado_aux->ai_next;
}
if (!esta_conectado) return ERROR;
return SUCCESS;
}
int socket_conectar_con_cliente(socket_t* skt) {
if (listen(skt->file_descriptor, 1) == SUCCESS){
skt->peer = accept(skt->file_descriptor, NULL, NULL);
if (skt->peer == ERROR) {
perror("Error al conectar con cliente");
return ERROR;
}
}
return SUCCESS;
}
int socket_recibir(socket_t* skt, unsigned char buffer[TAM_BUFFER],
size_t bytes_a_recibir) {
int total_bytes_recibidos = 0;
bool socket_abierto = true;
while (total_bytes_recibidos < bytes_a_recibir && socket_abierto){
int bytes_recibidos = recv(skt->peer, &buffer[total_bytes_recibidos],
bytes_a_recibir-total_bytes_recibidos, 0);
if (bytes_recibidos == ERROR) {
socket_abierto = false;
} else if (bytes_recibidos == SOCKET_CERRADO) {
socket_abierto = false;
} else {
total_bytes_recibidos += bytes_recibidos;
}
}
return total_bytes_recibidos;
}
int socket_enviar(socket_t* skt, unsigned char mensaje[TAM_BUFFER],
size_t bytes_a_enviar){
int total_bytes_enviados = 0;
bool socket_abierto = true;
while (total_bytes_enviados < bytes_a_enviar && socket_abierto) {
int bytes_enviados = send(skt->file_descriptor,
&mensaje[total_bytes_enviados],
bytes_a_enviar-total_bytes_enviados,
MSG_NOSIGNAL);
if (bytes_enviados == ERROR) {
socket_abierto = false;
} else if (bytes_enviados == SOCKET_CERRADO) {
socket_abierto = false;
} else {
total_bytes_enviados += bytes_enviados;
}
}
return total_bytes_enviados;
}
int socket_destruir(socket_t* skt) {
if (skt->peer) close(skt->peer);
if (skt->file_descriptor) close(skt->file_descriptor);
freeaddrinfo(skt->resultado);
return SUCCESS;
}