Jeg har kigget dit kode igennem paa kryds og tvaers men syntes ikke jeg kan faa mit til at broadcaste. Som sagt saa vil server og klient godt snakke sammen men kun en klient! Det ville vaere GULD vaerd hvis du hurtigt kunne skimte gennem mit kode og finde fejlen. Jeg har skrevet mit kode i en lille klasse. Her er headeren til klassen:
- #ifndef CLASS_SOCKET
- #define CLASS_SOCKET
-
- #pragma comment( lib, "Ws2_32.lib" )
-
- #define WIN32_LEAN_AND_MEAN
- #include <winsock2.h>
- #include <ws2tcpip.h>
-
- class CSocket
- {
- public:
- char Buffer[ 1024 ];
-
- CSocket( void );
- ~CSocket( void );
-
- bool Close( void );
- bool CreateBroadcast( const bool Server, const unsigned short Port, const char * IP );
- bool Created( void );
- bool Receive( void );
- bool Send( void );
-
- private:
- SOCKADDR_IN Addr;
- SOCKET Socket;
- unsigned short Port;
-
- bool SetOption( const int Level, const int Name, const char * Value, const int Length );
- bool SetupSocket( unsigned short Port );
- };
-
- #endif
og implementation i min cpp fil ser saaledes ud:
- #include "Socket.h"
-
- CSocket::CSocket( void )
- {
- for ( int I = 0; I < sizeof( Buffer ); I++ )
- Buffer[ I ] = 0;
-
- Port = 0;
- Socket = INVALID_SOCKET;
- }
-
- CSocket::~CSocket( void )
- {
- Close();
- }
-
- bool CSocket::Close( void )
- {
- if ( Socket != INVALID_SOCKET && closesocket( Socket ) == SOCKET_ERROR )
- {
- WSACleanup();
- return false;
- }
-
- WSACleanup();
- Socket = INVALID_SOCKET;
- return true;
- }
-
- bool CSocket::CreateBroadcast( const bool Server, const unsigned short Port, const char * IP )
- {
- if ( SetupSocket( Port ) )
- {
- if ( Server ) //Create server
- {
- Addr.sin_family = AF_INET;
- Addr.sin_port = htons( Port );
- Addr.sin_addr.s_addr = htonl( INADDR_ANY );
-
- //Bind the socket
- if ( bind( Socket, ( SOCKADDR * ) & Addr, sizeof( Addr ) ) != 0 )
- {
- Close();
- return false;
- }
- }
- else //Create client
- {
- Addr.sin_family = AF_INET;
- Addr.sin_port = htons( Port );
- Addr.sin_addr.s_addr = inet_addr( IP );
- }
-
- //Set broadcast option for client and server
- BOOL Flag = TRUE;
- if ( !SetOption( SOL_SOCKET, SO_BROADCAST, ( char * ) &Flag, sizeof( Flag ) ) )
- {
- Close();
- return false;
- }
- else
- return true;
- }
- else
- return false;
- }
-
- bool CSocket::Created( void )
- {
- if ( Socket == INVALID_SOCKET )
- return false;
- else
- return true;
- }
-
- bool CSocket::Receive( void )
- {
- int AddrSize = sizeof( Addr );
- if ( recvfrom( Socket, Buffer, sizeof( Buffer ), 0, ( SOCKADDR * ) & Addr, &AddrSize ) == SOCKET_ERROR )
- return false;
- else
- return true;
- }
-
- bool CSocket::Send( void )
- {
- if ( sendto( Socket, Buffer, sizeof( Buffer ), 0, ( SOCKADDR * ) & Addr, sizeof( Addr ) ) == SOCKET_ERROR )
- return false;
- else
- return true;
- }
-
- bool CSocket::SetOption( const int Level, const int Name, const char * Value, const int Length )
- {
- if ( setsockopt( Socket, Level, Name, Value, Length ) == SOCKET_ERROR )
- return false;
- else
- return true;
- }
-
- bool CSocket::SetupSocket( unsigned short Port )
- {
- if ( Port == 0 )
- return false;
-
- this->Port = Port;
-
- WSADATA Data;
- if ( WSAStartup( 0x0202, &Data ) != NO_ERROR )
- return false;
-
- Socket = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
- if ( Socket == INVALID_SOCKET )
- {
- WSACleanup();
- return false;
- }
-
- return true;
- }
og til sidst ser min main fil saaledes ud:
- #include "Socket.h"
-
- #include <stdio.h>
-
- int main( int argc, char * argv[] )
- {
- CSocket Socket;
-
- bool Server = true; //<-- True for server and false for client!
-
- if ( Socket.CreateBroadcast( Server, 5555, "192.50.13.255" ) )
- {
- bool B = !Server;
-
- if ( Server )
- printf( "Waiting for client...\n" );
-
- while( true )
- {
- if ( !B )
- {
- if ( Socket.Receive() )
- {
- printf( "Data received!\n" );
- if ( Server )
- B = true;
- }
- else
- {
- printf( "Error: %d\n", WSAGetLastError() );
- break;
- }
- }
- else
- if ( Socket.Send() )
- {
- printf( "Data sent!\n" );
- if ( !Server )
- B = false;
- }
- else
- {
- printf( "Error: %d\n", WSAGetLastError() );
- break;
- }
-
- Sleep( 1000 );
- }
- Socket.Close();
- }
- else
- printf( "Error: %d\n", WSAGetLastError() );
-
- return 0;
- }
Paa forhaand 1000 tak
Indlæg senest redigeret d. 04.01.2012 11:03 af Bruger #1474