qt android 网络编程实例,QT网络编程Tcp下C/S架构的即时通信实例

本文展示了如何使用QT库在Android平台上实现一个C/S架构的即时通信应用。通过创建TCP客户端和服务器,实现了用户加入聊天、发送消息和离开聊天的功能。文章详细介绍了客户端和服务器的界面布局、连接逻辑以及数据收发过程。
摘要由CSDN通过智能技术生成

先写一个客户端,实现简单的,能加入聊天,以及加入服务器的界面。

#ifndef TCPCLIENT_H

#define TCPCLIENT_H

#include

#include

#include

#include

#include

#include

#include

#include

class TcpClient : public QDialog

{

Q_OBJECT

public:

TcpClient(QWidget *parent = 0,Qt::WindowFlags f=0);

~TcpClient();

private:

QListWidget *contentListWidget;

QLineEdit *sendLineEdit;

QPushButton *sendBtn;

QLabel *userNameLabel;

QLineEdit *userNameLineEdit;

QLabel *serverIPLabel;

QLineEdit *serverIPLineEdit;

QLabel *portLabel;

QLineEdit *portLineEdit;

QPushButton *enterBtn;

QGridLayout *mainLayout;

bool status;

int port;

QHostAddress *serverIP;

QString userName;

QTcpSocket *tcpSocket;

public slots:

void slotEnter();

void slotConnected();

void slotDisconnected();

void dataReceived();

void slotSend();

};

#endif // TCPCLIENT_H

有一个加入服务器的按钮,还有一个发送消息的按钮,在头文件,先定义两个函数。

#include "tcpclient.h"

#include

#include

TcpClient::TcpClient(QWidget *parent,Qt::WindowFlags f)

: QDialog(parent,f)

{

setWindowTitle(tr("TCP Client"));

contentListWidget = new QListWidget;

sendLineEdit = new QLineEdit;

sendBtn = new QPushButton(tr("send"));

userNameLabel = new QLabel(tr("name"));

userNameLineEdit = new QLineEdit;

serverIPLabel = new QLabel(tr("server"));

serverIPLineEdit = new QLineEdit;

portLabel = new QLabel(tr("port"));

portLineEdit = new QLineEdit;

enterBtn= new QPushButton(tr("join chat"));

mainLayout = new QGridLayout(this);

mainLayout->addWidget(contentListWidget,0,0,1,2);

mainLayout->addWidget(sendLineEdit,1,0);

mainLayout->addWidget(sendBtn,1,1);

mainLayout->addWi

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值