Archived
3
0

first steps for the development of remote: connection on TCP done

This commit is contained in:
lapupe 2016-09-16 00:09:04 +02:00
parent 34ced4437a
commit 64e1e15b6e
5 changed files with 141 additions and 0 deletions

28
remote/Makefile Normal file
View File

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

37
remote/tcpdclient.c Normal file
View File

@ -0,0 +1,37 @@
#include "tcpdserver.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#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;
}

BIN
remote/tcpdserver Executable file

Binary file not shown.

67
remote/tcpdserver.c Normal file
View File

@ -0,0 +1,67 @@
#include "tcpdserver.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#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;
}

9
remote/tcpdserver.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef __TCPDSERVER_H__
#define __TCPDSERVER_H__
int initConnection ();
void endConnection (int sock, int csock);
#endif