【发布时间】:2013-10-11 18:08:07
【问题描述】:
我正在尝试用 C++ 编写一个程序,该程序将接受来自服务器上客户端的消息。但首先客户端发送消息的 size 并使用此值,服务器将创建一个字符数组来存储消息最终发送时。当我尝试使用消息大小值初始化数组时,编译器说存在错误,因为 messageSize 整数必须是一个常量值 - 我想知道为什么会这样,因为据我所知,初始化是完全可以接受的整数类型数组的长度:
//Deal with data in DNS style
int dnsStyle()
{
recv(clientSocket, bufferSize, 1, MSG_WAITALL);
return bufferSize[0];
}
//Communicate in the DNS style of addressing
char DNS()
{
int messageSize = dnsStyle();
printf("The message buffer has been tailoured to this size: '%d'", messageSize);
char bufferMessDNS[messageSize];
//Then recieve the actual message itself
recv(clientSocket, bufferMessDNS, messageSize, MSG_WAITALL);
//Then send the message back to client
send(clientSocket, bufferMessDNS, messageSize, 0);
//std::string returnMess = "OK";
//send(clientSocket, sendBack.c_str(), sendBack.size(),0);
}
【问题讨论】: