Advertisement
Guest User

U_SERVER

a guest
Mar 14th, 2013
896
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * uclient.c
  3.  *
  4.  *  Created on: Mar 14, 2013
  5.  *      Author: none
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <fcntl.h>
  13. #include <sys/socket.h>
  14. #include <sys/types.h>
  15. #include <arpa/inet.h>
  16. #include <netinet/in.h>
  17. #include <errno.h>
  18.  
  19.  
  20. #define     PORT    3666
  21.  
  22. void die(const char *type_error){
  23.     printf("Error \"%s\" in %s\n", strerror(errno), type_error);
  24.     exit(1);        //Remember: exit doesn't terminate current function but the entire program (closing and saving each file descriptor).
  25. }
  26.  
  27. size_t  get_file_len(int fd){
  28.     size_t  len;
  29.  
  30.     lseek(fd, 0, SEEK_SET);             //set at the beginning of your file
  31.     len = lseek(fd, 0, SEEK_END);   //lseek() returns number of byte measured
  32.     lseek(fd, 0, SEEK_SET);         //set again at the beginning of the file
  33.  
  34.     return len;
  35. }
  36.  
  37. int main (){
  38.  
  39.     //define most important socket memebers
  40.  
  41.     int                 server_fd;      //server file descriptor
  42.     struct sockaddr_in  server, client;
  43.     socklen_t           addr_size;      //
  44.  
  45.     int     fd;                         //your file descriptor
  46.     char    buff[64];
  47.     char    file_name[32];
  48.  
  49.     size_t  file_len, size_to_send;
  50.  
  51.     //CREATE SOCKET
  52.     if ( (server_fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0){
  53.         die("socket()");
  54.     }
  55.     puts("socket()");
  56.  
  57.     //INITIALIZE SERVER ADDRESS
  58.     server.sin_family       = AF_INET;
  59.     server.sin_port         = htons(PORT);
  60.     server.sin_addr.s_addr  = INADDR_ANY;
  61.  
  62.     puts("address initialized");
  63.  
  64.     //BIND ADDRESS AND SOCKET FILE DESCRIPTOR
  65.     addr_size = sizeof(struct sockaddr);
  66.     if ( (bind(server_fd, (struct sockaddr*)&server, addr_size)) < 0){
  67.         die("bind()");
  68.     }
  69.     puts("bind()");
  70.  
  71.     //RECV FROM CLIENT NAME OF THE FILE YOU WANT TO OPEN
  72.     if ( (recvfrom(server_fd, file_name, sizeof(file_name), 0, (struct sockaddr*)&client, &addr_size)) < 0){
  73.         die("recvfrom");
  74.     }
  75.     printf("Connected with %s at %d port\n", inet_ntoa(client.sin_addr), htons(client.sin_port));
  76.  
  77.     //OPEN YOUR FILE (IF IT EXISTS) AND THEN CALCULATE IT'S LENGTH AND SEND IT
  78.     if ( (fd = open(file_name, O_RDONLY)) < 0){
  79.         die("open()");
  80.     }
  81.     puts("file opened");
  82.  
  83.     //GET FILE LEN
  84.     file_len = get_file_len(fd);
  85.  
  86.     //SEND FILE LENGTH
  87.     if ( (sendto(server_fd, &file_len, sizeof(file_len), 0, (struct sockaddr*)&client, addr_size)) <0 ){
  88.         die("sendto()");
  89.     }
  90.     puts("file length sent");
  91.  
  92.     //NOW SEND YOUR FILE
  93.     //READ FROM YOUR FILE (fd) AND SAVE sizeof(buff) BYTES TO BUFF
  94.     read(fd, buff, sizeof(buff));
  95.     if ( (size_to_send = sendto(server_fd, buff, strlen(buff), 0, (struct sockaddr*)&client, addr_size)) <0 ){
  96.         die("sendto()");
  97.     }
  98.     puts("file sent");
  99.  
  100.     //YOUR FILE SHOULD HAVE BEEN SENT SUCCESSFULLY
  101.  
  102.     return EXIT_SUCCESS;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement