diff --git a/remote/Makefile b/remote/Makefile new file mode 100644 index 0000000..a873a06 --- /dev/null +++ b/remote/Makefile @@ -0,0 +1,28 @@ +CC=gcc +CFLAGS=-Wall -g +LDFLAGS= -pthread +CFILES=$(wildcard *.c) # CFILES => recompiles everything on a C file change +EXEC=$(basename $(wildcard *.c)) +SOURCES=$(wildcard ../lib/*.c) +OBJECTS=$(SOURCES:.c=.o) +TESTS=$(addsuffix .test, $(EXEC)) + +all: tcpdserver tcpdclient + +$(EXEC): $(OBJECTS) $(CFILES) + $(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -o $@ + +tcpdserver: tcpdserver.c tcpdserver.h + $(CC) $(CFLAGS) $@.c -o $@ + +tcpdclient: tcpdclient.c tcpdserver.h + $(CC) $(CFLAGS) $@.c -o $@ + +.c.o: + $(CC) -c $(CFLAGS) $< -o $@ + +clean: + -rm $(OBJECTS) + +mrproper: clean + rm $(EXEC) diff --git a/remote/tcpdclient.c b/remote/tcpdclient.c new file mode 100644 index 0000000..8187a67 --- /dev/null +++ b/remote/tcpdclient.c @@ -0,0 +1,37 @@ +#include "tcpdserver.h" +#include +#include +#include +#include +#include +#include +#include +#include + + +#define PORT 6000 + + +int main(int argc, char ** argv) { + int sock = socket(AF_INET, SOCK_STREAM, 0); + if(sock == -1) + { + perror("socket()"); + exit(errno); + } + + struct sockaddr_in sin = {0}; + sin.sin_addr.s_addr = inet_addr("127.0.0.1"); + sin.sin_port = htons(PORT); /* on utilise htons pour le port */ + sin.sin_family = AF_INET; + + if(connect(sock,(struct sockaddr *) &sin, sizeof(struct sockaddr)) == -1) + { + perror("connect()"); + exit(errno); + } + + close(sock); + + return 0; +} \ No newline at end of file diff --git a/remote/tcpdserver b/remote/tcpdserver new file mode 100755 index 0000000..4ad916a Binary files /dev/null and b/remote/tcpdserver differ diff --git a/remote/tcpdserver.c b/remote/tcpdserver.c new file mode 100644 index 0000000..87caf7d --- /dev/null +++ b/remote/tcpdserver.c @@ -0,0 +1,67 @@ +#include "tcpdserver.h" +#include +#include +#include +#include +#include +#include +#include +#include + +#define PORT 6000 + +int init_connection(void) +{ + int sock = socket(AF_INET, SOCK_STREAM, 0); + struct sockaddr_in sin = { 0 }; + + if(sock == -1) + { + perror("socket()"); + exit(errno); + } + + sin.sin_addr.s_addr = htonl(INADDR_ANY); + sin.sin_port = htons(PORT); + sin.sin_family = AF_INET; + + if(bind(sock,(struct sockaddr *) &sin, sizeof sin) == -1) + { + perror("bind()"); + exit(errno); + } + + if(listen(sock, 5) == -1) + { + perror("listen()"); + exit(errno); + } + + return sock; +} + + +// void printClientAddr(struct sockaddr_in &csin) { +// printf("%s\n", ); +// } + + +int main(int argc, char * argv[], char **env) { + + int sock = init_connection(); + + struct sockaddr_in csin = { 0 }; + socklen_t sinsize = sizeof csin; + int csock = accept(sock, (struct sockaddr *)&csin, &sinsize); + if(csock == -1) + { + perror("accept()"); + exit(errno); + } + + printf("new client\n"); + + close(sock); + close(csock); + return 0; +} \ No newline at end of file diff --git a/remote/tcpdserver.h b/remote/tcpdserver.h new file mode 100644 index 0000000..d5d4b92 --- /dev/null +++ b/remote/tcpdserver.h @@ -0,0 +1,9 @@ +#ifndef __TCPDSERVER_H__ +#define __TCPDSERVER_H__ + + +int initConnection (); +void endConnection (int sock, int csock); + + +#endif \ No newline at end of file