Renamed TCP files

This commit is contained in:
Mark Qvist 2020-06-24 14:00:23 +02:00
parent 5c8ddcd992
commit 26f1e48b19
2 changed files with 46 additions and 0 deletions

37
TCP.c Normal file
View File

@ -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);
}

9
TCP.h Normal file
View File

@ -0,0 +1,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
int open_tcp(char* ip, int port);
int close_tcp(int fd);