commit a1250cb3345cd3b4bbec10e1cbe461b1843f9a25 Author: Santiago Lo Coco Date: Fri Nov 5 18:26:05 2021 -0300 Add client (working with given server) Co-authored-by: Ezequiel Bellver Co-authored-by: Juan Barmasch diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..aa45b88 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "files.associations": { + "error.h": "c", + "client.h": "c", + "errors.h": "c" + } +} \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c26f792 --- /dev/null +++ b/Makefile @@ -0,0 +1,24 @@ +CC = gcc +CCFLAGS = -Wall -std=c99 -pedantic -g + +OBJECTS = server.o errors.o client.o + +all: $(OBJECTS) + +server.o: server.c include/errors.h + $(CC) $(CCFLAGS) -c server.c +errors.o: errors.c include/errors.h + $(CC) $(CCFLAGS) -c errors.c +client.o: client.c errors.o + $(CC) $(CCFLAGS) client.c -o client.o errors.o + +clean: + rm -rf $(OBJECTS) + +test: + pvs-studio-analyzer trace -- make + pvs-studio-analyzer analyze + plog-converter -a '64:1,2,3;GA:1,2,3;OP:1,2,3' -t tasklist -o report.tasks PVS-Studio.log + cppcheck --quiet --enable=all --force --inconclusive . + +.PHONY: all clean test \ No newline at end of file diff --git a/client.c b/client.c new file mode 100644 index 0000000..187fb49 --- /dev/null +++ b/client.c @@ -0,0 +1,34 @@ +#include "include/client.h" + +int main(int argc, char *argv[]) { + int fd = 0; + struct sockaddr_in serv_addr; + + if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + printSystemError("User: socket()"); + } + + serv_addr.sin_family = AF_INET; + serv_addr.sin_port = htons(PORT); + serv_addr.sin_addr.s_addr = inet_addr(ADDRESS); + + if (connect(fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { + printSystemError("User: connect()"); + } + + char input[MAX_LEN] = {0}; + int readChars; + while ((readChars = read(STDIN_FILENO, input, MAX_LEN - 1)) != 0) { + if (readChars < 0) + printSystemError("User: read()"); + input[readChars] = '\0'; + + if (write(fd, input, readChars) < 0) { + printSystemError("User: write()"); + } + } + + close(fd); + + return EXIT_SUCCESS; +} diff --git a/client.o b/client.o new file mode 100755 index 0000000..85bf82b Binary files /dev/null and b/client.o differ diff --git a/errors.c b/errors.c new file mode 100644 index 0000000..8752803 --- /dev/null +++ b/errors.c @@ -0,0 +1,12 @@ +#include "include/errors.h" + +void printError(char * string) { + fprintf(stderr, "%s", string); + fprintf(stderr, "\n"); + exit(EXIT_FAILURE); +} + +void printSystemError(char * string) { + perror(string); + exit(EXIT_FAILURE); +} \ No newline at end of file diff --git a/errors.o b/errors.o new file mode 100644 index 0000000..a398c99 Binary files /dev/null and b/errors.o differ diff --git a/include/client.h b/include/client.h new file mode 100644 index 0000000..3b2505f --- /dev/null +++ b/include/client.h @@ -0,0 +1,17 @@ +#ifndef CLIENT_H +#define CLIENT_H + +#include +#include +#include +#include +#include +#include +#include "errors.h" + +#define MAX_ARGS 3 +#define MAX_LEN 100 +#define PORT 8080 +#define ADDRESS "127.0.0.1" + +#endif \ No newline at end of file diff --git a/include/errors.h b/include/errors.h new file mode 100644 index 0000000..7c44512 --- /dev/null +++ b/include/errors.h @@ -0,0 +1,10 @@ +#ifndef ERRORS_H +#define ERRORS_H + +#include +#include + +void printError(char * string); +void printSystemError(char * string); + +#endif \ No newline at end of file diff --git a/server.c b/server.c new file mode 100644 index 0000000..e69de29 diff --git a/server.o b/server.o new file mode 100644 index 0000000..3282402 Binary files /dev/null and b/server.o differ