Advertisement
Guest User

CLIENT

a guest
Feb 26th, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. #include <netdb.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/socket.h>
  6. #include <unistd.h>
  7. #include <netinet/in.h>
  8. #include <arpa/inet.h>
  9.  
  10. #define MAX 80
  11. #define PORT 7778
  12. #define SA struct sockaddr
  13. #define BLOCK_SIZE 1024
  14.  
  15.  
  16. int main(int argc, char * argv[])
  17. {
  18. if (argc < 2)
  19. {
  20. printf("The second parameter is not specified: \ "name of the program to run on the server \"\n");
  21. exit(-1);
  22. }
  23.  
  24. FILE * file;
  25. if ((file = fopen(argv[1], "r")) == NULL)
  26. {
  27. printf("Error opening file\n");
  28. exit(-1);
  29. }
  30. int fileno_1 = fileno(file);
  31.  
  32. int sockfd, connfd;
  33. struct sockaddr_in servaddr, cli;
  34.  
  35. // socket create and verification
  36. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  37. if (sockfd == -1) {
  38. printf("Socket Creation Failed...\n");
  39. exit(0);
  40. }
  41. else
  42. printf("Socket Successfully Created...\n");
  43. bzero(&servaddr, sizeof(servaddr));
  44.  
  45. // assign IP, PORT
  46. servaddr.sin_family = AF_INET;
  47. servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  48. servaddr.sin_port = htons(PORT);
  49.  
  50. // connect the client socket to server socket
  51. if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {
  52. printf("Connection With the Server Failed...\n");
  53. exit(0);
  54. }
  55. else
  56. printf("Connected to the Server...\n");
  57.  
  58. // function for chat
  59. char buffer[BLOCK_SIZE];
  60. char buffer_2[BLOCK_SIZE];
  61. int count_chars;
  62. while ((count_chars = read(fileno_1, buffer, BLOCK_SIZE)) > 0)
  63. {
  64. write(sockfd, &buffer, count_chars);
  65. }
  66. printf("File sent\n");
  67. fclose(file);
  68. close(sockfd);
  69.  
  70. if ((file = fopen("result", "w")) == NULL)
  71. {
  72. printf("Error creating program result file\n");
  73. //close(sockfd);
  74. return -1;
  75. }
  76. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  77. if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0) {
  78. printf("Connection With the Server Failed...\n");
  79. exit(0);
  80. }
  81. else
  82. printf("Connected to the Server...\n");
  83.  
  84. printf("Waiting for result file\n");
  85. while ((count_chars = read(sockfd, buffer_2, BLOCK_SIZE)) > 0)
  86. {
  87. write(fileno(file), buffer_2, count_chars);
  88. }
  89. printf("The result is saved in a file result\n");
  90. fclose(file);
  91. close(sockfd);
  92. printf("Success finish: %s\n", argv[0]);
  93.  
  94. // close the socket
  95. close(sockfd);
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement