Last active
March 22, 2017 23:05
-
-
Save elarif/d988fa313268ad5249b71a93740fa984 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defaut : server | |
#CFLAGS = -Wall -DDEBUG | |
#CFLAGS = -Wall | |
server : server.o | |
cc -o server server.o | |
server.o : server.c | |
cc -c server.c | |
clean : | |
rm -f server client *.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <signal.h> | |
#include <assert.h> | |
void error(const char *msg) { | |
perror(msg); | |
exit(1); | |
} | |
int readPortNumber(int argc, char *argv[]) { | |
if (argc < 2) | |
error("ERROR, no port provided\n"); | |
int result = atoi(argv[1]); | |
return result; | |
} | |
int openSocket() { | |
int result = socket(AF_INET, SOCK_STREAM, 0); | |
if (result < 0) | |
error("ERROR opening socket"); | |
return result; | |
} | |
struct sockaddr_in initServAddr(int port) { | |
struct sockaddr_in result; | |
bzero((char *) &result, sizeof(result)); | |
result.sin_family = AF_INET; | |
result.sin_addr.s_addr = INADDR_ANY; | |
result.sin_port = htons(port); | |
return result; | |
} | |
void bindSocket(int sockfd, struct sockaddr_in serv_addr) { | |
if (bind(sockfd, (struct sockaddr *) &serv_addr, | |
sizeof(serv_addr)) < 0) | |
error("ERROR on binding"); | |
listen(sockfd,5); | |
} | |
int acceptConnection(int sockfd, struct sockaddr_in cli_addr) { | |
socklen_t clilen = sizeof(cli_addr); | |
int result = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); | |
if (result < 0) | |
error("ERROR on accept"); | |
return result; | |
} | |
int openAndListenOnPort(int port) { | |
int result = openSocket(port); | |
struct sockaddr_in serv_addr = initServAddr(port); | |
bindSocket(result, serv_addr); | |
return result; | |
} | |
void dostuff (int sock) { | |
int n; | |
char buffer[256]; | |
bzero(buffer,256); | |
n = read(sock,buffer,255); | |
if (n < 0) error("ERROR reading from socket"); | |
printf("Here is the message: %s\n",buffer); | |
n = write(sock,"I got your message",18); | |
if (n < 0) error("ERROR writing to socket"); | |
} | |
void waitForConnections(int sockfd) { | |
struct sockaddr_in cli_addr; | |
while (1) { | |
int newsockfd = acceptConnection(sockfd, cli_addr); | |
int pid = fork(); | |
if (pid < 0) | |
error("ERROR on fork"); | |
if (pid == 0) { | |
close(sockfd); | |
dostuff(newsockfd); | |
exit(0); | |
} | |
else close(newsockfd); | |
} | |
} | |
server(int argc, char** argv){ | |
if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) { | |
perror(0); | |
exit(1); | |
} | |
int port = readPortNumber(argc, argv); | |
int sockfd = openAndListenOnPort(port); | |
waitForConnections(sockfd); | |
} | |
struct Reveil{ | |
char * nom; | |
char *hour; | |
}; | |
char** str_split(char* a_str, const char a_delim) | |
{ | |
char** result = 0; | |
size_t count = 0; | |
char* tmp = a_str; | |
char* last_comma = 0; | |
char delim[2]; | |
delim[0] = a_delim; | |
delim[1] = 0; | |
while (*tmp) | |
{ | |
if (a_delim == *tmp) | |
{ | |
count++; | |
last_comma = tmp; | |
} | |
tmp++; | |
} | |
count += last_comma < (a_str + strlen(a_str) - 1); | |
count++; | |
result = malloc(sizeof(char*) * count); | |
if (result) | |
{ | |
size_t idx = 0; | |
char* token = strtok(a_str, delim); | |
while (token) | |
{ | |
assert(idx < count); | |
*(result + idx++) = strdup(token); | |
token = strtok(0, delim); | |
} | |
assert(idx == count - 1); | |
*(result + idx) = 0; | |
} | |
return result; | |
} | |
char* serialisation(struct Reveil reveil) { | |
char *result = malloc (sizeof (char) * (strlen(reveil.nom) + strlen(reveil.hour) + 5)); | |
strcat(result,"1-"); | |
strcat(result,reveil.nom); | |
strcat(result,"-"); | |
strcat(result,reveil.hour); | |
return result; | |
} | |
struct Reveil deserialisation(char* reveil) { | |
struct Reveil result; | |
char** tokens; | |
tokens = str_split(reveil, '-'); | |
result.nom = tokens[1]; | |
result.hour = tokens[2]; | |
return result; | |
} | |
int main(int argc, char** argv) { | |
char test[] = "1-toto-8h53"; | |
char* sd = malloc (sizeof (char) * strlen("1-toto-8h53")); | |
strcpy(sd, serialisation(deserialisation(test))); | |
assert(strcmp("1-toto-8h53", sd) == 0); | |
struct Reveil reveil; | |
reveil.nom = "toto"; | |
reveil.hour ="8:3"; | |
struct Reveil ds = deserialisation(serialisation(reveil)); | |
assert(strcmp(reveil.nom, ds.nom) == 0); | |
assert(strcmp(reveil.hour, ds.hour) == 0); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment