QT TCP通信为什么一点发送数据按钮就断开连接了?

void denglu2::on_pushButton_clicked()//QT端发送代码
{
mp_TCPSocket->write("1");
}

void loop()//arduino端接受代码
{
char flag=mySerial.read();
if(Serial.available()>0)
{
//从硬件串口读出一字节,写入软件串口
mySerial.write(Serial.read());
}
//如果软件串口有数据
if(mySerial.available())
{
//从软件串口读出一字节,写入硬件串口
Serial.write(mySerial.read());
}
// wendu();

if(flag=='1'||flag==49)
while(1)
duqu();
else if(flag=='b')
zhuce();
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
QT socket 通信自动发送数据
我把要发送的数据写入了一个txt文档,然后在textedit里通过QFile 已逐条读取的方式打开,手动点击发送按钮,数据能正常发给接收端,当我使用定时器每2秒让其自动发送一组数据时,前两个数据能正常接收,后面的就会出现黏包,界面还会卡死,请问大神们这是为什么?rnrn发送端程序:rn[img=https://img-bbs.csdn.net/upload/201804/27/1524836241_288927.png][/img]rn接收端程序:Python写的rn[img=https://img-bbs.csdn.net/upload/201804/27/1524836255_837064.png][/img]rn接收端运行的结果:rn[img=https://img-bbs.csdn.net/upload/201804/27/1524836267_139927.jpg][/img]rn要发送的数据:rn[img=https://img-bbs.csdn.net/upload/201804/27/1524836282_891694.png][/img]
Qt模拟串口-tcp连接发送数据
上一节已经基本上完成了qt界面的基本布局,下面就是后台数据的发送和接收了, 前面说了,qt和uc/os 交互使用的方式是tcp 为啥要用tcp,而不使用别的呢, udp:按道理udp是最好的,不用连接,想发就发,不用管端口。但是,qt这个鬼,他封装的udp函数不支持服务器下发数据,也就是udp的服务器不能下发数据,只能接收。 串口:既然是串口通讯,为啥不直接用串口呢。c语言对串口的支持太差
Qt简单实现Tcp通信
开发环境:Ubuntu 16.04语言:C++服务器程序:#include "dialog.h" #include "ui_dialog.h" #include <QtNetwork> Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(th...
QT实现TCP通信
相关代码有已经放入我的资源,有兴趣可前往下载 对于qt下的tcp,如下图: 一.服务器端 1.建立套接字 server=new QTcpServer(this); //监听套接字 server->listen(QHostAddress::Any,8888);//QHostAddress对应网卡ip地址 setWindowTitle("服务器:8888"...
qt tcp 通信demo
qt使用tcp通信的demo,包含server和client,在同一个工程中,分别执行即可。
Qt tcp通信简单实现
功能:Qt前台界面作为客户端,linux后台程序作为服务端,客户端发送字符串给服务端,服务端返回大写 前台界面实现:通过Qt designer生成    (地址映射:10.200.1.46:19961 -> 172.18.18.99:9961) 10.200.1.46:19961(windows本机访问linux服务端对外地址端口) 172.18.18.99:9961(linux服务端
qt tcp通信
假如有一个服务器,多个客户端,服务器怎么获取客户端IP,客户端怎么获取服务器IP, 客户端之间如何获得IP
Qt Tcp通信示例
代码中简单的模拟了TCP网络通信的一个示例。包括Server与Client的实现
Qt实现TCP通信
一、客户端 客户端使用Qt中的QTCPSocket类与服务器进行通信可分为5个步骤: ①创建QTcpSocket套接字对象 socket = new QTcpSocket(); ②使用这个对象连接服务器 socket->connectToHost(IP, port); ③使用write函数向服务器发送数据 socket.write(data); ④当socket接收缓冲区有新数据到来时,会发...
qt tcp通信问题
怎么完成一次连接多次通信 比如 客户端给服务器发送数据 服务器在给客户端回数据 客户端又给服务器发送数据 服务器再回 如此反复 直到断开连接 肯定有办法的把 小弟不会啊 还有怎么去主动断开连接呢
Qt中的TCP通信
简述     Qt的TCP通信主要使用了两个类 QTcpServer和QTcpSocket。它们封装个不同操作系统的通信套接字,使用起来也一样。       QTcpServer:主要用在服务端,用来监听客户端的连接请求。有两个常用的接口            newConnection():这个是一个信号,当QTcpServer检测到有客户端连接时会发出。
qt在实现tcp通信
#include "clienttest.h" #include "ui_clienttest.h" #include Clienttest::Clienttest(QWidget *parent) : QWidget(parent), ui(new Ui::Clienttest) { ui->setupUi(this); ret = 0 ;
QT之TCP通信简单实现
一、服务器端实现 1.tcpserver.h #ifndef TCPSERVER_H #define TCPSERVER_H #include #include namespace Ui { class TcpServer; } class TcpServer : public QMainWindow { Q_OBJECT public: explicit TcpS
TCP通信Qt源码
tcp通信Qt源码,客户端和服务器融合,可发文件和可发消息,
QT tcp 通信
QT TCP 服务器代码
QT之TCP通信
QT中可以通过TCP协议让服务器和客户端之间行通信。所以下面我就围绕服务器和客户端来写。 这是我么写服务器和客户端的具体流程: A、服务器: 1.创建QTcpServer对象 2.启动服务器(监听)调用成员方法listen(QHostAddress::Any,端口号) 3.当有客户端链接时候会发送newConnection信号,触发槽函数接受链接(得到一
Qt下TCP通信流程
Qt下TCP通信流程描述Qt下Tcp通信的整个流程 :服务器端: 1. 创建用于监听的套接字 2. 给套接字设置监听 3. 如果有连接到来, 监听的套接字会发出信号newConnected 4. 接收连接, 通过nextPendingConnection()函数, 返回一个QTcpSocket类型的套接字对象(用于通信) 5. 使用用于通信的套接字对象通信 1>. 发送数据: write 2...
基于QT的TCP通信
这是一个简单的基于QT的TCP通信协议的图片传输
解决TCP通信客户端断开连接服务器自动关闭
服务器会断开主要是 客户端断开后产生了 SIGPIPE信号   在对的位置忽略掉该信号就可以了! 蓝色框体为我自己测试结果!框体左边的是查的大牛们的讲解。 哈哈!第一次写博客 还请多包涵!都不会用这玩意儿 ...... 囧...
为什么我的按钮一点就闪退
界面如下,是大地测量问题的解算[img=https://img-bbs.csdn.net/upload/201412/28/1419740480_417338.png][/img]rn[code=csharp]using System;rnusing System.Collections.Generic;rnusing System.ComponentModel;rnusing System.Data;rnusing System.Drawing;rnusing System.Linq;rnusing System.Text;rnusing System.Windows.Forms;rnrnnamespace 大地测量实习1rnrn public partial class Form5 : Formrn rn public Form5(object items)rn rn InitializeComponent();rn tuoqiu = Convert.ToString(items);rn rn string tuoqiu;rn double a, b, c, arf, ef, epf;rn public void tq()rn rn if (tuoqiu == "克拉索夫斯基椭球")rn rn a = 6378245;rn b = 6356863.0188;rn c = 6399698.9018;rn arf = 0.00335232986926;rn ef = 0.00669342162297;rn epf = 0.00673852541468;rn rn else if (tuoqiu == "IUGG1975椭球")rn rn a = 6378140;rn b = 6356755.2882;rn c = 6399596.6520;rn arf = 0.00335281317790;rn ef = 0.00669438499959;rn epf = 0.00673950181947;rn rn else if (tuoqiu == "CGCS2000椭球")rn rn a = 6378137;rn b = 6356752.3141;rn c = 6399593.6259;rn arf = 1 / 298.257222101;rn ef = 0.00669438002290;rn epf = 0.00673949677547;rn rn rn private void Form5_Load(object sender, EventArgs e)rn rnrn rnrn private void button1_Click(object sender, EventArgs e)rn rn tq();rn double B, L,X, N, t, m, nn,l3,l6,n3,n6,L0,x6,y6,Y6,x3,y3,Y3;rn B = (Convert.ToDouble(textBox1.Text) * 3600 + Convert.ToDouble(textBox2.Text) * 60 + Convert.ToDouble(textBox3.Text)) / 3600;rn L=(Convert.ToDouble(textBox4.Text) * 3600 + Convert.ToDouble(textBox5.Text) * 60 + Convert.ToDouble(textBox6.Text)) / 3600;rn if (tuoqiu == "克拉索夫斯基椭球")rn rn X = 111134.8611 * B - 16036.4803 * Math.Sin(2 * B / 360 * 2 * Math.PI) + 16.8281 * Math.Sin(4 * B / 360 * 2 * Math.PI) - 0.0220 * Math.Sin(6 * B / 360 * 2 * Math.PI);rnrn rn else if (tuoqiu == "IUGG1975椭球")rn rn X = 111133.0047 * B - 16038.5282 * Math.Sin(2 * B / 360 * 2 * Math.PI) + 16.8326 * Math.Sin(4 * B / 360 * 2 * Math.PI) - 0.0220 * Math.Sin(6 * B / 360 * 2 * Math.PI);rn rn else if (tuoqiu == "CGCS2000椭球")rn rn X = 111132.95254700 * B - 16038.508741268 * Math.Sin(2 * B / 360 * 2 * Math.PI) + 16.832613326622 * Math.Sin(4 * B / 360 * 2 * Math.PI) - 0.021984374201268 * Math.Sin(6 * B / 360 * 2 * Math.PI);rn rn else X = 0;rn n6=(int)(L/6)+1;rn L0=n6*6-3;rn l6=L-L0;rn m = Math.Cos(B / 360 * 2 * Math.PI) * l6 * Math.PI / 180;rn nn = Math.Sqrt(epf) * Math.Cos(B / 360 * 2 * Math.PI);rn t = Math.Tan(B / 360 * 2 * Math.PI);rn N = a / Math.Sqrt(1 - ef * Math.Sin(B / 360 * 2 * Math.PI) * Math.Sin(B / 360 * 2 * Math.PI));rn x6 = X + N * t * (m * m / 2 + (5 - t * t + 9 * nn * nn + 4 * Math.Pow(nn, 4) * Math.Pow(m, 4)) / 24 + (61 - 58 * t * t + Math.Pow(t, 4)) * Math.Pow(m, 6) / 720);rn y6 = N * (m + (1 - t * t + nn * nn) * Math.Pow(m, 3) / 6 + (5 - 18 * t * t + Math.Pow(t, 4) + 14 * nn * nn - 58 * nn * nn * t * t) * Math.Pow(m, 5) / 120);rn Y6 = 19500000 + y6;rn n3 = (int)((L -1.5)/ 3) + 1;rn L0 = n3 * 3;rn l3 = L - L0;rn x3 = X + N * t * (m * m / 2 + (5 - t * t + 9 * nn * nn + 4 * Math.Pow(nn, 4) * Math.Pow(m, 4)) / 24 + (61 - 58 * t * t + Math.Pow(t, 4)) * Math.Pow(m, 6) / 720);rn y3 = N * (m + (1 - t * t + nn * nn) * Math.Pow(m, 3) / 6 + (5 - 18 * t * t + Math.Pow(t, 4) + 14 * nn * nn - 58 * nn * nn * t * t) * Math.Pow(m, 5) / 120);rn Y3 = 19500000 + y3;rn textBox7.Text = Convert.ToString(x3);rn textBox8.Text = Convert.ToString(y3);rn textBox9.Text = Convert.ToString(Y3);rn textBox10.Text = Convert.ToString(x6);rn textBox11.Text = Convert.ToString(y6);rn textBox12.Text = Convert.ToString(Y6);rn rnrn private void button2_Click(object sender, EventArgs e)rn rn tq();rn double x, y, X, Y, B13, B23, B33, L13, L23, L33, L16, L26, L36,Vf,tf,Nf,nnf,A1,B1,C1,D1,Bf1,B,L3,L6,L30,L60,l,n;rn double[] Bf = new double[1000];rn X = Convert.ToDouble(textBox15.Text);rn Y = Convert.ToDouble(textBox16.Text);rn x = Convert.ToDouble(textBox13.Text);rn y = Convert.ToDouble(textBox14.Text);rn A1 = 1 + 3 / 4 * ef + 45 / 64 * ef * ef + 175 / 256 * ef * ef * ef;rn B1 = 3 / 4 * ef + 15 / 16 * ef * ef + 525 / 512 * ef * ef * ef;rn C1 = 15 / 64 * ef * ef + 105 / 256 * ef * ef * ef;rn D1 = 35 / 512 * ef * ef * ef;rn Bf[0] = X / (a * (1 - ef) * A1);rn for (int i = 0; ; i++)rn rn Bf[i + 1] = (X / (a * (1 - ef)) + B1 / 2 * Math.Sin(2 * Bf[i]) - C1 / 4 * Math.Sin(4 * Bf[i]) + D1 / 6 * Math.Sin(6 * Bf[i])) / A1;rn if (Math.Abs(Bf[i + 1] - Bf[i]) <= 1 / Math.Pow(10, 20))rn Bf1 = Bf[i]; break; rn rn tf = Math.Tan(Bf1);rn Vf = Math.Sqrt(1 + epf * epf * Math.Cos(Bf1) * Math.Cos(Bf1));rn Nf = c / Vf;rn nnf = Math.Sqrt(epf) * Math.Cos(Bf1);rn B = Bf1 / Math.PI * 180 - Vf * Vf * tf * (y * y / Nf / Nf - (5 + 3 * tf * tf + nnf * nnf - 9 * nnf * nnf * tf * tf) / 12 * Math.Pow((y / Nf), 4) + (61 + 90 * tf * tf + 45 * tf * tf) * Math.Pow((y / Nf), 6) / 360) / 2 * 180 / Math.PI;rn l = (y / Nf - (1 + 2 * tf * tf + nnf * nnf) * Math.Pow((y / Nf), 3) / 6 + (5 + 28 * tf * tf + 24 * Math.Pow(tf, 4) + 6 * nnf * nnf + 8 * nnf * nnf * tf * tf) / 120 * Math.Pow((y / Nf), 5)) / Math.Cos(Bf1) * 180 / Math.PI;rn n = (int)(Y / 1000000);rn L30 = 3 * n;rn L60 = n * 6 - 3;rn L3 = L30 - l;rn L6 = L60 - l;rn L3 = L3 / Math.PI * 180;rn L13 = (int)L3;rn L23 = (int)((L3 - L13) * 60);rn L33 = ((L3 - L13) * 60 - L23) * 60;rn L16 = (int)L6;rn L26 = (int)((L6 - L16) * 60);rn L36 = ((L6 - L16) * 60 - L26) * 60;rn B13 = (int)B;rn B23 = (int)((B - B13) * 60);rn B33 = ((B - B13) * 60 - B23) * 60;rn textBox17.Text = Convert.ToString(B13);rn textBox18.Text = Convert.ToString(B23);rn textBox19.Text = Convert.ToString(B33);rn textBox20.Text = Convert.ToString(L13);rn textBox21.Text = Convert.ToString(L23);rn textBox22.Text = Convert.ToString(L33);rn textBox23.Text = Convert.ToString(B13);rn textBox24.Text = Convert.ToString(B23);rn textBox25.Text = Convert.ToString(B33);rn textBox26.Text = Convert.ToString(L16);rn textBox27.Text = Convert.ToString(L26);rn textBox28.Text = Convert.ToString(L36);rn rn rnrn[/code]rn不管输入什么数据,点击两个按钮就直接跳出
Tcp 通信中用Socket发送数据为什么会重复。
在程序中用了多线程socket编程,很奇怪在用Socket.send(byte[] data)方法时,会连续发送出两个或者三个data数据。这会不会跟发送缓存有什么关系啊?
QT基础(八)----TCP通信 & UDP通信
TCP和UDP程序实现方法!
安卓和C# 用TCP通信发送数据
目前在做一个程序,安卓手机每一秒钟向PC机发送若干条数据,有可能一台机器发,也有可能若干台同时发,PC机用C#接收数据。我测试了一台安卓发数据可以正常接收,可是当若干台同时发数据的时候,就出现问题了。后来我发现不知道为什么,这些安卓设备的IP地址用DHCP自动获取IP的时候IP都是一样的,我想会不会就是因为两个相同IP的机器发送数据的时候,因为后来发后数据的机器会把原先正在发送的机器建立的TCP通信链接给关掉然后自己在发送数据,现在由于实验室路由器比较特殊,我没有权限改成静态获取,所以在此问一下是不是因为这个原因出的错,下面列出代码,麻烦大家帮忙看一看,出错的地方我用注释标记出来了rn安卓端发送数据代码rn```rn public void sendToPC(String data)rn try rn s =new Socket("192.168.0.104",8730);rn catch (UnknownHostException e) rn e.printStackTrace();rn catch (IOException e) rn e.printStackTrace();rn rn DataOutputStream dout=null; rn try rn rn dout =new DataOutputStream(s.getOutputStream());rn Thread.sleep(50, 0); rn dout.writeBytes(data);rn dout.flush();rn catch(Exception e)rn rn rn rn```rnC#段代码rn```rn private void listenConnect()rn rn while (true)rn rn tcpClient = myListener.AcceptTcpClient();rn //获取网络流rn User user = new User(tcpClient);rn rn Thread receiveThread = new Thread(receiveData);rn receiveThread.IsBackground = true;rn receiveThread.Start(user);rnrn rn rn string receiveString = "";rn public void receiveData(object u)rn rn User user = (User)u;rn NetworkStream netStream1 = user.client.GetStream();rn while(true)rn byte[] myReadBuffer = new Byte[1024];rn int numberOfBytesRead = 0;rn StringBuilder myCompleteMessage = new StringBuilder();rn int nodeId = 0;rn dorn rn numberOfBytesRead = netStream1.Read(myReadBuffer, 0, myReadBuffer.Length);//出错总是在这一句,提示错误“远程主机主动关闭现有连接”rn myCompleteMessage.AppendFormat("0", Encoding.UTF8.GetString(myReadBuffer, 0, numberOfBytesRead));rn while (netStream1.DataAvailable);rn receiveString = myCompleteMessage.ToString();rn string[] receivearr = receiveString.Split('!');rn for (int i = 0; i < receivearr.Length-1;i++ )rn rn rn string[] infoarr = receivearr[i].Split('#');rn DoWork(infoarr[1], infoarr[0]);rn rn rn rn rn rn rn```rnrn
QT 学习 (TCP服务器通信)
QT编写TCP服务器通信总结: TCP服务器通信 , QT提供了服务器类QTcpServer和客户端类QTcpSocket 程序源码下载:https://download.csdn.net/download/dianzishi123/10849989 说明:上传的源码是最初始的,文档下的源码会根据不同时候的不同理解而改动 操作流程: 1. 创建一个基于MainWindows的UI工程...
qt TCP多客户端通信
QT TCP一个服务端连接多个客户端通信,欢迎大家参考!
QT编写的tcp通信例程
qt编写的一个chat例程,有client和server两部分。已经在tyni6410移植成功,做tcp通信的很好例程。
Qt TCP通信,多线程服务器端
相信许多初学Qt的同学都会和我一样遇到这样的问题:一、Qt TCP通信在使用nextPendingConnect后,服务器端就只会与最后接入的客户端通信,这个时候就会考虑继承QThread实现多线程,从而实现多个客户端与服务器端通信,每当一个新的客户端连接时,通过标识码socketDescriptor,实现与对应的客户端通信。void server::incomingConnection(int s
Qt套接字实现TCP通信
环境:主机:win10开发环境:Qt功能:用TCP进行收发通信界面:源代码:在TCPsocket.pro文件中#-------------------------------------------------## Project created by QtCreator 2018-03-21T13:40:24##------------------------------------------...
qt编写的tcp、udp通信
使用qt在Windows系统上实现了tcp和udp通信,分别有服务器程序和客户端程序, 已经编译成功,亲测可用
QT TCP Socket 通信示例
QT的SOCKET通信实例,基于TCP通信协议,关于具体的实现机制请参考:http://blog.csdn.net/bailang_zhizun/article/details/78327974
QT中的 TCP 通信 (一)
客户端 :widget.h#ifndef WIDGET_H #define WIDGET_H #include #include /* 4.5 版本的 QT 头文件 - ARM 开发板使用的 Qt 版本#include #include #include */ #include #include #include #include #include #incl
Qt下简单Tcp通信学习笔记
Qt下简单Tcp通信学习笔记 代码实现 TcpDemo TcpDemo.pro #------------------------------------------------- # # Project created by QtCreator 2018-09-07T18:33:47 # #-------------------------------------------...
qt udp/tcp 通信示例代码
很有用的代码示例,是学习qt,和文件传输的好例子。欢迎评论!
QT之TCP/IP通信---服务端
项目需求:使用QT的TCP实现客户端与服务端的通信,界面如下 写在开头:几个重要的方法与信号 //监听方法,监听本机所有IP的9999端口 tcpServer-&amp;gt;listen(QHostAddress::Any, 9999); //当新连接建立时触发该信号 &amp;amp;QTcpServer::newConnection //A. 取出tcpSocket套接字 tc...
QT之TCP/IP通信---客户端
继续上一篇的... 写在前面:客户端中几个重要的信号与方法 //连接服务器,port指的是服务器监听的端口 tcpSocket-&amp;gt;connectToHost(QHostAddress(ip), port); //与服务器连接成功时触发该信号 &amp;amp;QTcpSocket::connected //服务器发来消息时触发该信号 &amp;amp;QTcpSocket::readyRead ...
QT TCP通信服务器出错
服务器端在接收到客户端的消息之后就提示QObject: Cannot create children for a parent that is in a different thread.rn(Parent is QNativeSocketEngine(0xa7e6428), parent's thread is mythread(0x35ea30), current thread is QThread(0x3578b0)rnrn[color=#FF0000]//widget.cpp[/color]rn#include "widget.h"rn#include "ui_widget.h"rnrnWidget::Widget(QWidget *parent) :rn QWidget(parent),rn ui(new Ui::Widget)rnrn ui->setupUi(this);rn server = new mytcpserver;rn listen();rnrnrnvoid Widget::listen()rnrn if(server->listen(QHostAddress::Any,5555))rn rn ui->status_label->setText("listening");rn rn elsern rn qDebug() << server->errorString();rn close();rn rnrnrnWidget::~Widget()rnrn delete ui;rnrnrnvoid Widget::on_clear_pushButton_clicked()rnrn ui->listWidget->clear();rnrnrn[color=#FF0000]//mytcpserver.cpp[/color]rn#include "mytcpserver.h"rn#include "mythread.h"rnrnmytcpserver::mytcpserver(QObject *parent) :rn QTcpServer(parent)rnrnrnrnvoid mytcpserver::incomingConnection(int socketDescriptor)rnrn mythread *thread = new mythread(socketDescriptor,this);rn connect(thread,SIGNAL(finished()),thread,SLOT(deleteLater()));rn thread->start();rnrnrn[color=#FF0000]//mythread.cpp[/color]rn#include "mythread.h"rnrnmythread::mythread(int socketDcriptor,QObject *parent) :rn QThread(parent),socket_fd(socketDcriptor)rnrnrnrnvoid mythread::run()rnrn server_socket = new QTcpSocket; //不要在主线程中创建,即不要加上thisrn server_socket->setSocketDescriptor(socket_fd);rn connect(server_socket,SIGNAL(readyRead()),this,SLOT(readMessage()));rn exec(); //这样会显示的调用消息循环进而触发slotrnrnrnvoid mythread::readMessage()rnrn QByteArray block = server_socket->readAll();rn QDataStream in(&block,QIODevice::ReadOnly);rn in.setVersion(QDataStream::Qt_4_6);rn QString readmsg;rn quint16 blockSize = 0;rn in >> blockSize >> readmsg;rn qDebug() << tr("server receive a message:") << readmsg;rn sendMessage();rnrnrnvoid mythread::sendMessage()rnrn QString msg = "server received sucess";rn QByteArray block;rn QDataStream out(&block, QIODevice::WriteOnly);rn out.setVersion(QDataStream::Qt_4_6);rn out << (quint16)0;rn out << msg;rn out.device()->seek(0);rn out << (quint16)(block.size() - sizeof(quint16));rn server_socket->write(block);rn //server_socket->disconnectFromHost();rn //server_socket->waitForDisconnected();rnrnrn
TCP 发送数据
tcp在win2控制台程序中,服务器如何随机发送数据,我的意思是想什么时候发就什么时候发,这在mfc中很好实现,但是在c++中就不知道怎么写了,比方我下面的程序,就写了服务器发送接收部分rn...rnwhile( TRUE )rnrn char buf1[50];rn cin>>buf1;rn send( iClientSock, buf1, strlen( buf1 ), 0 );rn char buf2[50];rn int numbytes = recv( iClientSock, buf2, 50, 0 );rn buf2[ numbytes ] = '\0';rn printf( "Im Sever: %s\n", buf2 );rnrn...rn这样写的话,接收和发送一定要按顺序来,如果服务器发送数据后没有接收到客户端没的话,就一直不能发送,后来我还用rnWSAAsyncSelect()和 CALLBACK WinProc()结合的方法也解决不了我的问题,谁能帮我下,谢谢,在线等
QT之TCP网络传输 - 发送数据、命令和文件
结合前面参考的两个例子,完成了如下通信。代码以后添加。
Qt tcp 多线程下QTCPsocket不能发送数据~
工程文件MainWindow.pro文件~rn在实现QTCPsocket的基本功能(连接服务器,(不循环)发送数据)~rn但是接受不到数据(有readyread()信号)rnrn添加qt类 mythread类;并且在mainwindow头文件中包含此类的头文件rn(类中包含QTCPsocket文件和QTCPsocket *p_socket指针,和要发生的数据char sendbuff[])rnrn在mainwindow类中 实现线程循环时,实例一个mythread的对象,并对其赋值;rnrnmythread.senbuf = mainwindow的数组rnmythread.p_sendbuff = mainwindow的QTCPsocket 指针rnrn我企图用上面的代码来传递指针和要发送的数组;rnrn但是代码怎么也发送不过去~~(mythread数组的值已经传递过来,但是指针的值我不敢确定)rnrn还有就是在mainwindow类中的QTCPsocket 指针读不出来数据(有readyread()信号);rnrn到了现在我实在是无招了~~rnrn所以上来问问,请大仙们给点指导和意见~rnrn还请大仙不灵赐教 (附代码~)rnrnrnrnrn[code=C/C++]rn//MAINWINDOW 头文件rnrn#ifndef MAINWINDOW_Hrn#define MAINWINDOW_Hrn#include rn#include rn#include rn#include rn#include rn//#include rn#include rn#include rnrnrnstruct message_streamrn unsigned char protocol_version; //1BYTErn QByteArray message_type; //2BYTErn QByteArray message_length; //2BYTErn QByteArray parameter_type; //2BYTErn QByteArray parameter_length; //2BYTErn QByteArray parameter_Value; //不定长rn;rnrnnamespace Ui rnclass MainWindow;rnrnrnclass MainWindow : public QMainWindowrnrn Q_OBJECTrnrnpublic:rn explicit MainWindow(QWidget *parent = 0);rn ~MainWindow();rn QSettings *p_longfile; //rn QTcpSocket *p_socketet;rn QTcpSocket *P_thr_socket;rn QByteArray qba_message; //recv到的数据rn MyThread mthread; //rn message_stream mag_stra; //rnrn char m_buffer[8];rn int blockSize; //发送/接收文件的大小rn bool autosendstat;rnrn bool GenerateCW( char *buffer); //生成控制字rn void SocketTCPstart();rn void SockSendData(char *buffer);rn void analyzingbuff(QByteArray qb_recvarrat);rnrnrnrnprivate slots:rnrnrn void SockRecvData(); //接收数据rnrn void socketerror(); //socker错误rnrn void connectError(); //连接服务器错误rnrn void on_pushButton_send_clicked();rnrn void on_action_socket_close_triggered();rnrn void on_action_socket_open_triggered();rnrn void on_action_exit_triggered();rnrn void on_action_serverconfig_triggered();rnrn void on_action_default_triggered();rnrn void on_action_rand_triggered();rnrn void on_action_define_triggered();rnrnprivate:rn Ui::MainWindow *ui;rn;rnrn#endif // MAINWINDOW_Hrnrnrnrn rnrn//MAINWINDOW cpp文件rnrnrn/////////////////////////mainwindow.cpp///////////////rnrn#include "mainwindow.h"rn#include "ui_mainwindow.h"rnrnMainWindow::MainWindow(QWidget *parent) :rn QMainWindow(parent),rn ui(new Ui::MainWindow)rnrn ui->setupUi(this);rn p_socketet = new QTcpSocket(this);rn autosendstat = false;rn memset(m_buffer,0,sizeof(m_buffer));rn //connect(,SIGNAL(),this,SLOT());rnrn connect(p_socketet,SIGNAL(readyRead()),this,SLOT(SockRecvData())); //有数据来rn connect(p_socketet,SIGNAL(disconnect()),this,SLOT(connectError()));rn connect(p_socketet,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(socketerror()));rn ui->pushButton_send->setEnabled(false);rn SocketTCPstart();rnrnrnMainWindow::~MainWindow()rnrn delete ui;rn delete p_socketet;rnrnrnvoid MainWindow::SocketTCPstart()rnrn p_longfile = new QSettings("ClientConfig.ini",QSettings::IniFormat,0);rn QString temp_STR = p_longfile->value("CLIENT_CONFIG/SERVERIP").toString();rn int temp_I = p_longfile->value("CLIENT_CONFIG/SERVERPORT").toUInt();rn if(temp_STR.isEmpty()||(temp_I == 0))rn rn ui->label_dissata->setText("错误:client启动失败!可能是配置文件出错!");rn return;rn rn elsern ui->pushButton_send->setEnabled(true);rnrn p_socketet->connectToHost(temp_STR,temp_I,QIODevice::WriteOnly);rnrn delete p_longfile;rnrnrnrnbool MainWindow::GenerateCW(char *buffer)rnrn......rnrnrnvoid MainWindow::SockSendData(char *buffer)rnrn // GenerateCW(buffer);rn p_socketet->write(buffer,8);rnrnrnvoid MainWindow::on_pushButton_send_clicked()rnrn if( GenerateCW(m_buffer))rn rnrn if(mthread.b_threadsata == true)rn rn ui->pushButton_send->setText("发送");rn mthread.b_threadsata = false;rn mthread.exit();rn ui->lineEdit->clear();rn rnrn if(ui->lineEdit->text().isEmpty()==false)rn rn mthread.b_threadsata = false;rn mthread.p_sock = p_socketet;rn mthread.sendnumber =ui->lineEdit->text().toUInt();rn mthread.p_senddata =m_buffer;rn mthread.start();rn // mthread.wait();rn // ui->pushButton_send->setText("停止");rnrn rnrn else if(ui->lineEdit->text().isEmpty())rn rn SockSendData(m_buffer);rn rn rnrnrnvoid MainWindow::on_action_socket_close_triggered()rnrn .....rnrnrnvoid MainWindow::on_action_socket_open_triggered()rnrn......rnrnrnvoid MainWindow::on_action_exit_triggered()rnrn exit(0);rnrnrnvoid MainWindow::on_action_serverconfig_triggered()rnrn.....rnrnrnvoid MainWindow::on_action_default_triggered()rnrn....rnrnrnvoid MainWindow::on_action_rand_triggered()rnrn.....rnrnrnvoid MainWindow::on_action_define_triggered()rnrn.....rnrnrnvoid MainWindow::socketerror()rnrn.......rnrnrnvoid MainWindow::SockRecvData()rnrn QString qs_str;rn QByteArray qb_bay;rn char buf[64] = 0;rn qb_bay = p_socketet->readAll(); //虽然有信号但是读不到数据rn rn //qba_message.clear();rnrnrnrnvoid MainWindow::connectError()rnrn p_socketet->abort();rn ui->label_dissata->setText("错误:连接服务器失败!");rn ui->pushButton_send->setEnabled(false);rn return;rnrnrnvoid MainWindow::analyzingbuff(QByteArray qb_recvarrat)rnrn mag_stra.protocol_version = qb_recvarrat[0];rnrnrnrnrnrnrnrn//MYTHREAD头文件rnrn#ifndef MYTHREAD_Hrn#define MYTHREAD_Hrn#includern#include rnrnclass MyThread : public QThreadrnrn Q_OBJECTrnpublic:rn explicit MyThread(QObject *parent = 0);rn void run();rn void qrandcw(char *p_buff);rnrnrn QTcpSocket * p_sock;rn QString qrandcd_str;rn int sendnumber;rn char *p_senddata;rn bool b_threadsata;rn char buffer[8];rnrnsignals:rnpublic slots:rnrn;rnrn#endif // MYTHREAD_Hrnrnrnrnrn//mythread cpp文件rn#include "mythread.h"rn#include rnrnMyThread::MyThread(QObject *parent) :rn QThread(parent)rnrn b_threadsata = false;rn memset(buffer,0,sizeof(buffer));rn QSettings p_file;rn qrandcd_str =p_file.value("CW_DEFAULT/DEFAULT").toString();rnrnrnvoid MyThread::run()rnrn if (sendnumber == 0)rn return;rnrn for(int i = 0;i <8 ; i ++)rn rn buffer[i]=p_senddata[i];rn rnrn for(int i = 0;i <3 ; i ++)rn rn if(qrandcd_str.isEmpty())rn rn memset(buffer,0,sizeof(buffer));rn qrandcw(buffer);rn rnrn long int I =p_sock->write(buffer,sizeof(buffer));rn //p_sock->atEnd();rn // msleep(1000/1);rn if(I ==-1)rn return;rnrn rnrnrnvoid MyThread::qrandcw(char *p_buff)rnrn QString s_temp;rn bool ok=false;rn p_buff[0] = s_temp.setNum(qrand()%16).toAscii().toUInt(&ok,16);rn p_buff[1] = s_temp.setNum(qrand()%16).toAscii().toUInt(&ok,16);rn p_buff[2] = s_temp.setNum(qrand()%16).toAscii().toUInt(&ok,16);rn p_buff[4] = s_temp.setNum(qrand()%16).toAscii().toUInt(&ok,16);rn p_buff[5] = s_temp.setNum(qrand()%16).toAscii().toUInt(&ok,16);rn p_buff[6] = s_temp.setNum(qrand()%16).toAscii().toUInt(&ok,16);rnrnrnrn//还有其他几个实现对话框的文件(无关紧要)rnrnrn[/code]rnrnrnrn
QT之TCP实现客户端接收和发送数据
利用QT实现客户端的接收和发送数据.............................................................................................
相关热词 c#检测非法字符 c#双屏截图 c#中怎么关闭线程 c# 显示服务器上的图片 api嵌入窗口 c# c# 控制网页 c# encrypt c#微信网页版登录 c# login 居中 c# 考试软件