Tag: sendto

通过C中的套接字传递一个结构

我试图将整个结构从客户端传递到服务器,反之亦然。 让我们假设我的结构如下 struct temp { int a; char b; } 我正在使用sendto并发送结构variables的地址,并使用recvfrom函数在另一端接收它。 但是我无法获得在接收端发送的原始数据。 在sendto函数中,我将接收到的数据保存到struct temptypes的variables中。 n = sendto(sock, &pkt, sizeof(struct temp), 0, &server, length); n = recvfrom(sock, &pkt, sizeof(struct temp), 0, (struct sockaddr *)&from,&fromlen); 其中pkt是struct temptypes的variables。 尽pipe我正在接收8bytes的数据,但如果我尝试打印它只是显示垃圾值。 任何帮助解决它? 注: 不得使用第三方库。 编辑1:我对这个序列化的概念是新的。但是没有做序列化我不能通过套接字发送结构? 编辑2:当我尝试发送一个string或一个整数variables使用sendto和recvfrom函数我正在接收端的数据正确接收。 为什么不在一个结构的情况下? 如果我不必使用序列化函数,那么我应该单独发送结构的每个成员? 这实际上不是一个合适的解决scheme,因为如果有'n'个成员,那么为了发送或接收数据就会添加“n”行代码。