Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <strings.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <sys/stat.h>
- #include <sys/time.h>
- #include <fcntl.h>
- #include <string.h>
- /* Size of the buffer used to send the file in several blocks */
- #define BUFFERT 512
- static int create_server_socket(int port, in_addr_t ip );
- static struct sockaddr_in sock_serv;
- /**
- * @brief Function which allowing the creation of a socket returns a file descriptor
- * @param port port of the socket
- * @param ipaddr ip address of the socket
- * @return soket's file descriptor value.
- */
- static int
- create_client_socket(int port, in_addr_t ip )
- {
- int l;
- int sfd;
- int yes = 1;
- sfd = socket(PF_INET, SOCK_STREAM, 0);
- if (sfd == -1)
- {
- perror("Socket failure");
- return EXIT_SUCCESS;
- }
- if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
- {
- perror("Setsockopt error");
- exit(5);
- }
- /* Preparing the address of the destination socket */
- l = sizeof(struct sockaddr_in);
- struct sockaddr_in sock_serv, sock_clt;
- /* @ToDo non-POSIX func must be replaced to the memset()
- * memset( ):
- */
- bzero(&sock_serv, l);
- sock_serv.sin_family = AF_INET;
- sock_serv.sin_port = htons(port);
- sock_serv.sin_addr.s_addr = ip;
- /* Assign an identity to the socket */
- if (bind(sfd, (struct sockaddr *) &sock_serv, l) == -1)
- {
- perror("Fail the binding");
- return EXIT_FAILURE;
- }
- struct in_addr addr;
- memset(&addr, 0, sizeof (struct in_addr));
- addr.s_addr = ip;
- printf("Client started and listening on %s:%d\n", inet_ntoa (addr), port );
- return sfd;
- }
- int main(int argc, char **argv) {
- /* Connection variables */
- in_addr_t ip_p = 0x7f000001; /* Default ip address is 127.0.0.1 */
- int port_p = 1234;
- char ch; /* char for the parameter to read from argv */
- int sfd, fd; /* Descriptors */
- char buf[BUFFERT];
- int l = sizeof(struct sockaddr_in);
- off_t count = 0, m, sz;
- long int n;
- struct stat buffer;
- // if (argc != 4)
- // {
- // printf("Error usage : %s <ip_server> <port_server> <filename>\n", argv[0]);
- // return EXIT_FAILURE;
- // }
- /* Parsing the command line parameters and arguments */
- while ((ch = getopt(argc, argv, "d:p:f:")) != EOF)
- switch (ch)
- {
- case 'd':
- ip_p = inet_addr(optarg);
- break;
- case 'p':
- port_p = atoi(optarg);
- break;
- case 'u':
- printf("updating the server");
- break;
- default:
- fprintf(stderr, "Unknown option: '%s'\n", optarg);
- return 1;
- }
- /* Creating the socket file descriptor */
- sfd = create_client_socket(port_p, ip_p);
- if ((fd = open(argv[3], O_RDONLY)) == -1)
- {
- perror("Socket opening failure");
- return EXIT_FAILURE;
- }
- /* file size */
- if (stat(argv[3], &buffer) == -1)
- {
- perror("Stat fail");
- return EXIT_FAILURE;
- } else
- sz = buffer.st_size;
- // preparation of the transmission
- bzero(&buf, BUFFERT);
- if (connect(sfd, (struct sockaddr *) &sock_serv, l) == -1)
- {
- perror("Connection error\n");
- exit(3);
- }
- n = sprintf(buf,"File %s\n",argv[3]);
- m = sendto(sfd, buf, n, 0, (struct sockaddr *) &sock_serv, l);
- if (m == -1)
- {
- perror("File sending error");
- return EXIT_FAILURE;
- }
- n = sprintf(buf,"%ld\n",sz);
- m = sendto(sfd, buf, n, 0, (struct sockaddr *) &sock_serv, l);
- if (m == -1)
- {
- perror("File sending error");
- return EXIT_FAILURE;
- }
- n = read(fd, buf, BUFFERT);
- while (n)
- {
- if (n == -1)
- {
- perror("Reading fails");
- return EXIT_FAILURE;
- }
- m = sendto(sfd, buf, n, 0, (struct sockaddr *) &sock_serv, l);
- if (m == -1) {
- perror("File sending error");
- return EXIT_FAILURE;
- }
- count += m;
- bzero(buf, BUFFERT);
- n = read(fd, buf, BUFFERT);
- }
- /* Sending data to the server */
- m = sendto(sfd, buf, 0, 0, (struct sockaddr *) &sock_serv, l);
- while (n = recv(sfd, buf, BUFFERT, 0))
- {
- printf("%s",buf);
- }
- printf("Bytes transferred:% ld \n", count);
- printf("Total size:% ld \n", sz);
- close(sfd);
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment