Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. #include <sys/socket.h>
  2. #include <stdio.h>
  3. #include <netinet/in.h>
  4. #include <string.h>
  5. #include <arpa/inet.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8.  
  9. #define LENGTH 512
  10.  
  11. int main(void) {
  12. char message[1024];
  13. struct sockaddr_in adr;
  14. int socket;
  15. char separator[] = " ";
  16. char * token;
  17.  
  18. socket = socket(PF_INET, SOCK_STREAM, 0);
  19. adr.sin_family = AF_INET;
  20. adr.sin_port = htons(3047);
  21. adr.sin_addr.s_addr = inet_addr("150.254.79.243");
  22. if (connect(socket, (struct sockaddr*) &adr,
  23. sizeof(adr)) < 0)
  24. {
  25. printf("Nawiazanie polaczenia nie powiodlo sie.\n");
  26. return 1;
  27. }
  28.  
  29. while(1) {
  30. memset(message, 0, 1024);
  31. printf("Polaczenie nawiazane.\nPodaj message: ");
  32. fflush(stdout);
  33. if (fgets(message,1024,stdin) != NULL)
  34. {
  35. printf("-------------------- ");
  36. }
  37.  
  38. token = strtok( message, separator );
  39. printf("token: %s\n", token);
  40.  
  41. if(strncmp(token, "send", 4) == 0) {
  42. printf( "wywolaj funkcje: send\n" );
  43. send(socket, token, strlen(token), 0);
  44. printf("message wyslana (message sent).\n");
  45. } else if(strncmp(token, "list", 4) == 0) {
  46. printf( "wywolaj funkcje: list\n");
  47. } else if(strncmp(token, "quit", 4) == 0) {
  48. send(socket, token, strlen(token), 0);
  49. close(socket);
  50. exit(0);
  51. } else {
  52. printf("Nieznana komenda!\n");
  53. }
  54. }
  55. close(socket);
  56.  
  57. /* Send File to Server */
  58. int sendFile() {
  59. char* fs_name = "file.txt"; //to variable
  60. char sdbuf[LENGTH];
  61. printf("[Client] Sending %s to the Server...", fs_name);
  62. FILE *fs = fopen(fs_name, "r");
  63. if(fs == NULL)
  64. {
  65. printf("ERROR: File %s not found.\n", fs_name);
  66. exit(1);
  67. }
  68.  
  69. bzero(sdbuf, LENGTH);
  70. int fs_block_sz;
  71. //int success = 0;
  72. while((fs_block_sz = fread(sdbuf, sizeof(char), LENGTH, fs))>0)
  73. {
  74. if(send(socket, sdbuf, fs_block_sz, 0) < 0)
  75. {
  76. printf("ERROR: Failed to send file %s.\n", fs_name);
  77. break;
  78. }
  79. bzero(sdbuf, LENGTH);
  80. }
  81. printf("Ok File %s from Client was Sent!\n", fs_name);
  82. //success = 1;
  83. }
  84. /* Receive File from Server */
  85. /*printf("[Client] Receiveing file from Server and saving it as final.txt...");
  86. char* fr_name = "/home/aryan/Desktop/progetto/final.txt"; //to variable
  87. FILE *fr = fopen(fr_name, "a");
  88. if(fr == NULL)
  89. printf("File %s Cannot be opened.\n", fr_name);
  90. else
  91. {
  92. bzero(revbuf, LENGTH);
  93. int fr_block_sz = 0;
  94. int success = 0;
  95. //while(success == 0)
  96. //{
  97. while(fr_block_sz = recv(sockfd, revbuf, LENGTH, 0))
  98. {
  99. if(fr_block_sz < 0)
  100. {
  101. error("Receive file error.\n");
  102. }
  103. int write_sz = fwrite(revbuf, sizeof(char), fr_block_sz, fr);
  104. if(write_sz < fr_block_sz)
  105. {
  106. error("File write failed.\n");
  107. }
  108. else if(fr_block_sz)
  109. {
  110. break;
  111. }
  112. bzero(revbuf, LENGTH);
  113. }
  114. printf("Ok received from server!\n");
  115. success = 1;
  116. fclose(fr);
  117. //}
  118. }*/
  119. return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement