Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
if(FD_ISSET(m_acceptSocket, &fdSetRead)) { newConnection.socket = accept(m_acceptSocket, (SOCKADDR*)&newConnection.addr, &addrsize); freeClientSlot = getFreeClientSlot(); if(freeClientSlot == -1) continue; m_Client[freeClientSlot] = newConnection; m_Client[freeClientSlot].used = true; return 1;*/ } |
Zitat von »"MSDN"«
C-/C++-Quelltext
1 2 3 4 5 6 7 int select( __in int nfds, __inout fd_set* readfds, __inout fd_set* writefds, __inout fd_set* exceptfds, __in const struct timeval* timeout );
The parameter readfds identifies the sockets that are to be checked for readability. If the socket is currently in the listen state, it will be marked as readable if an incoming connection request has been received such that an acceptis guaranteed to complete without blocking. For other sockets, readability means that queued data is available for reading such that a call to recv, WSARecv, WSARecvFrom, or recvfrom is guaranteed not to block.
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Client newConnection; int addrsize = sizeof(SOCKADDR_IN); int freeClientSlot; if(FD_ISSET(m_acceptSocket, &fdSetRead)) { newConnection.socket = accept(m_acceptSocket, (SOCKADDR*)&newConnection.addr, &addrsize); freeClientSlot = getFreeClientSlot(); if(freeClientSlot != -1) { m_Client[freeClientSlot] = newConnection; m_Client[freeClientSlot].used = true; } } |
Zitat von »"Nox"«
listen muss nur einmal gestartet werden, aber du rufst in dem Code nie startWinsocket auf.
C-/C++-Quelltext |
|
1 |
Server () { startWinsocket(); } |
Zitat von »"Nox"«
Auch kannst du das vereinfachen:
C-/C++-Quelltext
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Client newConnection; int addrsize = sizeof(SOCKADDR_IN); int freeClientSlot; if(FD_ISSET(m_acceptSocket, &fdSetRead)) { newConnection.socket = accept(m_acceptSocket, (SOCKADDR*)&newConnection.addr, &addrsize); freeClientSlot = getFreeClientSlot(); if(freeClientSlot != -1) { m_Client[freeClientSlot] = newConnection; m_Client[freeClientSlot].used = true; } }
Zitat von »"Nox"«
Außerdem würde ich nur auf die Fehlercodes prüfen und solche Sachen wie if(ret < 0) eher verzichten, aber das ist nur eine persönliche Meinung
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 |
int Server::process () { unsigned int x, ret; fd_set fdSetRead; FD_ZERO(&fdSetRead); FD_SET(m_acceptSocket, &fdSetRead); // ... |
Werbeanzeige