gpt4 book ai didi

C Socket Server 无法终止连接?

转载 作者:太空宇宙 更新时间:2023-11-04 08:29:17 25 4
gpt4 key购买 nike

我做了一个套接字服务器和客户端。客户端选择 pdf 文件并将其发送到服务器。这段代码在本地机器上运行完美,即服务器和客户端在同一主机上运行,​​文件在服务器端传输并且打开内容但是当我在不同的主机上运行服务器和客户端时我遇到以下问题服务器无法退出并且传输的文件在服务器端被覆盖,因此无法显示文件内容,就像我发送来自客户端的 240 kb 文件在服务器上收到的文件是 2 GB。 我在 qt creator 中编程,客户端是一个 Gui 应用程序,它也运行 Socket 客户端代码,而服务器是一个控制台项目。

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;


}

最佳答案

当您输入字符串 quit 时,它还会有 \n

所以当你比较字符串时,像这样比较:

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

然后执行代码。现在字符串将被匹配,连接将被终止。

关于C Socket Server 无法终止连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29137492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com