59 lines
1.5 KiB
C
59 lines
1.5 KiB
C
#include <errno.h>
|
|
#include "ctools/libft/printf/ft_printf.h"
|
|
#include "ctools/printf/ft_printf.h"
|
|
#include <stddef.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/types.h>
|
|
#include <netdb.h>
|
|
|
|
int send_socket_message(int sfd, const char *msg) {
|
|
ssize_t len = ft_strlen(msg);
|
|
if (send(sfd, msg, len, 0) != len) {
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
if (argc < 3) {
|
|
ft_fprintf(STDERR_FILENO, "[ERROR] Invalid argument count: %d\n", argc);
|
|
ft_fprintf(STDERR_FILENO, "Usage: %s <ADDR> <PORT>\n", argv[0]);
|
|
return 1;
|
|
}
|
|
|
|
|
|
struct addrinfo *rp;
|
|
struct addrinfo hints = {0};
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
hints.ai_flags = 0;
|
|
hints.ai_protocol = 0;
|
|
|
|
int info = getaddrinfo(argv[1], argv[2], &hints, &rp);
|
|
if (info == -1) {
|
|
ft_fprintf(STDERR_FILENO, "[ERROR] Addr info: %s\n", strerror(errno));
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
int sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
|
|
|
if (connect(sock, rp->ai_addr, rp->ai_addrlen) != 0) {
|
|
close(sock);
|
|
ft_fprintf(STDERR_FILENO, "[ERROR] connect: %s\n", strerror(errno));
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
freeaddrinfo(rp);
|
|
while (1) {
|
|
if (!send_socket_message(sock, "Hello\n")) {
|
|
ft_fprintf(STDERR_FILENO, "send: %d\n", strerror(errno));
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
}
|