PDA

View Full Version : [c++] winsock gameplay


okay
20-07-2006, 13:26
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??

cionci
20-07-2006, 18:16
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...

okay
21-07-2006, 10:45
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...


con questo

if(WSASendTo(SocketInfo->Socket, &(SocketInfo->DataBuf), 1, &SendBytes, 0, NULL,NULL, NULL, NULL))


tramite ciclo for trasmetto sempre al client che ha inviato il suo messaggio.

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.

cionci
21-07-2006, 11:03
Mi metti il for che replica il messaggio a tutti i client ?

okay
21-07-2006, 11:10
Mi metti il for che replica il messaggio a tutti i client ?


è 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




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;
}
}
}
}

}
}

okay
22-07-2006, 06:38
up

cionci
31-07-2006, 19:21
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...

okay
31-07-2006, 19:30
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...

si ci sono riuscito...

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 -....