Commit 09fff080 authored by Maxime POULAIN's avatar Maxime POULAIN
Browse files

push project

parent 95b15de6
#ifndef BOOL_H_
#define BOOL_H_
typedef enum {false, true}bool;
#endif
\ No newline at end of file
File added
#include "client.h"
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include "bool.h"
int printMenu(const char *ip) {
char choice;
printf("────────────────── %s ─────────────────\n", ip);
printf("1) Size\n");
printf("2) Sort\n");
printf("3) Exit\n");
scanf("%c%*c", &choice);
return choice - '0';
}
void loop(const char *ip, int port) {
int choice = 0;
char tmp[BUFFER_SIZE / 2] = {0};
char bufferIn[BUFFER_SIZE] = {0};
char bufferOut[BUFFER_SIZE] = {0};
int sock = 0;
struct sockaddr_in serv_addr;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
fprintf(stderr,"\n Socket creation error \n");
return;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
if (inet_pton(AF_INET, ip, &serv_addr.sin_addr) <= 0) {
fprintf(stderr, "Invalid address\n");
return;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
fprintf(stderr, "Connection Failed\n");
return;
}
do {
system("clear");
memset(bufferIn, 0, BUFFER_SIZE);
memset(bufferOut, 0, BUFFER_SIZE);
choice = printMenu(ip);
if(choice == 3){
sprintf(bufferOut,"exit");
send(sock, bufferOut, BUFFER_SIZE, 0);
printf("Disconnected !\n");
break;
}
fgets(tmp, BUFFER_SIZE / 2, stdin);
tmp[strlen(tmp) - 1] = '\0';
sprintf(bufferOut, "%d %s", choice, tmp);
send(sock, bufferOut, BUFFER_SIZE, 0);
recv(sock, bufferIn, BUFFER_SIZE, 0);
printf("Server : %s\n", bufferIn);
getc(stdin);
} while (true);
}
#ifndef CLIENT_H
#define CLIENT_H
#define BUFFER_SIZE 1024
int printMenu(const char * ip);
void loop(const char * ip, int port);
#endif
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include "client.h"
void usage(void) {
printf("usage : \n");
printf("\t client [ip] [port]\n");
}
int main(int argc, char const *argv[]) {
if (argc != 3) {
usage();
return -1;
}
loop(argv[1], atoi(argv[2]));
return 0;
}
#CC : le compilateur à utiliser
CC=gcc
#CFLAGS : les options de compilation
CFLAGS=-g
# les librairies à utiliser
LIBS=
#LDFLAGS : les options d'édition de lien
LDFLAGS=
#lieu où se trouve les sources :
SRC=.
#les fichiers objets
OBJ=$(SRC)/client.o\
$(SRC)/main.o
client : $(OBJ)
$(CC) $(LDFLAGS) $^ -o $@ $(LIBS)
$(SRC)%.o: $(SRC)%.c $(SRC)%.h
$(CC) $(CFLAGS) $(LDFLAGS) -c $< -o $@
clean:
rm client *.o 2>/dev/null
#ifndef BOOL_H_
#define BOOL_H_
typedef enum {false, true}bool;
#endif
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include "server.h"
void usage(void) {
printf("usage : \n");
printf("\t server [port]\n");
}
int main(int argc, char const *argv[])
{
if(argc != 2){
usage();
return -1;
}
start(atoi(argv[1]));
return 0;
}
#CC : le compilateur à utiliser
CC=gcc
#CFLAGS : les options de compilation
CFLAGS=-g
# les librairies à utiliser
LIBS=
#LDFLAGS : les options d'édition de lien
LDFLAGS=
#lieu où se trouve les sources :
SRC=.
#les fichiers objets
OBJ=$(SRC)/server.o\
$(SRC)/main.o
server : $(OBJ)
$(CC) $(LDFLAGS) $^ -o $@ $(LIBS)
$(SRC)%.o: $(SRC)%.c $(SRC)%.h
$(CC) $(CFLAGS) $(LDFLAGS) -c $< -o $@
clean:
rm server *.o 2>/dev/null
File added
#include "server.h"
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include "bool.h"
void start(int port) {
bool needToStop = false;
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char bufferIn[BUFFER_SIZE] = {0};
char bufferOut[BUFFER_SIZE] = {0};
strcpy(bufferOut, "coucou");
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(port);
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
printf("Listening on port %d\n", port);
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
while (!needToStop) {
memset(bufferIn, 0, BUFFER_SIZE);
memset(bufferOut, 0, BUFFER_SIZE);
recv(new_socket, bufferIn, BUFFER_SIZE, 0);
handleClientRequest(bufferIn, bufferOut);
printf("CLIENT : %s\n",bufferIn);
printf("SERVER : %s\n",bufferOut);
send(new_socket, bufferOut, BUFFER_SIZE, 0);
}
}
void handleClientRequest(char *bufferIn, char *bufferOut) {
switch (bufferIn[0]) {
case '1':
handleClientRequestSize(bufferIn, bufferOut);
break;
case '2':
handleClientRequestSort(bufferIn, bufferOut);
break;
}
}
void handleClientRequestSize(char *bufferIn, char *bufferOut) {
int len = strlen(bufferIn)-2;
sprintf(bufferOut, "%d", len);
}
void handleClientRequestSort(char *bufferIn, char *bufferOut) {
strcpy(bufferOut, bufferIn + 2);
sort(bufferOut);
}
void swap(char *a, char *b) {
char tmp = *a;
*a = *b;
*b = tmp;
}
void sort(char *word) {
int len = strlen(word);
for (unsigned int i = 0; i < len - 1; i++)
for (unsigned int j = i + 1; j < len; j++)
if (word[i] > word[j])
swap(&word[i], &word[j]);
}
#ifndef SERVER_H
#define SERVER_H
#define BUFFER_SIZE 1024
void start(int port);
void handleClientRequest(char *bufferIn, char *bufferOut);
void handleClientRequestSize(char * bufferIn, char * bufferOut);
void handleClientRequestSort(char * bufferIn, char * bufferOut);
void swap(char *a, char *b);
void sort(char *word);
#endif
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment