0
点赞
收藏
分享

微信扫一扫

做报表用什么工具?不想再用Excel了!!!

E_topia 2024-08-15 阅读 22

ui界面设计

我们给客户端提供服务器的ip地址,以及服务器的端口号
1.界面设计
在这里插入图片描述
2.修改对象名称

在这里插入图片描述

代码实现

断开按键的槽函数处理
在这里插入图片描述

转到槽,然后实现槽函数,直接关闭该窗口,就可以了


连接槽函数编写,首先要支持网络通信,在pro里面图示位置+network
在这里插入图片描述
加tcp头文件
在这里插入图片描述
在这里插入图片描述
在连接时需要connectToHost,我们可以在帮助里面查一下
在这里插入图片描述
我们需要调用connectToHost,所以我们需要定义一个QTcpSocket* socket,然后在构造函数中new一个QTcpSocket来调用connectToHost,做法如下:
在这里插入图片描述
在这里插入图片描述

客户端完整代码

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include<QTcpSocket>
#include<QString>
#include<QHostAddress>
#include<QMessageBox>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void on_closebutton_clicked();

    void on_connectbutton_clicked();

private:
    Ui::Widget *ui;
    QTcpSocket* socket;
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    socket =new QTcpSocket;
}

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


void Widget::on_closebutton_clicked()
{
    this->close();
}

void Widget::on_connectbutton_clicked()
{
    QString ip=ui->iplabel->text();
    QString port=ui->portlabel->text();

   socket->connectToHost(QHostAddress(ip),port.toShort());

   connect(socket,&QTcpSocket::connected,[this]()
   {

    QMessageBox::information(this,"连接提示","连接成功");


   });

   connect(socket,&QTcpSocket::disconnected,[this]()
   {

    QMessageBox::warning(this,"连接提示","连接异常");


   });


}

服务端代码

我直接给出了
main.cc

#include<iostream>
#include"tcpserver.hpp"
#include<memory>
#include<cstdio>
using namespace std;
int main(int argc,char*argv[])
{
  if(argc!=2)
  {
   cout<<"please:"<<"./server port[1024+]"<<endl;
  }
  uint16_t port=stoi(argv[1]);
  unique_ptr<tcpserver> ser(new tcpserver(port));
  ser->init();
  ser->run();
  return 0;
 


  
}

tcpserver.hpp

#pragma once 
#include<iostream>
#include<string>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include<unistd.h>
#include<cstring>
using namespace std;
uint16_t defaultport=8080;
string defaultip="0.0.0.0";
const int backlog=5;
class tcpserver
{
public:
tcpserver(uint16_t& port=defaultport,string& ip=defaultip)
:sockfd_(0)
,tcpip_(ip)
,tcpport_(port)
{

}
void init()
{
 sockfd_=socket(AF_INET,SOCK_STREAM,0);
 if(sockfd_<0)
 {cout<<"创建套接字失败"<<endl;}
 cout<<"创建套接字成功"<<endl;


 //绑定
 struct sockaddr_in client;
 client.sin_family=AF_INET;
 client.sin_port=htons(tcpport_);
 //char str[32];
 inet_aton(tcpip_.c_str(),&client.sin_addr);
 int len=sizeof(client);


 if(bind(sockfd_,(const sockaddr*)&client,len)<0)
 {
   cout<<"绑定失败"<<endl;
   exit(1);

 }
   cout<<"绑定成功"<<endl;



   int n=listen(sockfd_,backlog);
   if(n<0)
   {cout<<"监听失败"<<endl;}
   cout<<"监听成功"<<endl;

}

void run()
{
  while(true)
  {
  struct sockaddr_in local;

  
   socklen_t len=sizeof(local);

  int sockfd=accept(sockfd_,(sockaddr*)&local,&len);
  if(sockfd>0){
    cout<<"三次握手成功"<<endl;
  

  }
       char buffer[4096];

    
  while(1)
  {
  int n=read(sockfd,buffer,sizeof(buffer));
  if(n>0)
  {
   buffer[n]=0;
   cout<<"client say#"<<buffer<<endl;



   string message="server say#";
   message+=buffer;
   cout<<message<<endl;



  write(sockfd,message.c_str(),message.size());


   }


  }





  
  
  











  }



}



~tcpserver()
{





}
private:

string tcpip_;
int sockfd_;
uint16_t tcpport_;







};

makefile

server:main.cc
	g++ -o server main.cc 
.PHONY:clean
clean:
	rm -rf server 

效果演示

tcp客户端

举报

相关推荐

0 条评论