2015-03-19 112 views
0

我已經做了一個Socket服務器和client.the客戶端選擇pdf文件並將其發送到服務器。此代碼在本地機器上完美運行即服務器和客戶端運行在同一主機上,文件傳輸在服務器端並打開隨着內容,但當我運行服務器和客戶端在不同的主機上我遇到以下問題服務器無法退出和文件傳輸被覆蓋在服務器端,從而無法顯示文件內容,如果我發送240來自客戶端的kb文件在服務器上收到的文件爲2 GB。 我在qt創建器中編程,客戶端是一個Gui應用程序,它也運行Socket客戶端代碼,而服務器是控制檯項目。C套接字服務器無法終止連接?

This is server.cpp 
#include <QtCore/QCoreApplication> 
#include <stdio.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
#define Size 2048 

    int main(int argc, char *argv[]) 
    { 
     QCoreApplication a(argc, argv); 
     int s, fd, len; 
     struct sockaddr_in my_addr; 
     struct sockaddr_in remote_addr; 
     socklen_t sin_size; 
     char buf[BUFSIZ]; 
     FILE *fp = fopen("/home/D.A.D19/Desktop/Filecopy/Q7Basic_essentials.pdf","a+"); 
     memset(&my_addr, 0, sizeof(my_addr)); 
     my_addr.sin_family = AF_INET; 
     my_addr.sin_addr.s_addr = inet_addr("192.168.103.128"); 
     my_addr.sin_port = htons(8000); 
     if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) 
     { 
     qWarning("socket"); 

     return 1; 
     } 
     if (bind(s,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))<0) 
     { 
     qWarning("bind"); 
     return 1; 
     } 
     listen(s, 5); 
     sin_size = sizeof(struct sockaddr_in); 
     if ((fd =accept(s,(struct sockaddr *)&remote_addr,&sin_size)) < 0) 
     { 
     qWarning("accepted client %s\n", inet_ntoa(remote_addr.sin_addr)); 
     return 1; 
     } 

     len = send(fd, "Welcome to my server\n", 21, 0); 

      while(1) 
      { 
       len = recv(fd,buf,Size,0); 

的問題是在這裏

    if (strcmp(buf,"quit") == 0){ 
        qWarning("Connection terminated"); 
        break; 

       } 

       fwrite(buf,Size,1, fp); 
       memset(buf,0,Size); 
      } 


     fclose(fp); 
     close(fd); 
     close(s); 

     return a.exec(); 
    } 

    This is Client.cpp 

    #include "mainwindow.h" 
    #include "ui_mainwindow.h" 
    #include <QLabel> 
    #include <QPushButton> 
    #include <QFileDialog> 
    #include <QLineEdit> 
    #include <QString> 
    #include <QtCore/QCoreApplication> 
    #include <stdio.h> 
    #include <sys/types.h> 
    #include <sys/socket.h> 
    #include <netinet/in.h> 
    #include <arpa/inet.h> 
    #define SIZE 2048 

    QString Path; 
    MainWindow::MainWindow(QWidget *parent) : 
     QMainWindow(parent), 
     ui(new Ui::MainWindow) 
    { 
     ui->setupUi(this); 

     QLabel *label1 = new QLabel(this); 
     label1->setText("FileName"); 
     label1->setGeometry(15,25,70,30); 
     label1->show(); 


     QPushButton *pushbutton = new QPushButton(this); 
     pushbutton->setText("Browse"); 
     pushbutton->setGeometry(180,28,90,25); 
     pushbutton->show(); 

     QObject::connect(pushbutton,SIGNAL(clicked()),this, SLOT(browse())); 

    } 

    MainWindow::~MainWindow() 
    { 
     delete ui; 
    } 

    void MainWindow::browse() 
    { 
     int s, len; 
      struct sockaddr_in remote_addr; 
      char buf[BUFSIZ]; 
      memset(&remote_addr, 0, sizeof(remote_addr)); 

      remote_addr.sin_family = AF_INET; 
      remote_addr.sin_addr.s_addr = inet_addr("192.168.103.128"); 
      remote_addr.sin_port = htons(8000); 

     QLineEdit *line = new QLineEdit(this); 
     line->setGeometry(82,28,90,25); 
     QString filename = QFileDialog::getOpenFileName(this,tr("Find files"), 
                 "/home/D.A.D19/Desktop"); 
     line->setText(filename); 
     line->show(); 



     if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) 
     { 
      qWarning("socket"); 
      //return 1; 
     } 


     if (::connect(s, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr)) < 0) 
     { 
      qWarning("connect"); 
      //return 1; 
     } 



     qWarning("connected to server \n"); 
     len = recv(s, buf, BUFSIZ, 0); 
     qWarning("%s \n",buf); 
     buf[len] = '\0'; 

     sprintf(buf, "%s", qPrintable(filename)); 

     FILE *fp = fopen(buf,"r"); 

      while(1) 
      { 
       if(feof(fp)) 
       { 
        strcpy(buf,"quit"); 
        send(s,buf,SIZE,0); 
        break; 
       } 
       if(!feof(fp)) 
       { 
       int count = fread(buf,SIZE,1,fp); 
       len = send(s, buf,SIZE, 0); 
       memset(buf,0,SIZE); 
       } 




      } 

      fclose(fp); 
      ::close(s); 
      return; 


    } 

回答

2

當你輸入的字符串退出,這也將有\n

所以,當你比較字符串,比較像這樣:

 if (strcmp(buf,"quit\n") == 0) 

然後執行代碼。現在該字符串將被匹配並且連接將被終止。

+0

你試過了嗎? – 2015-03-20 07:56:24

+0

你試過了嗎? – sharon 2015-03-21 05:02:45

+0

是的,我提供的解決方案無法正常工作..我解決了它..將更新解決方案 – 2015-03-21 08:45:42

相關問題