【发布时间】:2021-12-19 16:55:45
【问题描述】:
我无法确定套接字,我只是向服务器询问某个位置的数据 (glm::i64vec4) 并期待响应
但是当我得到响应和数据时,位置就会偏离位置反映了这一点(也就是我的体素游戏看起来很酷但没用)
可能只是我不了解套接字,或者这个库可能有些奇怪
我的一个想法是这可能与服务器和客户端上不匹配的阻塞和非阻塞有关
但是当我将服务器切换到阻塞状态时(并将每个客户端放在一个单独的线程中,并与接受进程分开),它什么也没做
如果我在做一些非常愚蠢的事情,请告诉我我对套接字几乎一无所知
这是一些可能看起来很糟糕的代码
服务器代码
std::deque <CActiveSocket*> clients;
CPassiveSocket socket;
socket.Initialize();
socket.SetNonblocking();//I'm doing this so i don't need multiple threads for clients
socket.Listen("0.0.0.0",port);
while (1){
{
CActiveSocket* c;
if ((c = socket.Accept()) != NULL){
clients.emplace_back(c);
}
}
for (CActiveSocket*& c : clients){
c->Receive(sizeof(glm::i64vec4));
if (c->GetBytesReceived() == sizeof(glm::i64vec4)){
chkpkt chk;
chk.pos = *(glm::i64vec4*)c->GetData();
LOOP3D(chksize+2){
chk.data(i,j,k).val = chk.pos.y*chksize+j;
chk.data(i,j,k).id=0;
}
while (c->Send((uint8*)&chk,sizeof(chkpkt)) != sizeof(chkpkt)){}
}
}
}
客户代码
//v is a glm::i64vec4
//fsock is set to Blocking
if(fsock.Send((uint8*)&v,sizeof(glm::i64vec4)))
if (fsock.Receive(sizeof(chkpkt))){
tthread::lock_guard<tthread::fast_mutex> lock(wld->filemut);
wld->ichks[v]=(*(chkpkt*)fsock.GetData()).data;//i tried using the position i get back from the server to set this (instead of v) but that made it to where nothing loaded
//i checked it and the chunks position never lines up with what i sent
}
【问题讨论】: