来自UDP连接的第一个缓冲区包含垃圾-随后的连接是好的
First buffer from UDP connection contains junk - subsequent connections are fine
我真的要尽量避免在这里发布我的整个项目-除非它是必要的:)我有一个客户端/服务器程序,我正在编写客户端通过UDP端口连接服务器,并期望接收格式为"@7777~15~3701"
的数据包-我第一次连接,我在缓冲区中得到垃圾。然而,一旦客户端退出,我再次启动它,下一个缓冲区"@7777~15~3702"
就会正常通过。
int
RoutingManager::SendMessage(struct sockaddr_in toNode, char buffer[1024])
{
#if logging > 1
cout << "Sending: " << buffer << endl;
#endif
int n;
unsigned int length = sizeof(struct sockaddr_in);
//buffer = "@7777~15~3702"
n = sendto(mySocket, buffer, strlen(buffer),0,
(const struct sockaddr *)&toNode,length);
if (n < strlen(buffer))
perror("Sendto");
cout << "Sent: " << n << " bytes of datan";
}
. .我在这里传递的缓冲区是从
生成的://FIXED: This was the source of my issue it appears - a corrected
//implementation of this method has been included at the bottom.
char*
RoutingManager::GenerateConnectionString(struct Node n)
{
char buffer[512];
bzero(buffer,512);
sprintf(buffer, "@7777~15~%d", n.id);
cout << MYPORT << endl;
return buffer;
}
服务器输出:
Sending: @7777~15~3701
Sent: 1 bytes of data
Waiting for nodes...
客户端:RoutingNode::GetMyID()
{
int n;
char buffer[256];
bzero(buffer,256);
unsigned int length = sizeof(struct sockaddr_in);
struct sockaddr_in from;
n = sendto(mySocket,"!", strlen("!"),0,(const struct sockaddr *)&server,length);
if (n < 0)
perror("Sendto");
//once we have our information from the manager, let's hog some cpu
//remove this crap when stuff gets more reliable
fcntl(mySocket, F_SETFL, O_NONBLOCK);
while(buffer[0] != '@')
n = recvfrom(mySocket,buffer,256,0,(struct sockaddr *)&from, &length);
if (n < 0)
perror("recvfrom");
parser.ParseMessage(buffer, fromNode, messages);
}
解析消息时:
bool
RoutingMessage::ParseMessage(char* buffer, int &fromNode, map<int, string> &messages, const int MAX_CHARS_PER_LINE,
const int MAX_TOKENS_PER_LINE, const char* const DELIMITER)
{
#if logging > 1
cout << "Buffer: " << buffer << endl;
#endif
if (buffer[0] != '@')
{
perror("Buffer malformated!");
return false;
}
//remove the '@'
buffer++;
char buf[MAX_CHARS_PER_LINE];
strcpy(buf, buffer);
char* temp = strtok(buf, DELIMITER);
if (temp == NULL)
{
perror("Buffer malformated!");
return false;
}
fromNode = atoi(temp);
temp = strtok(NULL, DELIMITER);
vector<string> tokens;
while(temp != NULL)
{
string val(temp);
tokens.push_back(val);
temp = strtok(NULL, DELIMITER);
}
//store messages in the map: <message-type>, <message>
for (int i = 0; i < tokens.size(); i+=2)
messages.insert(pair<int, string>(atoi(tokens[i].c_str()), tokens[i+1]));
//all good
return true;
}
和输出结果,我得到垃圾:
Buffer: <junk-symbol>
Buffer malformated!: Success
Node: 0
但是当客户端断开连接时,我重新启动相同的可执行文件,我得到:
Buffer: @7777~15~3702
Node: 7777
Message Type: 15 Message: 3702
如我所料。有谁知道我能查什么吗?
修正方法——
void
RoutingManager::GenerateConnectionString(struct Node n, char* buffer)
{
bzero(buffer,512);
sprintf(buffer, "@7777~15~%d", n.id);
}
以上似乎解决了我的问题。
问题似乎是在GenerateConnectionString()
功能:
char*
RoutingManager::GenerateConnectionString(struct Node n)
{
char buffer[512];
....
return buffer;
}
返回指向局部变量的指针。所以返回的指针指向堆栈。因此,当程序使用该区域的堆栈时,数据将被损坏。
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 编译包含字符串的代码时遇到问题
- c++库的公共头文件中应该包含什么
- 将包含C样式数组的对象初始化为成员变量(C++)
- 是否需要删除包含对象的"pair"?
- 当套接字连接断开时检测C/C++Unix
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 使用mongocxx驱动程序时包含头文件问题
- 如何在h文件中包含.o对象文件
- 在混合代码库中将C转换为C++时出现许多包含错误
- VS2017,C++包含目录与附加包含目录,子文件夹包含失败-但为什么
- cmath抛出错误C2062、C2059、C2143和C2447.cmath包含在矢量文件中
- 为什么您需要C++头文件的包含保护
- CMake 错误"源似乎不包含 CMakeLists.txt",路径/库连接问题
- 如何在C++中将边列表转换为邻接矩阵,其中包含连接到顶点的边数而不是一条边?
- 包含路径的字符串连接
- 如果 else 语句连接到子字符串,则使用只会导致 else-语句仅包含用户输入的任何内容
- 来自UDP连接的第一个缓冲区包含垃圾-随后的连接是好的