[WinSock] Problem z przesyłaniem danych w pętli
Ostatnio zmodyfikowano 2013-01-15 08:50
wojt91 Temat założony przez niniejszego użytkownika |
[WinSock] Problem z przesyłaniem danych w pętli » 2013-01-15 08:50:05 Witam Mam taki problem do gry próbuje dopisać multi. Wszystko ładnie działa gra wysyła dane na serwer serwer rozsyła je do wszystkich użytkowników ale po przesłaniu kilkuset pakietów (zależnie od ilości klientów dla dwóch rozsyła około 1000 paczek do każdego) klient przestaje rozsyłać albo serwer przestaje odbierać. #pragma comment(lib, "Ws2_32.lib")
#include <WinSock.h> #include <Windows.h> #include <iostream>
using namespace std;
SOCKADDR_IN addr; SOCKET sListen; SOCKET sConnect; SOCKET * Connections;
int addrlen = sizeof( addr ); int ConCounter = 0; const int dl = 50;
struct Bufor { int ID; char wiadomosc[ dl ]; };
int ServerThread( int ID ) { int i = 0; Bufor bufor; char * Recv = new char[ dl ]; ZeroMemory( Recv, dl ); char * Send = new char[ sizeof( Bufor ) ]; ZeroMemory( Send, dl ); for(;; Sleep( 20 ) ) { if( recv( Connections[ ID ], Recv, dl, NULL ) ) { bufor.ID = ID; i++; cout << i << " "; cout << bufor.ID; memcpy( & bufor.wiadomosc, Recv, dl ); memcpy( Send, & bufor, sizeof( Bufor ) ); for( int a = 0; a <= ConCounter; a++ ) { send( Connections[ a ], Send, sizeof( Bufor ), NULL ); } ZeroMemory( Recv, dl ); } } return 0; }
int InitWinSock() { int RetVal = 0; WSAData wsaData; WORD DllVersion = MAKEWORD( 2, 1 ); RetVal = WSAStartup( DllVersion, & wsaData ); return RetVal; }
int main() { int RetVal = 0; RetVal = InitWinSock(); if( RetVal != 0 ) { MessageBoxA( NULL, "Winsock startup failed", "Error", MB_OK | MB_ICONERROR ); exit( 1 ); } Connections =( SOCKET * ) calloc( 64, sizeof( SOCKET ) ); sListen = socket( AF_INET, SOCK_STREAM, NULL ); sConnect = socket( AF_INET, SOCK_STREAM, NULL ); addr.sin_addr.s_addr = inet_addr( "127.0.0.1" ); addr.sin_port = htons( 1234 ); addr.sin_family = AF_INET; bind( sListen,( SOCKADDR * ) & addr, sizeof( addr ) ); listen( sListen, 64 ); for(;; ) { if( sConnect = accept( sListen,( SOCKADDR * ) & addr, & addrlen ) ) { Connections[ ConCounter ] = sConnect; char * ID = new char[ 64 ]; ZeroMemory( ID, sizeof( ID ) ); itoa( ConCounter, ID, 10 ); send( Connections[ ConCounter ], ID, sizeof( ID ), NULL ); ConCounter = ConCounter + 1; CreateThread( NULL, NULL,( LPTHREAD_START_ROUTINE ) ServerThread,( LPVOID )( ConCounter - 1 ), NULL, NULL ); } } WSACleanup(); return 0; }
gry nie będę wklejał bo zbyt długa jest ale działa na zasadzie while(!al_key_down(&key_state,ALLEGRO_KEY_ESCAPE)) { Wyslij(); Odbierz(); //pozostałe rzeczy gry } gra napisana jest w allegro 5.0.7. Proszę o pomoc, jakiekolwiek sugestie |
|
« 1 » |