2013-04-06 4 views
2

Я работаю с примерами примеров C++. Клиент запускается и может подключаться к серверу, но не может отправить файл. Я думаю, что есть проблема с send(), но я не могу это исправить. Исправлено: сообщение об ошибке «Сброс соединения с помощью однорангового узла»Не удалось отправить файл через сокет

Любые идеи приветствуются.

Я использую OpenSUSE с QT 4.7.4

Вот приема и передачи функции

void str_server(int sock) 
{ 
    char buf[1025]; 
    const char* filename = "//home//romanov//Documents//HelloWorld-build-desktop-Qt_4_7_4_in_PATH__System__Release//ss.png"; 

    FILE *file = fopen(filename, "rb"); 
    if (!file) 
    { 

     cerr<<"Can't open file for reading"; 
     return; 
    } 
    while (!feof(file)) 
    { 
     int rval = fread(buf, 1, sizeof(buf), file);//read value 
     if (rval < 1) 
     { 

      cerr<<"Can't read from file"; 
      fclose(file); 
      return; 
     } 

     int off = 0; 
     do 
     { 
      int sent = send(sock, &buf[off], rval - off, 0); 
      if (sent < 1) 
      { 
       // if the socket is non-blocking, then check 
       // the socket error for WSAEWOULDBLOCK/EAGAIN 
       // (depending on platform) and if true then 
       // use select() to wait for a small period of 
       // time to see if the socket becomes writable 
       // again before failing the transfer... 

       cout<<"Can't write to socket"; 
       fclose(file); 
       return; 
      } 

      off += sent; 
     } 
     while (off < rval); 
    } 

    fclose(file); 
} 

// ===================

void RecvFile(int winsock) 
{ 
    int rval; 
    char buf[1025]; 
    FILE *file = fopen("//home//romanov//Documents//HelloWorld-build-desktop-Qt_4_7_4_in_PATH__System__Release//ss2.png", "wb"); 
    if (!file) 
    { 
     printf("Can't open file for writing"); 
     return; 
    } 

    do 
    { 
     rval = recv(winsock, buf, sizeof(buf), 0); 
     if (rval < 0) 
     { 
      // if the socket is non-blocking, then check 
      // the socket error for WSAEWOULDBLOCK/EAGAIN 
      // (depending on platform) and if true then 
      // use select() to wait for a small period of 
      // time to see if the socket becomes readable 
      // again before failing the transfer... 

      printf("Can't read from socket"); 
      fclose(file); 
      return; 
     } 

     if (rval == 0) 
      break; //line 159 

     int off = 0; 
     do 
     { 
      int written = fwrite(&buf[off], 1, rval - off, file); 
      if (written < 1) 
      { 
       printf("Can't write to file"); 
       fclose(file); 
       return; 
      } 

      off += written; 
     } 
     while (off < rval); 
    } //line 175 
    while (1); 
    fclose(file); 
} 
+1

Нет необходимости удваивать символы '/' в вашей строке. '\\' появляется в Windows, потому что '\' является escape-символом, и для получения литерала '\' у вас должно быть два из них подряд. – Omnifarious

+0

Как это происходит? Печатает ли какие-либо сообщения? Это случай, когда вы должны использовать 'cerr', а не' cout' BTW. – Omnifarious

+0

Спасибо, что указал. Выход «невозможно записать из сокета». Поэтому я думаю, что есть проблема с 'send()'. Я отредактирую вопрос сейчас, мой плохой :( – Tiana987642

ответ

0

Ну, проблема в том, мой плохой, I read the wrong socket в recvFile(), если вы, ребята столкнулись с той же проблемой, вы должны проверить его.

Смежные вопросы