【发布时间】:2009-09-15 02:01:12
【问题描述】:
我是一个新手程序员。我有一个问题如下,
void SockSend()
{
char *sendbuf;
int sendsize; /* send data size(variable size)*/
int iPos = 0, iTotSize;
char hdr;
char *data = "ABCDEFGHIJKLMNO"; /* its just example, data can be any thing */
sendsize = strlen(data);
hdr = '\0'; /* header character */
sendbuf = (char*)malloc(sendsize + 2);
sendbuf[iPos] = hdr;
iPos++;
strncpy(sendbuf + iPos, data, 15);
iPos += sendsize;
sendbuf[iPos] = '\0'; /* append null at end of string*/
iTotSize = strlen(sendbuf);
send(sockid, sendbuf, iTotSize, 0);
}
在上面的代码中,我需要发送带有标题字符的数据。 如果标题 ascii 字符介于 1h - ffh 之间,而不是 0h 可以正常工作。 我知道如果将 null 添加到字符串中,则将其视为字符串的结尾。 但我需要通过套接字发送带有数据的 NULL 字符。 谁能帮我解决这个问题。
提前谢谢你
【问题讨论】: