C ++套接字 - 我只能发送字符? [英] C++ Sockets - Can i only send characters?

查看:126
本文介绍了C ++套接字 - 我只能发送字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用带有win32窗口的同步套接字,并使用send()和recv()函数通过互联网TCP发送数据;

I'm using synchronised sockets with a win32 window, and using the send() and recv() function to send data over internet TCP;

what i' m想知道,我将如何发送一些整数,甚至我自己的类/结构在tcp套接字?因为send()函数只允许我发送字符。

what i'm wondering, how would i send some integers or even my own class/structure over the tcp socket? because the send() function only lets me send characters.

我只需要发送字符,然后可以使用atoi()将它们转换为整数。或者如果我想发送一个类结构,我会发送很多字符串,然后将它们一个接一个地放入变量。

Would i just have to send characters and then maybe convert them to integer with atoi()? or if i wanted to send a class structure, would i send many strings over and then put them in the variables.. one by one.

推荐答案

它不是在文本意义上发送字符 - 它发送连续的字节数组,它引用使用char *。你可以指向这种方式的任何值类型的字节,所以如果你想发送一个int,

It isn't sending characters in the textual sense - it's sending contiguous arrays of bytes, which it refers to using a char*. You can point to the bytes of any value type this way, so if you wanted to send an int,

int A = 5;
const char* pBytesOfA = (const char*)&A;
int lengthOfBytes = sizeof(A);

send(socket, pBytesOfA, lengthOfBytes, flags);

这篇关于C ++套接字 - 我只能发送字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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