|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Feb 2002
Messaggi: 906
|
[c++] winsock gameplay
sto cercando/studiare di implementare un sistema per un gameplay.
Ne ho fatto uno con DirectPlay ma dato che directplay è deprecated dalla microsoft ho intenzione di farne uno con il winsock. il problema è che devo capire come il server invii i messaggi ricevuti da un client a tutti gli altri client (anche allo stesso client) con il proprio numero di ID. qualcuno ne sà qualcosa?? |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Te lo devi fare a mano, i messaggi te li devi replicare da solo... Solitamente si fa tramite UDP...
Devi tenerti una lista dei client e replicare il dato su tutti i client... |
![]() |
![]() |
![]() |
#3 | |
Senior Member
Iscritto dal: Feb 2002
Messaggi: 906
|
Quote:
con questo Codice HTML:
if(WSASendTo(SocketInfo->Socket, &(SocketInfo->DataBuf), 1, &SendBytes, 0, NULL,NULL, NULL, NULL)) Ho fatto in modo che 3 client inviano una stringa diversa, al server arrivano le 3 stringhe, ma il server smista la stringa solo al client che l'ha inviata. |
|
![]() |
![]() |
![]() |
#4 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Mi metti il for che replica il messaggio a tutti i client ?
|
![]() |
![]() |
![]() |
#5 | |
Senior Member
Iscritto dal: Feb 2002
Messaggi: 906
|
Quote:
è un pò lunghino cmq accetta le stringhe e le reinvia al client chiamante io voglio reinviare la stringa ricevuta dal server a tutti i client Codice HTML:
while(TRUE) { // Initialize the Read and Write socket set. FD_ZERO(&Reader); FD_ZERO(&Writer); // Check for connection attempts. FD_SET(ListenSocket, &Reader); // Set Read and Write notification for each socket based on the // current state the buffer. for (i = 0; i < TotalSockets; i++) if (SocketList[i]->RecvBytes > SocketList[i]->SendBytes) FD_SET(SocketList[i]->Socket, &Writer); else FD_SET(SocketList[i]->Socket, &Reader); if ((Total = select(0, &Reader, &Writer, NULL, NULL)) == SOCKET_ERROR) { printf("select function returned with error %d\n", WSAGetLastError()); return; } // Check for arriving connections on the listening socket. if (FD_ISSET(ListenSocket, &Reader)) { Total--; if ((AcceptSocket = accept(ListenSocket, NULL, NULL)) != INVALID_SOCKET) { // Set the accepted socket to non-blocking mode so the server will // not get caught in a blocked condition on WSASends NonBlock = 1; if (ioctlsocket(AcceptSocket, FIONBIO, &NonBlock) == SOCKET_ERROR) { printf("ioctlsocket() failed with error %d\n", WSAGetLastError()); return; } if (CreateSocketInformation(AcceptSocket) == FALSE) return; } else { if (WSAGetLastError() != WSAEWOULDBLOCK) { printf("accept() failed with error %d\n", WSAGetLastError()); return; } } } // Check each socket for Read and Write notification for Total number of sockets for (i = 0; Total > 0 && i < TotalSockets; i++) { LPSOCKET_INFORMATION SocketInfo = SocketList[i]; // If the Reader is marked for this socket then this means data // is available to be read on the socket. if (FD_ISSET(SocketInfo->Socket, &Reader)) { Total--; SocketInfo->DataBuf.buf = SocketInfo->Buffer; SocketInfo->DataBuf.len = BUFFERSIZE; Flags = 0; if (WSARecv(SocketInfo->Socket, &(SocketInfo->DataBuf), 1, &RecvBytes, &Flags, NULL, NULL) == SOCKET_ERROR) { if (WSAGetLastError() != WSAEWOULDBLOCK) { printf("Receive failed with error\n"); FreeSocketInformation(i); } continue; } else { SocketInfo->RecvBytes = RecvBytes; printf("Ricevuto %s\n", SocketInfo->DataBuf.buf); SocketInfo->DataBuf.buf="Fratello"; // If zero bytes are received, this indicates connection is closed. if (RecvBytes == 0) { FreeSocketInformation(i); continue; } } SocketInfo->DataBuf.buf = SocketInfo->Buffer + SocketInfo->SendBytes; SocketInfo->DataBuf.len = SocketInfo->RecvBytes - SocketInfo->SendBytes; } // If the Writer is marked on this socket then this means the internal // data buffers are available for more data. if (FD_ISSET(SocketInfo->Socket, &Writer)) { Total--; // lstrcpy(SocketInfo->DataBuf.buf,"Fratello"); // SocketInfo->SendBytes = 10; SocketInfo->DataBuf.buf = SocketInfo->Buffer + SocketInfo->SendBytes; SocketInfo->DataBuf.len = SocketInfo->RecvBytes - SocketInfo->SendBytes; SocketInfo->DataBuf), 1, &SendBytes, 0, if(WSASendTo(SocketInfo->Socket, &(SocketInfo->DataBuf), 1, &SendBytes, 0, (struct sockaddr *)&InternetAddr,sizeof(struct sockaddr_in), NULL, NULL)) { if (WSAGetLastError() != WSAEWOULDBLOCK) { printf("Send failed with error\n"); FreeSocketInformation(i); } continue; } else { SocketInfo->SendBytes += SendBytes; if (SocketInfo->SendBytes == SocketInfo->RecvBytes) { SocketInfo->SendBytes = 0; SocketInfo->RecvBytes = 0; } } } } } } |
|
![]() |
![]() |
![]() |
#6 |
Senior Member
Iscritto dal: Feb 2002
Messaggi: 906
|
up
|
![]() |
![]() |
![]() |
#7 |
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Scusa per la risposta tardiva, ma ero in vacanza.
La select mi sembra ad occhio utilizzata bene... Io prima proverei a fare esperimenti con la select e poi a sviluppare il codice per il tuo gioco... Coimincia con qualcosa di semplice...tipo la ricezione, la replica e la trasmissione di stringhe di testo... |
![]() |
![]() |
![]() |
#8 | |
Senior Member
Iscritto dal: Feb 2002
Messaggi: 906
|
Quote:
questo è il post: http://www.hwupgrade.it/forum/showthread.php?t=1251042 se lo vuoi leggere... non riesco a stampare per es.: nella FD_READ forse perchè sono partito con un progetto che inizializza una finestra non modale tipo DIalogBox... mha...! cmq se sai come fare per stampare in quella posizione del ciclo dei messaggi sarebbe utile in quanto il progettino è niente male. Questo progettino mi è servito come impalcatura infatti mettere il tutto in un progetto dx win32... e provare a vedere come si comporta inviando le stringhe in modo continuativo e non come una chat che è ora... cmq lo scopo è raggiunto + o -.... |
|
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 18:51.