tncattach/TCP.c

37 lines
803 B
C
Raw Normal View History

2020-06-24 06:00:23 -06:00
#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;
2020-06-24 06:44:30 -06:00
server = gethostbyname(ip);
2020-06-24 06:00:23 -06:00
if (server == NULL) {
perror("Error resolving host");
exit(1);
}
2020-06-24 06:28:50 -06:00
memset(&serv_addr, 0, sizeof(serv_addr));
2020-06-24 06:00:23 -06:00
serv_addr.sin_family = AF_INET;
2020-06-24 06:28:50 -06:00
memcpy(server->h_addr, &serv_addr.sin_addr.s_addr, server->h_length);
2020-06-24 06:00:23 -06:00
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) {
2020-06-24 06:22:42 -06:00
return close(fd);
2020-06-24 06:00:23 -06:00
}