如何在C ++中通过套接字将向量从客户端传递到服务器 [英] How to pass vector from client to server over socket in c++

查看:71
本文介绍了如何在C ++中通过套接字将向量从客户端传递到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个结构来保存文件夹属性.

 typedef 结构
{
    字符串 FolderName;
     int  Foldersize;
     int  FolderFiles;
} DeskFold; 



然后,我使用结构矢量来收集桌面中存在的文件夹属性.
向量< DeskFold> MyVect;

现在,向量具有文件夹名称,其中的文件数和文件夹大小.

我想通过TCP/IP套接字连接将向量中的这些数据发送到服务器.
但是send()函数只能发送char *.

现在我要怎么做才能将这些数据发送到矢量?..

请帮助我...

解决方案

不能使用send命令一次发送向量.

您可以看到一个包含向量索引和数据的结构.
对所有矢量元素使用send()函数.

在使用send类型时,强制转换结构指针.

在接收方,应相应地构造矢量.


矢量和字符串是动态的.您不能通过硬拷贝实例驻留的内存来发送它们".
要发送它们,您必须在发送过程中序列化,在阅读过程中反序列化.
(请尝试使用Google进行"C ++数据序列化",以获取信息).

这本身并不是一个简单的概念,但是在陷入无限的困境之前,让我们尝试如何将这个特性变成一个字符*,然后停下来.

根据您之前的评论,无法想象像您这样的人在理解矢量和字符串之类的基本数据结构如何工作方面仍然存在问题,假装可以与联网机器和套接字通信一起使用.

您首先要了解动态数据结构如何工作(注意:数据结构不是struct的同义词),然后了解诸如序列化"和封送处理"之类的内容.并最终进行解析".

您也可以通过舒适的快捷键和很多co的内容来获得成功,但是编写您不理解的代码可能会愚弄一个人,但是您不可能一辈子都在愚弄任何人!


I created one structure to hold the folder properties.

typedef struct
{
    string FolderName;
    int Foldersize;
    int FolderFiles;
}DeskFold;



Then I used a structure vector to collect the folder properties present in desktop.
vector<DeskFold> MyVect;

Now the vector has folder name, number of files inside it and folder size.

I want to send these data in vector to server through TCP/IP socket connection.
But the send() function can send only the char *.

Now what I want to do to send those data in vector?..

Please help me...

解决方案

Sending the vector a once is not possible using send command.

You can came a structure which contains the vector index and the data.
Use the send() function for all vector elements.

while using send type cast the structure pointer.

On the receiving side constuct the vector accoringly.


Vectors and strings are dynamic. You cannot "send them" by hardcopying the memory the instance resides.
To send them you have to serialize in sending and deserialize in reading.
(Try Google for "C++ data serialization", to get information).

That''s not by itself an easy concept, but before lost yourself in an infinite wood of "let''s try how to cat this s*it into a char*", STOP a WHILE.

Basing on your previous comments, it not conceivable that someone like you, with still problem in understanding how the basic data-structures like vectors and strings work, pretend to work with networked machines and socket communication.

You''ve first to understand how dynamic data structure work (note: data structure is not a synonymous of struct) then understand things like "serialization" and "marshaling". And eventually "parsing".

You can also succeed with comfortable shortcuts and a lot of coy&paste, but writing a code you don''t understand may fool someone, but you cannot fool anyone for all of your life!


这篇关于如何在C ++中通过套接字将向量从客户端传递到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆