如何通过tcp发送unsigned char? [英] How to send unsigned char over tcp ?
本文介绍了如何通过tcp发送unsigned char?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要帮助在unix中通过tcp发送无符号字符
但是这行中的大小写错误
i need help to send unsigned character over tcp in unix
but it case error in this line
void Write(char * a,int size)
{
unsigned char dt []=a;// erorr
write(Socket,a,2); // error
}
推荐答案
只需将文件描述符arr
和size
传递给写
函数。只要count参数指定缓冲区中包含的有效字节数,缓冲区指针的类型就不在乎了。另请参见 man 2 write [ ^ ]。
write函数只传输数据。它不关心传输什么类型的数据。因此,第二个参数的类型是void *
。
Just pass the file descriptor,arr
andsize
to thewrite
function. The type of the buffer pointer does not care as far as the count parameter specifies a valid number of bytes contained in the buffer. See also man 2 write[^].
The write function just transfers data. It does not care what kind of data are transferred. For that reason the type of the second parameter isvoid *
.
在内部,有符号和无符号类型是相同的。你发送的char只是8位数据,就像unsigned char一样。因此,
Internally, a signed and an unsigned type are the same. The char you send is just 8 bits of data, just like an unsigned char. Therefore,
void Write(char * arr,int size)
{
unsigned char* dt = (unsigned char*)a; // Cast to the type you want
write(Socket,dt,size);
}
将发送与
will send exactly the same data as
void Write(char * arr,int size)
{
write(Socket,arr,size);
}
我暂时解决这个问题
i solve it temporary like this
void WriteU(unsigned char buff[], int size)
{
int h ;
h = write( m_Client->Socket,buff,size);
// printf("\n data write \n %i",h);
}
如果有其他方式我为什么厌倦了自己?
why i tired my self if there is another way
这篇关于如何通过tcp发送unsigned char?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文