tcp通信计划实现代码

来源:互联网 发布:魔兽世界优化 编辑:程序博客网 时间:2024/05/22 05:20

中途遇到了些问题,网上的答案千奇百怪,主要是很多都不经验证直接转载。

主要问题,我能记得的有

1 tcp通信首先要保证主机和arm板子之间能够ping通,我的有时候ping的通,有时候不行,总结了一下,大致流程是这样的。首先,改主机的ip使主机和板子在一个网关上,如果我的板子是192.168.1.6 则在主机上运行 ifconfig eth0 192.168.1.21   只要192.168.1这几位一样后面无所谓 。然后我的是ubuntu系统,重启网关指令是/etc/init.d/networking restart 这时候ping一下,ping通了。

2 编程头文件问题,网上的代码写的头文件是#include<QTcpSocket>,但是我的库里面没有这个,查了一下原来应该这么写#include<QtNetwork/QTcpSocket>,就没问题了。

3 tcp这个工程,不知到为啥最好在pro文件中添加QT += network这么一句话,如果不加,会发现语法没错,但是各种报错,很郁闷。

4 要自己看代码,我这个代码是基于网上一个代码编的,原来的根本运行不动,各种错误,但是框架是对的,所以需要自己改改,~~难不成我错解了原作者的良库苦用心惊恐

好了开始粘代码

首先客户端我命名为 tcp_client_arm在板子上运行的。由于我不知到怎么上传cpp,只能文本描述了

mainwindow.h

///////////////////////

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QDialog>
#include <QPushButton>
#include <QTextEdit>
#include <QGridLayout>
#include <QMainWindow>
#include<QtNetwork/QTcpSocket>


class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
  QPushButton *connectButton;
  QPushButton *sendButton;
  QPushButton *exitButton;
  QTextEdit *textEdit;
  QGridLayout *Layout;
  QTcpSocket *tcpSocket;
 public slots:
  void send_slot();
  void recv_slot();
  void connect_slot();
  void enable_sendButton();
};

#endif // MAINWINDOW_H
///////////////
main.cpp

///////////

#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    
    return a.exec();
}

//////

mainwindow.cpp

//////////

#include "mainwindow.h"
#include<QtGui>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)

{

    connectButton = new QPushButton("connect");
     sendButton = new QPushButton("send");
     exitButton = new QPushButton("exit");
     tcpSocket=new QTcpSocket;

     textEdit = new QTextEdit;

     Layout = new QGridLayout;
     Layout->addWidget(textEdit,0,0,4,8);
     Layout->addWidget(connectButton,5,0,1,2);
     Layout->addWidget(sendButton,5,3,1,2);
     Layout->addWidget(exitButton,5,6,1,2);
     QWidget * widget = new QWidget (this) ;
     widget->setLayout(Layout) ;
     this->setCentralWidget(widget) ;



     sendButton -> setDisabled(true);
     tcpSocket = new QTcpSocket(this);

     connect(connectButton,SIGNAL(clicked()),this,SLOT(connect_slot()));
     connect(sendButton,SIGNAL(clicked()),this,SLOT(send_slot()));
     connect(exitButton,SIGNAL(clicked()),this,SLOT(close()));
     connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(recv_slot()));
     connect(tcpSocket,SIGNAL(connected()),this,SLOT(enable_sendButton));
}
void MainWindow::connect_slot()
{
    tcpSocket->connectToHost("192.168.1.21",8000);//connectToHost第一个参数是字符串,表示ip地址
    sendButton->setDisabled(false);

}
void MainWindow::send_slot()
{
QString str;
str=textEdit->toPlainText();
tcpSocket->write(qPrintable(str));

}
void MainWindow::recv_slot()
{
QByteArray byte;
byte=tcpSocket->readAll();
textEdit->setPlainText(QString(byte));

}
void MainWindow::enable_sendButton()
{
sendButton->setDisabled(false);

}
MainWindow::~MainWindow()
{

}

然后是server端

mainwindow.h

/////////////

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPushButton>
#include <QTextEdit>
#include <QtGui/QGridLayout>
#include <QtNetwork/QTcpSocket>
#include <QtNetwork/QTcpServer>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
  QPushButton *sendButton;
  QPushButton *exitButton;
  QTextEdit *textEdit;
  QGridLayout *layout;
  QTcpServer *tcpServer;
  QTcpSocket *tcpSocket;
public slots:
  void send_slot();
  void recv_slot();
  void connect_slot();


};

#endif // MAINWINDOW_H

///////////

main.cpp省

//////////

mainwindow.cpp

///////

#include "mainwindow.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
     sendButton = new QPushButton("send");
     exitButton = new QPushButton("exit");
     textEdit = new QTextEdit();
     layout= new QGridLayout;
     tcpServer = new QTcpServer;
     tcpSocket=new QTcpSocket;

     layout->addWidget(textEdit,0,0,4,6);
     layout->addWidget(sendButton,5,0,1,2);
     layout->addWidget(exitButton,5,4,1,2);

     QWidget * widget = new QWidget (this);
     widget->setLayout(layout);
     this->setCentralWidget(widget);

     tcpServer->listen(QHostAddress::Any,8000);

     connect(tcpServer,SIGNAL(newConnection()),this,SLOT(connect_slot()));
     connect(sendButton,SIGNAL(clicked()),this,SLOT(send_slot()));
     connect(exitButton,SIGNAL(clicked()),this,SLOT(close()));
}

void MainWindow::connect_slot()
{
tcpSocket=tcpServer->nextPendingConnection();//返回下一个待链接的sock对象,获取一个tcpSock指针,里面包含fd指针信息
connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(recv_slot()));

}
void MainWindow::recv_slot()
{
QByteArray byte=tcpSocket->readAll();
textEdit->setPlainText(QString(byte));

}
void MainWindow::send_slot()
{
    QString str = textEdit->toPlainText();
    tcpSocket->write(qPrintable(str));


}

MainWindow::~MainWindow()
{

}

本人运行,是没问题

原创粉丝点击