From 26f1e48b192951c1724d93d159007f498befb32c Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Wed, 24 Jun 2020 14:00:23 +0200 Subject: [PATCH] Renamed TCP files --- TCP.c | 37 +++++++++++++++++++++++++++++++++++++ TCP.h | 9 +++++++++ 2 files changed, 46 insertions(+) create mode 100644 TCP.c create mode 100644 TCP.h diff --git a/TCP.c b/TCP.c new file mode 100644 index 0000000..5ca898e --- /dev/null +++ b/TCP.c @@ -0,0 +1,37 @@ +#include "TCP.h" + +int open_tcp(char* ip, int port) { + int sockfd = socket(AF_INET, SOCK_STREAM, 0); + + if (sockfd < 0) { + perror("Could not open AF_INET socket"); + exit(1); + } + + struct hostent *server; + struct sockaddr_in serv_addr; + + server = gethostbyname(ip); + + if (server == NULL) { + perror("Error resolving host"); + exit(1); + } + + bzero((char *) &serv_addr, sizeof(serv_addr)); + serv_addr.sin_family = AF_INET; + + bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length); + serv_addr.sin_port = htons(port); + + if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) { + perror("Could not connect TCP socket"); + exit(1); + } + + return sockfd; +} + +int close_tcp(int fd) { + return close(fd); +} \ No newline at end of file diff --git a/TCP.h b/TCP.h new file mode 100644 index 0000000..d386b59 --- /dev/null +++ b/TCP.h @@ -0,0 +1,9 @@ +#include +#include +#include +#include +#include +#include + +int open_tcp(char* ip, int port); +int close_tcp(int fd); \ No newline at end of file