Already I guess. The file whose name is transmitted through the socket from the client to the server does not open. Everything is transmitted correctly, if any newline is added. Maybe you need to remake from char[] into a string?

  char buffer[1024]; ....(тут recv через сокет), в буфере при отладке "С:/6.pro" FILE *in = fopen(buffer, "r"); if(in == NULL) printf("Error opening file"); else printf("File was successfully opened"); 
  • in the task so 2. The client sends a request to the server containing the path of the required file and the size of the block into which the file should be split. 3. The server searches for the file and forwards the file written by the client block by block (in the absence of the required file, an error message is displayed). - AnnaHatiko
  • one
    Well, let the block, to calm the nerves of the teacher. Loop fread - send. Blocks (although the connection itself is a pipe, more precisely two pipes - back and forth, and there are no separators of blocks initially there). - alexlz
  • @ AnnaHatiko, one little debugging advice. Usually, everything is clear when there is something like the printing of lines whose contents are “doubtful” printf ("buf: [% s] \ n", buf); spaces, etc. at the beginning or at the end of the buffer immediately catch the eye. And also check the results of the call and always print them in case of an error, call perror (), and do not limit yourself to a message like "I can not open the file". By the way, one more note. It is not necessary to send the entire buffer (BUF_SIZE) to send (). Correctly limited to only relevant bytes. - (another tip does not fit. See continuation) - avp
  • (continued) And note that in recv () you can only receive a portion of the messages sent in one send (). Yes, TCP is a stream of bytes, there are no "marks" from each of send (). Actually recv () will return min from the number of data requested and stored in the OS buffer. For localhost or fast and unloaded network, receiving part of the data sent is uncharacteristic. Therefore, at this point rarely pay attention to the demo programs, but it must be kept in mind. In real protocols, either at the beginning of the data portion, its length is transmitted, or the end marker is used. Do you have this m. '\\ n' or '\\ 0'. - avp
  • @avp Read until the result of recv () is 0. If the reception is blocking (by default), then there will be nothing more. - alexlz 5:09

3 answers 3

Hm Like that

  fgets(buff, BUF_SIZE, stdin); n = strlen(buff); if (buff[n-1] == '\n') --n; send(my_socket, buff, n, 0); 

To transfer not all buff, but only the necessary characters.

  • Now the question has arisen: when the path to the file is sent - for some files it is attached to the name of the random-sized crackers, so they do not open .. how to avoid this? This generally happens for files that can not be calculated .. - AnnaHatiko
  • buffer "D: \ data2.txt Дю ^ UХJw" for example .. ( - AnnaHatiko
  • you line terminate the null character. - Roman Goriachevskiy
  • comes to the server in this form - AnnaHatiko
  • one
    As far as I understand (by the vc ++ label) we are talking about Windows. In it, fgets () counts a string in the buffer that ends with not one '\\ n', but two characters '\\ r' '\\ n' . This must be taken into account. @ AnnaHotiko, for debugging, print both the client and server results returned by all send () and recv () and the contents of the buffer (of course, only sent / received bytes). - avp
  • Test your code with fopen("С:/6.pro", "r") . What happened?

  • Maybe your buffer is not a null-terminated string?

  • Potentially (although very unlikely) there may be no corresponding permission'ов for the file.

  • fopen such things, but still you have the wrong slash in the path "C:/6.pro".

  • This is offtopic, but if you put a C++, tag C++, use std::ifstream or std::istreambuf_iterator .

    Check if you close this file before opening it again, see what is in the errno variable after this call:

     char *str; ... if(in == NULL) { str = strerrno(errno); printf("Error opening file - %s",str); }