void NetworkServer::RunServer()//main server loop
{
while (flags & server_running)
{
sok client = listener.Accept();
if (listener && client.IsValid())
{
if (clients.size >= MaxClients)
{
client.Close();
continue;
}
ClientHandler* h = constructor->ConstructClient();
//ConstructClient() is just doing "new ClientHandler()";
h->com_stream.forceConnected(client);
h->id = client_ids.getId();
h->flags = client_active;
h->server = this;
this->HandleNewConnection(h);//nothing..
locker.Enter();
clients.add(h);//add client to the client array
h->threadRun();//stars the thread
locker.Leave();
}
else
{
break;
}
}
}
void tcpStream::forceConnected(sok& ss)
{
server.socket = ss.socket;
connected = true;
}
class sok
{
private:
SOCKET socket;
public:
inline sok()
: socket(INVALID_SOCKET)
{
}
inline sok(SOCKET s)
: socket(s)
{
}
inline sok(const sok & s)
: socket(s.socket)
{
}
inline operator bool()const
{
return (socket != INVALID_SOCKET);
}
inline ~sok()
{
Close();
}
inline bool IsValid()const
{
return (socket != INVALID_SOCKET);
}
inline void operator = (const sok & s)
{
socket = s.socket;
}
public:
inline void Close()
{
if (socket != INVALID_SOCKET)
{
closesocket(socket);
socket = INVALID_SOCKET;
}
}
inline sok Accept()
{
return sok(::accept(socket, 0, 0));
}
bool tcpClient(NetAddress& adr);
bool tcpServer(wtf::ushort port, wtf::u32 clients = 10);
private:
friend class tcpStream;
};
uint tcpStream::read(void* out, const uint size)
{
wtf::n32 r = ::recv(server.socket, (char*)out, size, 0);//this failes
//int e = WSAGetLastError();
connected = ((r) != (-1));
return ((uint)r);/**/
}
任何機會,你可以張貼一些示例代碼?這會讓我們更容易進行調試。我猜grub應該是'搶'? – 2010-06-21 12:43:26
它是最基本的服務器 SOCKER s = :: accept(lsock,0,0); Cient * c =新客戶; C-> RunThread(); //這裏的線程將被創建並啓動 – Raxvan 2010-06-21 12:45:32
您是否嘗試過這樣做沒有穿了一會兒......如果是這樣,你可以發佈工作的例子嗎? – 2010-06-21 12:52:09