小编Sat*_*iya的帖子

QT套接字不读取所有数据

我想通过Qt中的socket读取数据.我正在使用QBytearray来存储数据.实际上服务器在一个延伸中发送4095个字节,但在QT客户端,由于我的应用程序设计,我收到了不同的块.

void Dialog::on_pushButton_clicked()
{
socket=new QTcpSocket(this);
socket->connectToHost("172.17.0.1",5000);
if(socket->waitForConnected(-1))
qDebug()<<"Connected";
Read_data();
}

void Dialog::Read_data()
{
QString filename(QString("%1/%2.bin").arg(path,device));
qDebug()<<"filename"<<filename;
QFile fileobj(filename);

int cmd,file_size,percentage_completed;
if(!fileobj.open(QFile::WriteOnly | QFile::Text))
{
    qDebug()<<"Cannot open file for writting";
    return;
}
QTextStream out(&fileobj);
while(1)
{

socket->waitForReadyRead(-1);
byteArray=socket->read(4);
qDebug()<<"size of bytearray"<<byteArray.size();

length=0xffff & ((byteArray[3]<<8)|(0x00ff & byteArray[2]));
int rem;
byteArray=socket->read(length);
while(byteArray.size()!=length)
{    

rem=length-byteArray.size();

byteArray.append( socket->read(rem));

}
fileobj.write(byteArray);
fileobj.flush();
byteArray.clear();
}
}
Run Code Online (Sandbox Code Playgroud)

服务器代码:

#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/ioctl.h>
#include<mtd/mtd-user.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <errno.h> …
Run Code Online (Sandbox Code Playgroud)

sockets qt

3
推荐指数
1
解决办法
4288
查看次数

从 C 包含并编译 C++ 头文件

我有一个 C++ 文件及其头文件。我需要在 C 代码中包含这个头文件并使用其中的函数。

cpp.h通过 编译文件时main.c,由于 C++ 链接,编译失败。

关于使用宏__cplusplus streamstring没有解决,是否有某种方法可以编译文件cpp.h并执行?

我只给出了我的代码的概要。

C++头文件cpp.h

struct s1
{
string a;
string b;
};
typedef struct s1 s2;
class c1
{
public:
void fun1(s2 &s3);
private: 
fun2(std::string &x,const char *y);
};
Run Code Online (Sandbox Code Playgroud)

C++ 文件cpp.cpp

c1::fun1(s2 &s3)
{
 fstream file;

}

c1::fun2(std::string &x,const char *y)
{

}
Run Code Online (Sandbox Code Playgroud)

C文件main.c

#include "cpp.h"
void main()
{
 c1 c2;
 s1 structobj;
 c2.fun1(&structobj);
 printf("\n …
Run Code Online (Sandbox Code Playgroud)

c c++

2
推荐指数
1
解决办法
2万
查看次数

关闭客户端套接字并保持服务器套接字活

我正在使用TCP套接字建立服务器 - 客户端连接.每当我关闭客户端套接字时,我的服务器也会关闭.但我希望只关闭我的客户端,我的服务器必须等待下一次accept().

服务器端:

{
bind(lfd,(struct sockaddr*)&serv_addr, sizeof(serv_addr));
listen(lfd, 10);
   while(1)
   {
        cfd = accept(lfd, (struct sockaddr*)NULL, NULL);
   //server must wait here after client closes the connection application code
        close(lfd);
    }
}
Run Code Online (Sandbox Code Playgroud)

客户端:

inet_pton(AF_INET, argv[1], &serv_addr.sin_addr);
connect(fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
// ... application code
if(c == 1)
   close(fd);
Run Code Online (Sandbox Code Playgroud)

c sockets

1
推荐指数
2
解决办法
2万
查看次数

在 C 中递增和递减多个数据类型的 malloced 数组时遇到问题

在我的计算机科学课程中,我们学习了一种将值存储在 malloced 数组的第 0 个元素中的方法,然后递增该数组,以便诸如数组大小之类的内容可以存储在该元素中并在以后检索。我曾尝试使用此方法的修改版本在这些递增元素中存储各种数据类型。

以下是如何创建此类数组的示例:

int *array; 
array = malloc(sizeof(int) + sizeof(double) + (n * sizeof(int)))
*(array) = n;
array++;
(double*)array++;
return array;
Run Code Online (Sandbox Code Playgroud)

在这个例子中,malloc 语句中的sizeof(int)andsizeof(double)是用来存储东西的元素,比如 int 元素中数组的大小,而在 double 元素中我们可以存储一些东西,比如数组中所有数字的平均值(当然不包括这两个元素)

(n * sizeof(int))用于创建数组中的其余元素,其中n是元素数,sizeof(int)是这些元素所需的数据类型,理论上这应该适用于任何数据类型的数组。

现在,这是我遇到的麻烦:

我创建了另一个函数来检索数组的大小,但是我在递减和递增数组时遇到了问题。这是我的代码:

getArraySize(void* array){
(double*)array--;//Decrement past the double element
(int*)array--;//Decrement past the int element

int size = *((int*)array);//Acquire size of the array

(int*)array++;//Increment past int element
(double*)array++;//Increment past the double element

return size;}
Run Code Online (Sandbox Code Playgroud)

这个函数无法获取数组的大小,我意识到这是因为编译器首先增加数组然后类型转换它。但是,当我尝试修复这样的增量/减量语句时:

((int*)array)++;
Run Code Online (Sandbox Code Playgroud)

我收到一个错误,说 …

c arrays casting increment

1
推荐指数
1
解决办法
54
查看次数

fwrite()返回值与文件大小不匹配

fwrite()用来在循环中将数据写入文件.它重复了40次,但在fwrite()最后几个KB中没有写入文件.连续fwrite()工作正常.当我运行单个块时,4000字节fwrite()返回4000,但文件内容小于4000(接近3000字节).同时也没有报告错误perror().请帮助我,并提前感谢.

c fwrite

0
推荐指数
1
解决办法
559
查看次数

标签 统计

c ×4

sockets ×2

arrays ×1

c++ ×1

casting ×1

fwrite ×1

increment ×1

qt ×1