我有一个dll挂钩网络应用程序的recv功能。代码工作得很好(它使所有内容都被禁止),但是如果我将输出日志添加到文件中,连接会在一段时间后关闭(服务器端应用程序抛出错误“现有连接被远程强制关闭主办”)。
那个时间甚至不一样,有时几乎在初始化连接时关闭,有时我可以在关闭之前使用连接几秒钟。它不会给出任何错误或警告消息。如果我删除日志代码,应用程序运行正常。知道为什么会这样吗?我在Windows 8 x64中运行它
此外,即使删除日志代码,连接也会在windows xp x32中保持关闭状态。
这是recv钩子代码:
int __stdcall NewRecv(SOCKET socket, char *data, int datalen, int flags) {
int result = 0;
if(!IsLoginServerPacket(&socket)) {
INT size = 0,opcode = 0,temp = 0,writer = 0,second_op = 0;
do {
size = 0;
second_op = 0;
temp = 0;
writer = 0;
while(temp < 2) {
temp += recvPacket(socket,recv_gs_buffer+writer,2 - temp,flags);
writer += temp;
}
size = (*(SHORT*)recv_gs_buffer) & 0xffff;
// THIS IS THE LOG CODE
FILE *f = fopen("debug.txt", "a");
fprintf(f, "datalen=%d, size=%d\n", datalen, size);
fclose(f);
while(temp < size) {
temp += recvPacket(socket,recv_gs_buffer+writer,size - temp,flags);
writer += temp;
}
Decrypt(&gs_crypt,recv_gs_buffer+2,size-2);
opcode = (*(recv_gs_buffer+2) & 0xff);
if(opcode == EXTENDED_PROTOCOL) {
second_op = *(SHORT*)(recv_gs_buffer + 3);
second_op &= 0xffff;
HandleGameServerPacket(second_op,recv_gs_buffer+2,size-2);
}
} while(second_op == 0x8a || second_op == 0x8b);
if(opcode == 0x00) {
SetKey(recv_gs_buffer+4,&gs_crypt);
SetKey(recv_gs_buffer+4,&client_crypt);
} else
Crypt(&client_crypt,recv_gs_buffer+2,size-2);
int i = 0;
while(i < size) {
data[i] = recv_gs_buffer[i];
i++;
}
//memcpy(data,recv_gs_buffer,size);
result = size;
} else
result = recvPacket(socket,data,datalen,flags);
return result;
}
答案 0 :(得分:0)
我刚刚发现了问题及其解决方案。
注入的应用程序正在非阻塞模式下配置套接字。任何一点延迟都使它抛出WSAEWOULDBLOCK(10035错误代码)。我必须做的就是修复它,如果我收到任何错误,请重试recv请求
INT val = 0;
while(temp < 2) {
val = recvPacket(socket,recv_gs_buffer+writer,2 - temp,flags);
if(val > 0) {
temp += val;
writer += temp;
}
}
并且
val = 0;
while(temp < size) {
val = recvPacket(socket,recv_gs_buffer+writer,size - temp,flags);
if(val > 0) {
temp += val;
writer += temp;
}
}