Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdint.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/socket.h>
- #include <sys/sendfile.h>
- struct SendInfo {
- const char *filename;
- char *filebuffer;
- };
- static void onexit(int a, int b, int c, int d) {
- exit(0);
- }
- static void * sender (void *arg) {
- int newsockd = *(int *)arg;
- int sockd;
- int fpl, fd;
- struct stat fileStat;
- uint32_t fsize, size_to_send;
- int rc;
- char buffer[512];
- off_t offset;
- if((fpl = open("/tmp/input.txt", O_RDONLY)) < 0){
- perror("open file with open");
- onexit(newsockd, sockd, 0, 2);
- }
- if(fstat(fpl, &fileStat) < 0){
- perror("Errore fstat");
- onexit(newsockd, sockd, fpl, 3);
- }
- fsize = fileStat.st_size;
- if(send(newsockd, &fsize, sizeof(fsize), 0) < 0){
- perror("Errore durante l'invio grandezza file list");
- onexit(newsockd, sockd, fpl, 3);
- }
- fd = fpl;
- offset = 0;
- for (size_to_send = fsize; size_to_send > 0; ){
- rc = sendfile(newsockd, fd, &offset, size_to_send);
- if (rc <= 0){
- perror("sendfile");
- onexit(newsockd, sockd, fd, 3);
- }
- /*offset += rc;*/
- size_to_send -= rc;
- }
- close(fd); /*
- * la chiusura del file va qui altrimenti rischio loop
- * infinito e scrittura all'interno del file
- */
- memset(buffer, 0, sizeof(buffer));
- strcpy(buffer, "226 File Successfully transfered\n");
- if(send(newsockd, buffer, strlen(buffer), 0) < 0){
- perror("Errore durante l'invio 226");
- onexit(newsockd, sockd, 0, 2);
- }
- memset(buffer, 0, sizeof(buffer));
- }
- static void * receiver (void *arg) {
- int fd;
- int sockd = *(int *)arg;
- struct SendInfo sInfo = { "/tmp/output.txt", 0 };
- uint32_t fsize, fsize_tmp, total_bytes_read = 0;
- int nread;
- char buffer[512];
- if(read(sockd, &fsize, sizeof(fsize)) < 0){
- perror("Errore durante ricezione grandezza file\n");
- onexit(sockd, 0 ,0 ,1);
- }
- fd = open(sInfo.filename, O_CREAT | O_WRONLY, 0644);
- if (fd < 0) {
- perror("open");
- onexit(sockd, 0 ,0 ,1);
- }
- fsize_tmp = fsize;
- sInfo.filebuffer = malloc(fsize);
- if(sInfo.filebuffer == NULL){
- perror("malloc");
- onexit(sockd, 0, fd, 4);
- }
- while((total_bytes_read != fsize) &&
- ((nread = read(sockd, sInfo.filebuffer, fsize_tmp)) > 0)){
- if(write(fd, sInfo.filebuffer, nread) != nread){
- perror("write RETR");
- onexit(sockd, 0, 0, 1);
- }
- total_bytes_read += nread;
- fsize_tmp -= nread;
- }
- close(fd); /*
- * la chiusura del file va qui altrimenti rischio loop
- * infinito e scrittura all'interno del file
- */
- memset(buffer, 0, sizeof(buffer));
- if(recv(sockd, buffer, 34, 0) < 0){
- perror("Errore ricezione 226");
- onexit(sockd, 0, 0, 1);
- }
- printf("%s", buffer);
- memset(buffer, 0, sizeof(buffer));
- /*memset(dirpath, 0, sizeof(dirpath));*/
- free(sInfo.filebuffer);
- }
- int main () {
- int sp[2];
- pthread_t t[2];
- socketpair(AF_UNIX, SOCK_STREAM, 0, sp);
- pthread_create(t+0, 0, sender, sp+0);
- pthread_create(t+1, 0, receiver, sp+1);
- pthread_join(t[0], 0);
- pthread_join(t[1], 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement