Skip to content

Commit

Permalink
creation du main + makefiles
Browse files Browse the repository at this point in the history
  • Loading branch information
ejalaa12 committed Jan 16, 2017
1 parent e6e5ae8 commit d1e86da
Show file tree
Hide file tree
Showing 23 changed files with 417 additions and 2 deletions.
Empty file modified .gitignore
100755 → 100644
Empty file.
Empty file modified README.md
100755 → 100644
Empty file.
95 changes: 95 additions & 0 deletions main/client.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#define BUFSIZE 512
#define MAXSERVERNAME 100
int my_work(FILE *, int, struct sockaddr * ,socklen_t);

int main( int C, char *argv[] )
{
int sd, ret;
struct sockaddr_in serveraddress;
if (NULL == argv[1])
{
printf("Please enter the IP Address of the server\n");
exit(1);
}
if (NULL== argv[2])
{
printf("Please enter the Port Number of the server\n");
exit(1);
}
sd = socket( AF_INET, SOCK_DGRAM, 0 );
if(0 > sd )
{
perror( "socket" );
exit (1);
}

memset( &serveraddress, 0, sizeof(serveraddress) );
serveraddress.sin_family = AF_INET;
serveraddress.sin_port = htons(atoi(argv[2]));//PORT NO
serveraddress.sin_addr.s_addr = inet_addr(argv[1]);//ADDRESS

printf("Client Starting service\n");

ret = my_work(stdin,sd ,(struct sockaddr *)&serveraddress,
sizeof(serveraddress));
if (0 > ret)
{
printf("Client Exiting - Some error occured\n");
close(sd);
exit(1);
}
close(sd);
exit(0);
}



int my_work(FILE *fp, /*Here to be used as stdin as argument*/
int sockfd ,
struct sockaddr *to ,socklen_t length) /*Connection Socket */
{

char sendbuf[BUFSIZE], recvbuf[BUFSIZE],servername[MAXSERVERNAME];
char *cptr;
int ret, numbytes, slen;
socklen_t structlen;
struct sockaddr_in serveraddr;


while(1){
printf("Enter Data For the server or press CTRL-D to exit\n");
/*Reading data from the keyboard*/
cptr = fgets(sendbuf,BUFSIZE,fp);
if (NULL == cptr)
{
printf("Possible error or end of file\n");
return 0;
}
slen = strlen (sendbuf);
/*Sending the read data over socket*/
ret = sendto(sockfd,sendbuf,slen,0,to,length);
if (0 > ret)
{
perror("Error in sending data:\n");
return -1;
}
printf("Data Sent To Server---------------------\n");
}




}

23 changes: 23 additions & 0 deletions main/encrypt_func.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void encrypt(char * message, int degree) {

int n = 0, rotateSwap = 0;
int i;

for(i=0; i<strlen(message); i++){
if(message[i] >='a' && message[i] <='z'){
n = 'z' - message[i];
if( degree > n ){
rotateSwap = degree - n - 1;
message[i] = 'a';
message[i] += rotateSwap;
}
else{
message[i] = message[i] + degree;
}
}
}
}
10 changes: 10 additions & 0 deletions main/encrypt_func.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef ENCRYPT_H_INCLUDED
#define ENCRYPT_H_INCLUDED

void encrypt(char * message, int degree);

#endif
86 changes: 86 additions & 0 deletions main/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <signal.h>
#include "encrypt_func.h"
#include "morse_func.h"

#define BUFSIZE 512
#define MYPORT 69
#define MAXNAME 100


int main(int argc, char *argv[] )

{
int sd, numbytes,bytessent, ret;
struct sockaddr_in
serveraddress,cliaddr;
socklen_t length;
char clientname[MAXNAME],datareceived[BUFSIZE];

sd = socket( AF_INET, SOCK_DGRAM, 0 );
if(0 > sd )
{
perror( "socket" );
exit( 1 );
}


memset( &serveraddress, 0, sizeof(serveraddress) );
memset( &cliaddr, 0, sizeof(cliaddr) );
serveraddress.sin_family = AF_INET;
serveraddress.sin_port = htons(MYPORT);//PORT NO
serveraddress.sin_addr.s_addr = htonl(INADDR_ANY);//IP ADDRESS
ret=bind(sd,(struct sockaddr*)&serveraddress,sizeof(serveraddress));
if(0 > ret)
{
perror("Bind Fails:");
exit(1);
}

printf("UDP Server: Waiting for client data\n");
length=sizeof(cliaddr);


while(1){
// Chaine de caractere en morse
char * msg_morse = (char *) malloc(sizeof(char)*BUFSIZE);
/*Received a datagram*/
numbytes = recvfrom(sd,datareceived,BUFSIZE,0,
(struct sockaddr*)&cliaddr, &length);
if (0 > numbytes)
{
perror("Error while receiving:");
exit(1);
}
/*Printing client's address*/
printf("Data Received from %s\n",
inet_ntop(AF_INET,&cliaddr.sin_addr,
clientname,sizeof(clientname)));

/*Sending the Received datagram back*/
datareceived[numbytes]='\0';
printf("Server Receives: %s\n",datareceived);

encrypt(datareceived, 3);
printf("Message encrypted: %s\n", datareceived);

ascii2morse(datareceived, msg_morse);
printf("MORSE message: %s\n", msg_morse);

free(msg_morse);

}


}


5 changes: 5 additions & 0 deletions main/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CC = ~/Documents/tp-armadeus/armadeus/buildroot/output/host/usr/bin/arm-linux-gcc

all: main.c encrypt_func.c morse_func.c
$(CC) -o main.o main.c encrypt_func.c morse_func.c -I.
scp main.o [email protected]:/home/tp/main.o
92 changes: 92 additions & 0 deletions main/morse_func.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include "morse_func.h"

int ascii2morse(char *s, char *outmsg){

char *letters[] = {
"1 -1 3 ", // A | a
"3 -1 1 -1 1 -1 1 ", // B | b
"3 -1 1 -1 3 -1 1 ", // C | c
"3 -1 1 -1 1 ", // D | d
"1 ", // E | e
"1 -1 1 -1 3 -1 1 ", // F | f
"3 -1 3 -1 1 ", // G | g
"1 -1 1 -1 1 -1 1 ", // H | h
"1 -1 1 -1 ", // I | i
"1 -1 3 -1 3 -1 3 ", // J | j
"3 -1 1 -1 3 ", // K | k
"1 -1 3 -1 1 -1 1 ", // L | l
"3 -1 3 ", // M | m
"3 -1 1 ", // N | n
"3 -1 3 -1 3 ", // O | o
"1 -1 3 -1 3 -1 1 ", // P | p
"3 -1 3 -1 1 -1 3 ", // Q | q
"1 -1 3 -1 1 ", // R | r
"1 -1 1 -1 1 ", // S | s
"3 ", // T | t
"1 -1 1 -1 3 ", // U | u
"1 -1 1 -1 1 -1 3 ", // V | v
"1 -1 3 -1 3 ", // W | w
"3 -1 1 -1 1 -1 3 ", // X | x
"3 -1 1 -1 3 -1 3 ", // Y | y
"3 -1 3 -1 1 -1 1 " // Z | z
};

char *numbers[] = {
"3 -1 3 -1 3 -1 3 -1 3 ", // 0
"1 -1 3 -1 3 -1 3 -1 3 ", // 1
"1 -1 1 -1 3 -1 3 -1 3 ", // 2
"1 -1 1 -1 1 -1 3 -1 3 ", // 3
"1 -1 1 -1 1 -1 1 -1 3 ", // 4
"1 -1 1 -1 1 -1 1 -1 1 ", // 5
"3 -1 1 -1 1 -1 1 -1 1 ", // 6
"3 -1 3 -1 1 -1 1 -1 1 ", // 7
"3 -1 3 -1 3 -1 1 -1 1 ", // 8
"3 -1 3 -1 3 -1 3 -1 3 ", // 9
};

int i, c;
for (i=0;i<strlen(s);i++){

c = s[i];
if (c != ' '){
// selectionner le caractere
if((c <= 'z') && (c >= 'a'))
strcat(outmsg, letters[c - 'a']);

else if((c <= 'Z') && (c >= 'A'))
strcat(outmsg, letters[c - 'A']);

else if((c <= '9') && (c >= '0'))
strcat(outmsg, numbers[c - '0']);

else if(c == '.')
strcat(outmsg, "1 -1 3 -1 1 -1 3 -1 1 -1 3 ");

else if(c == ',')
strcat(outmsg, "3 -1 3 -1 1 -1 1 -1 3 -1 3 ");

else if(c == '?')
strcat(outmsg, "1 -1 1 -1 3 -1 3 -1 1 -1 1 ");

else if(c == '-')
strcat(outmsg, "3 -1 1 -1 1 -1 1 -1 1 -1 3 ");

else // caractere non valide
printf("Le caracter %c n'est pas valide \nValeur en ASCII = %d\ni = %d",s[i],s[i],i);

// espace entre elements
if (s[i+2] == '\0')
break;
if (s[i+1] != ' ')
strcat(outmsg, "-3 "); // entre elements
else{
strcat(outmsg, "-7 "); // fin d'un mot
i++;
}
}
}

//fclose(f);

return EXIT_SUCCESS;
}
10 changes: 10 additions & 0 deletions main/morse_func.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED

int ascii2morse(char *s, char *outmsg);

#endif
Loading

0 comments on commit d1e86da

Please sign in to comment.