-
Notifications
You must be signed in to change notification settings - Fork 0
/
sc2.c
48 lines (42 loc) · 1.09 KB
/
sc2.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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/*#define PORT 80*/
#define PORT 1025
/*#define IP "216.58.193.4" // google.com*/
#define IP "127.0.0.1"
int main() {
int fd;
// 1. Crear socket TCP
fd = socket(AF_INET, SOCK_STREAM, 0);
// 2. Establecer conexion
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
inet_pton(AF_INET, IP, &addr.sin_addr);
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
perror("Unable to connect\n");
close(fd);
exit(1);
}
// 3. Comunicacion
/*char *str = "GET / HTTP/1.1\r\n\r\n";*/
char *str = "hola 2";
write(fd, str, strlen(str));
printf("%s sent\n", str);
int tam = strlen(str);
char *buffer = malloc(tam*sizeof(char));
read(fd, buffer, tam);
printf("%s received\n", buffer);
str = "OK";
write(fd, str, strlen(str));
free(buffer);
// 4. Cerrar conexion
close(fd);
return 0;
}