Created
December 8, 2013 14:30
-
-
Save maxisoft/7858292 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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <errno.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netdb.h> | |
#include <limits.h> | |
#include <string.h> | |
#include <math.h> | |
#include <sys/stat.h> | |
//CLIENT | |
#define PORT 5215 | |
#define CHUNK 4096 | |
FILE *infile = NULL; | |
typedef int SOCKET; | |
SOCKET sock = -1; | |
char hash(char *data, size_t len) { | |
char ret = 0xC1; | |
int i; | |
for (i = 0; i < len; ++i) { | |
ret ^= data[i]; | |
} | |
return ret; | |
} | |
int main(int argc, char** argv) { | |
sock = socket(AF_INET, SOCK_STREAM, 0); | |
if (sock == -1) { | |
perror("socket ceation"); | |
exit(errno); | |
} | |
int flag = 1; | |
//setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof (int)); | |
char *filenamebuff = calloc(PATH_MAX + sizeof(size_t), sizeof (char)); | |
int filenamebufflen = snprintf(filenamebuff, PATH_MAX, "%s", "toto"); | |
if (filenamebufflen < 0) { | |
exit(EXIT_FAILURE); | |
} | |
infile = fopen(filenamebuff, "r"); | |
if (!infile) { | |
perror("fichier local"); | |
exit(errno); | |
} | |
memset(filenamebuff, 0, filenamebufflen); //remise a zero du buffer | |
filenamebufflen = snprintf(filenamebuff, PATH_MAX, "%s", "toto.out"); | |
if (filenamebufflen < 0) { | |
exit(EXIT_FAILURE); | |
} | |
#ifdef __linux__ | |
struct stat st; | |
stat(filenamebuff, &st); | |
const size_t size = st.st_size; | |
#else | |
fseek(infile, 0, SEEK_END); // seek to end of file | |
const size_t size = ftell(infile); // get current file pointer | |
fseek(infile, 0, SEEK_SET); // seek back to beginning of file | |
#endif | |
memmove(filenamebuff+sizeof(size_t), filenamebuff, filenamebufflen); | |
((size_t *)filenamebuff)[0] = size; | |
struct hostent *hostinfo = NULL; | |
struct sockaddr_in sin = {0}; | |
const char *hostname = "127.0.0.1"; | |
hostinfo = gethostbyname(hostname); | |
if (!hostinfo) /* l'hôte n'existe pas */ { | |
fprintf(stderr, "Unknown host %s.\n", hostname); | |
exit(EXIT_FAILURE); | |
} | |
if (!hostinfo->h_length) { | |
fprintf(stderr, "Unknown host %s. (host_length is empty)\n", hostname); | |
exit(EXIT_FAILURE); | |
} | |
sin.sin_addr = *(struct in_addr *) hostinfo->h_addr_list[0]; | |
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); | |
} | |
if (send(sock, filenamebuff, filenamebufflen + sizeof(size_t), 0) < 0) { | |
perror("send filename"); | |
exit(errno); | |
} | |
free(filenamebuff); | |
char buff[CHUNK]; | |
int read_len; | |
size_t total_len = 0; | |
while (!feof(infile)) { | |
printf("HAHA\n"); | |
read_len = fread(buff, sizeof (char), CHUNK, infile); // lance la lecture du fichier par morceaux | |
total_len += read_len; | |
if (ferror(infile)) { | |
fputs("Erreur lors de la lecture du fichier source\n", stderr); | |
} | |
if (send(sock, buff, read_len, 0) < 0) { | |
perror("send file content"); | |
exit(errno); | |
} | |
} | |
printf("%i\n", size); | |
close(sock); | |
fcloseall(); | |
return (EXIT_SUCCESS); | |
} | |
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 <errno.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#include <arpa/inet.h> | |
#include <unistd.h> | |
#include <netdb.h> /* gethostbyname */ | |
#include <limits.h> | |
#include <string.h> | |
//SERVER | |
#define PORT 5215 | |
#define CHUNK 4096 | |
FILE *outfile = NULL; | |
typedef int SOCKET; | |
SOCKET sock = -1; | |
int main(int argc, char** argv) { | |
sock = socket(AF_INET, SOCK_STREAM, 0); | |
if (sock == -1) { | |
perror("socket ceation"); | |
exit(errno); | |
} | |
struct sockaddr_in sin = {0}; | |
sin.sin_addr.s_addr = htonl(INADDR_ANY); //nimporte quelle addresse. | |
sin.sin_family = AF_INET; | |
sin.sin_port = htons(PORT); | |
if (bind(sock, (struct sockaddr *) & sin, sizeof sin) == -1) { | |
perror("bind"); | |
exit(errno); | |
} | |
if (listen(sock, 1) == -1) { | |
perror("listen"); | |
exit(errno); | |
} | |
SOCKET csock; | |
struct sockaddr_in csin = {0}; | |
size_t size; | |
csock = accept(sock, (struct sockaddr *) & csin, &size); | |
if (csock == -1) { | |
perror("accept"); | |
exit(errno); | |
} | |
//timeout - par ce que c'est pas bien d'attendre indefiniment . | |
struct timeval tv; | |
tv.tv_sec = 30; /* 30 Secs Timeout */ | |
tv.tv_usec = 0; | |
setsockopt(csock, SOL_SOCKET, SO_RCVTIMEO, (char *) &tv, sizeof (struct timeval)); | |
char *filenamebuff = calloc(PATH_MAX + sizeof(size_t), sizeof (char)); | |
int recv_len = 0; | |
if ((recv_len = recv(csock, filenamebuff, PATH_MAX + sizeof(size_t), 0)) < 0) { | |
perror("recv filename"); | |
exit(errno); | |
} | |
size = ((size_t *)filenamebuff)[0]; | |
printf("%i\n", size); | |
printf("%s\n", filenamebuff + sizeof(size_t)); | |
outfile = fopen(filenamebuff + sizeof(size_t), "w"); | |
free(filenamebuff); | |
filenamebuff = NULL; | |
if (!outfile) { | |
perror("fichier a ecrire"); | |
exit(errno); | |
} | |
char buff[CHUNK]; | |
const size_t lim = CHUNK - 1; | |
size_t total_len = 0; | |
//si l'on recoit un packet et que la taille des data est nulle, alors on a finit de lire le fichier. | |
while ((recv_len = recv(csock, buff, CHUNK, 0)) > 0) { // temps que l'on recoit des data. | |
printf("recv_len %i\n", recv_len); | |
fwrite(buff, sizeof (char), recv_len, outfile); | |
if (ferror(outfile)) { | |
fputs("Erreur lors de l'écriture dans le fichier de sortie\n", stderr); | |
} | |
total_len += recv_len; | |
if (recv_len != CHUNK){ // fin du fichier | |
break; | |
} | |
} | |
if (recv_len < 0) { | |
perror("recv file content"); | |
exit(errno); | |
} | |
printf("%i\n", total_len); | |
fcloseall(); | |
close(csock); | |
close(sock); | |
return (EXIT_SUCCESS); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment