利用QT的UDP技术,实现两个QT程序之间的聊天程序。
#ifndef WIDGET_H
#define WIDGET_H #include <QWidget>
#include <QUdpSocket>
#include <QPushButton>
#include <QLineEdit>
#include <QTextBrowser>
#include <QLabel>
#include <QCloseEvent> class Widget : public QWidget
{
Q_OBJECT public:
Widget(QWidget *parent = );
~Widget();
private:
QUdpSocket *udpsock;
QPushButton *btn1,*btn2,*btn3;
QLineEdit *edit1,*edit2,*edit3;
QLabel *label1,*label2,*label3;
QTextBrowser *text1;
void closeEvent(QCloseEvent *event);
private slots:
void mybindip();
void mysenddata();
void recvmydata();
}; #endif // WIDGET_H
#include "widget.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QHostAddress>
#include <QMessageBox> Widget::Widget(QWidget *parent)
: QWidget(parent)
{
this->setWindowTitle("聊天室");
udpsock=new QUdpSocket(this);
//udpsock->bind(8080);
connect(udpsock,SIGNAL(readyRead()),this,SLOT(recvmydata()));
label1=new QLabel(tr("发送端口号:"));
edit1=new QLineEdit();
label2=new QLabel(tr("接收端口号:"));
edit2=new QLineEdit();
btn1=new QPushButton(tr("绑定"));
connect(btn1,SIGNAL(clicked()),this,SLOT(mybindip())); btn2=new QPushButton(tr("发送"));
connect(btn2,SIGNAL(clicked()),this,SLOT(mysenddata()));
label3=new QLabel(tr("消息内容:"));
edit3=new QLineEdit();
text1=new QTextBrowser(); QHBoxLayout *lay1=new QHBoxLayout();
lay1->addWidget(label1);
lay1->addWidget(edit1);
lay1->addWidget(label2);
lay1->addWidget(edit2);
lay1->addWidget(btn1); QHBoxLayout *lay2=new QHBoxLayout();
lay2->addWidget(label3);
lay2->addWidget(edit3);
lay2->addWidget(btn2);
QVBoxLayout *lay3=new QVBoxLayout(this);
lay3->addLayout(lay1);
lay3->addLayout(lay2);
lay3->addWidget(text1);
} //绑定接收端口号
void Widget::mybindip()
{
udpsock->close();
//获取接收端口号
QString port1=edit2->text();
if(port1.isEmpty())
{
QMessageBox::critical(this,"错误信息","发送端口号不可以为空!");
return ;
}
udpsock->bind(port1.toInt());
QMessageBox::information(this,"提示信息","绑定成功!端口号是"+port1);
} //发送消息
void Widget::mysenddata()
{
//获取发送端口号
QString port2=edit1->text();
if(port2.isEmpty())
{
QMessageBox::critical(this,"错误信息","发送端口号不可以为空!");
return ;
}
//获取发送内容
QString txt=edit3->text();
char buf[]={};
strcpy(buf,txt.toStdString().data());
//定义地址类
QHostAddress *serip=new QHostAddress();
serip->setAddress("127.0.0.1");
udpsock->writeDatagram(buf,strlen(buf),*serip,port2.toInt());
delete serip;
edit3->clear();
edit3->setFocus();
} //接收消息
void Widget::recvmydata()
{
QMessageBox::information(this,"提示信息","接收到消息");
char buf[]={};
while(udpsock->hasPendingDatagrams())
{
udpsock->readDatagram(buf,sizeof(buf));
text1->append(buf);
memset(buf,,sizeof(buf));
}
} //关闭
void Widget::closeEvent(QCloseEvent *event)
{
if(QMessageBox::information(this,"提示信息","确定要退出该程序?",QMessageBox::Yes|QMessageBox::No,QMessageBox::No)==QMessageBox::Yes)
{
event->accept();
}else
{
event->ignore();
}
} Widget::~Widget()
{ }

QT UDP聊天小程序的更多相关文章

  1. [Socket]Socket聊天小程序

    一个简单是Socket聊天小程序,读写操作在不同的线程中.服务器端采用线程池. 1.Server import java.io.IOException; import java.net.ServerS ...

  2. Netty 聊天小程序

    这节讲解基于 Netty 快速实现一个聊天小程序. 一.服务端 1. SimpleChatServerHandler(处理器类) 该类主要实现了接收来自客户端的消息并转发给其他客户端. /** * 服 ...

  3. netty使用以及聊天小程序

    <从零开始搭建游戏服务器>Netty导入创建Socket服务器 Netty入门教程 Netty 聊天小程序

  4. Netty学习——基于netty实现简单的客户端聊天小程序

    Netty学习——基于netty实现简单的客户端聊天小程序 效果图,聊天程序展示 (TCP编程实现) 后端代码: package com.dawa.netty.chatexample; import ...

  5. 类似微信聊天小程序-网易云信,IM DEMO小程序版本

    类似微信聊天小程序-网易云信,IM DEMO小程序版本 代码地址: https://github.com/netease-im/NIM_Web_Weapp_Demo 云信IM DEMO 小程序版本 ( ...

  6. 基于JAVA网络编程的聊天小程序

    package com.neusoft.edu.socket; import java.io.BufferedReader; import java.io.IOException; import ja ...

  7. java 网络编程(四)----UDP进阶篇聊天小程序

    设计要求:单线程模式,客户端只发送数据,数据的来源为键盘录入,服务器端只接收数据,当客户端发送886的时候,客户端和服务器端都退出. 1. 发送端: public class Send impleme ...

  8. 用Socket编写的聊天小程序

    Socket是什么? 是套接字,除此之外我也不太清楚,先略过 直接上实例,首先服务端: ; //自定义端口号 private string ServerUser = "Tracy" ...

  9. python实现简单的聊天小程序

    概要 这是一个使用python实现一个简单的聊天室的功能,里面包含群聊,私聊两种聊天方式.实现的方式是使用套接字编程的一个使用TCP协议 c/s结构的聊天室 实现思路 x01 服务端的建立 首先,在服 ...

随机推荐

  1. C++/CLI——读书笔记《Visual C++/CLI从入门到精通》 第Ⅳ部分

    =================================版权声明================================= 版权声明:本文为博主原创文章 未经许可不得转载  请通过右 ...

  2. android 利用线程刷新UI方法

    新建线程new Thread(new Runnable() 线程方法:public void run() private void setAddWidgetEnabled(boolean enable ...

  3. 010 使用netmap API接管网卡,接收数据包,回应ARP请求

    一.本文目的: 上一节中,我们已经在CentOS 6.7 上安装好了netmap,也能接收和发送包了,这节我们来调用netmap中的API,接管网卡,对网卡上收到的数据包做分析,并回应ARP请求. 二 ...

  4. AIR ANE(本机扩展)使用中的一些问题(Android平台)

    关于如何写ANE,就不说了,用关键字,Android ANE 开发,会搜索到N多. 下面写一下碰到的问题,和一些别人可能没有说清的地方 1. 生成的ANE是直接拷到lib里使用吗?A:这个一定不要直接 ...

  5. C语言中不同函数之间怎么传值?

    #include <stdio.h> int change(); int change(int j) { j=; return(j); } void main() { int b = ch ...

  6. Linux autojump命令

    一.简介 autojump是一个命令行工具,它允许你可以直接跳转到你喜爱的目录,而不用管你现在身在何处. 二.安装 yum install autojump 三.用法 j [目录的名字或名字的一部分] ...

  7. 301重定向.htaccess规则(含二级目录跳转二级域名)

    301重定向是一种非常重要的"自动转向"技术.网址重定向最为可行的一种办法.当用户或搜索引擎向网站服务器发出浏览请求时,服务器返回的HTTP数据流中头信息(header)中的状态码 ...

  8. KVM 介绍(4):I/O 设备直接分配和 SR-IOV [KVM PCI/PCIe Pass-Through SR-IOV]

    学习 KVM 的系列文章: (1)介绍和安装 (2)CPU 和 内存虚拟化 (3)I/O QEMU 全虚拟化和准虚拟化(Para-virtulizaiton) (4)I/O PCI/PCIe设备直接分 ...

  9. 32-bit ALU [Verilog]

    Based on MIPS Instruction Structure Main Module module Alu( input [31:0] a, // operand 1 input [31:0 ...

  10. 求最大边/最小边的比值最小的路径 codevs 1001 舒适的路线

    codevs 1001 舒适的路线 2006年  时间限制: 2 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond   题目描述 Description Z小镇是一个景色宜人 ...