用QT的Socket写的简单程序,有客户端和服务器端,后期还添加了一个选择IP地址的comBox选择框
在客户端里面输入文字,按Send按钮或者按回车键,在服务器端就可以接收到(如果你只有一台电脑的话,选ip时,请选择127,0,0,1)。
文件清单:
主要代码:
MyWidget.cpp
int main(int argc,char** argv)
{
QApplication app(argc,argv);
//MyWidget w;
//w.show();
TcpServer s;s.show();
TcpClient c;c.show();
s.setWindowTitle("Server");
c.setWindowTitle("Client");
app.exec();
}
TcpClient.cpp
#include "TcpClient.h"
#include <QHBoxLayout>
#include <QPushButton>
TcpClient::TcpClient(QWidget *parent) : QWidget(parent)
{
_socket = new QTcpSocket(this);
_socket->connectToHost("127.0.0.1",9988);
_lineEdit = new QLineEdit;
QHBoxLayout* lay = new QHBoxLayout(this);
lay->addWidget(_lineEdit);
QPushButton* button = new QPushButton("Send");
lay->addWidget(button);
connect(button,SIGNAL(clicked()),this,SLOT(slotButtonClick()));
connect(_lineEdit,SIGNAL(returnPressed()),this,SLOT(slotButtonClick()));
}
void TcpClient::slotButtonClick()
{
QString strText = _lineEdit->text();
if(strText.isEmpty())
return;
_socket->write(strText.toUtf8());
_lineEdit->clear();
}
TcpServer.cpp
#include "TcpServer.h"
#include <QHBoxLayout>
#include <QNetworkInterface>
#include "ChooseInterface.h"
#include <QMessageBox>
TcpServer::TcpServer(QWidget *parent) : QWidget(parent)
{
//创建服务器并监听
_server = new QTcpServer;
ChooseInterface dlg;
dlg.exec();
QMessageBox::information(NULL,"you select the ip:",dlg._strSelect);
_server->listen(QHostAddress(dlg._strSelect),9988);
//当有客户端来连接时,调用slotNetConnection方法
connect(_server,SIGNAL(newConnection()),this,SLOT(slotNetConnection()));
_show = new QTextBrowser;
QHBoxLayout* lay = new QHBoxLayout(this);
lay->addWidget(_show);
}
void TcpServer::slotNetConnection()
{
//判断是否有未处理的连接
while(_server->hasPendingConnections())
{
//调用nextPendingConnection去获得连接的socket
_socket = _server->nextPendingConnection();
_show->append("New connection....");
//为新的socket提供槽函数,来接收数据
connect(_socket,SIGNAL(readyRead()),this,SLOT(slotReawRead()));
}
}
void TcpServer::slotReawRead()
{
//接收数据,判断是否有数据,如果有,通过readAll函数获取所有数据
while(_socket->bytesAvailable()>0)
{
_show->append("Data arrived....");
QByteArray buf = _socket->readAll();
_show->append(buf);
}
}
ChooseInterface.cpp
#include "ChooseInterface.h"
#include <QNetworkInterface>
#include <QVBoxLayout>
ChooseInterface::ChooseInterface(QWidget *parent) : QDialog(parent)
{
/*get all interface*/
QList<QHostAddress> addrList = QNetworkInterface::allAddresses();
#if 0
QList<QNetworkInterface> infList = QNetworkInterface::allInterfaces();
QList<QNetworkAddressEntry> entryList = infList.at(0).addressEntries();
entryList.at(0).broadcast();
#endif
_comboBox = new QComboBox;
QVBoxLayout* lay = new QVBoxLayout(this);
lay->addWidget(_comboBox);
foreach(QHostAddress addr,addrList)
{
quint32 ipaddr = addr.toIPv4Address();
if(ipaddr==0)
continue;
_comboBox->addItem(QHostAddress(ipaddr).toString());
}
connect(_comboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(slotComboxBoxChange(QString)));
}
void ChooseInterface::slotComboxBoxChange(QString str)
{
this->_strSelect = str;
}
补充一下头文件
------ ------我是----- ------华丽的------ ------分割线------ ------ ------
ChooseInterface.h
#ifndef CHOOSEINTERFACE_H
#define CHOOSEINTERFACE_H
#include <QWidget>
#include <QComboBox>
#include <QDialog>
class ChooseInterface : public QDialog
{
Q_OBJECT
public:
explicit ChooseInterface(QWidget *parent = 0);
QComboBox* _comboBox;
QString _strSelect;
signals:
public slots:
void slotComboxBoxChange(QString);
};
#endif // CHOOSEINTERFACE_H
MyWidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
class MyWidget : public QWidget
{
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = 0);
signals:
public slots:
};
#endif // MYWIDGET_H
TcpClient.h
#ifndef TCPCLIENT_H
#define TCPCLIENT_H
#include <QWidget>
#include <QTcpSocket>
#include <QLineEdit>
class TcpClient : public QWidget
{
Q_OBJECT
public:
explicit TcpClient(QWidget *parent = 0);
QTcpSocket* _socket;
QLineEdit* _lineEdit;
signals:
public slots:
void slotButtonClick();
};
#endif // TCPCLIENT_H
TcpServer.h
#ifndef TCPSERVER_H
#define TCPSERVER_H
#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
#include <QTextBrowser>
class TcpServer : public QWidget
{
Q_OBJECT
public:
explicit TcpServer(QWidget *parent = 0);
QTcpServer* _server;
QTcpSocket* _socket;
QTextBrowser* _show;
signals:
public slots:
void slotNetConnection();
void slotReawRead();
};
#endif // TCPSERVER_H
成功的关键,项目文件不可以漏掉
MYT13IO.proHEADERS += \
MyWidget.h \
TcpServer.h \
TcpClient.h \
ChooseInterface.h
SOURCES += \
MyWidget.cpp \
TcpServer.cpp \
TcpClient.cpp \
ChooseInterface.cpp
QT +=gui widgets network
CONFIG += C++11
最后,再来一张图,有图有真相
植树节快乐