Please send a minimal and complete code so that I can test it directly.
Which version of SFML are you using? Which OS?
SFML-Version 1.5, dynamic Lib
OS: Vista 32-bit Home Premium
Compiler: MSVC08, IDE: Codeblocks
This program is for testing purposes only, here's the complete Code:
#include <iostream>
#include <SFML\Network.hpp>
struct s_InfoPlayer
{
sf::Uint8 Address[16];
sf::Uint8 Nick[20];
void Clear() { memset( this, 0, sizeof(s_InfoPlayer) ); };
};
class CNetTest
{
private:
sf::SocketTCP m_Listener;
sf::SocketTCP m_ClientTCP;
sf::SocketUDP m_ClientUDP;
float m_Ping;
private:
bool GetClientInfo()
{
size_t iCount = 0;
sf::Uint32 iBuild = 0;
s_InfoPlayer PlayerInfo;
if (m_ClientTCP.Receive( (char*)&iBuild, sizeof(sf::Uint32), iCount) != sf::Socket::Done) { return 0; }
if (m_ClientTCP.Receive( (char*)&PlayerInfo, sizeof(s_InfoPlayer), iCount) != sf::Socket::Done) { return 0; }
std::cout << "Client Build " << iBuild << std::endl;
std::cout << "Added Client " << PlayerInfo.Nick << std::endl;
return 1;
};
bool SendClientInfo()
{
std::string sNick("NetTest");
std::string sAddress("127.0.0.1");
s_InfoPlayer PlayerInfo; PlayerInfo.Clear();
sNick.copy( (char*)&PlayerInfo.Nick[0], sizeof(PlayerInfo.Nick) );
sAddress.copy( (char*)&PlayerInfo.Address[0], sizeof(PlayerInfo.Address) );
m_ClientTCP.Send( (char*)&PlayerInfo, sizeof(s_InfoPlayer) );
std::cout << "Sent Client-Info" << std::endl;
return 1;
};
public:
CNetTest()
{
m_Ping = 0.1f;
std::cout << "Listening..." << std::endl;
m_Listener.Listen( 5100 );
m_Listener.Accept(m_ClientTCP, 0);
std::cout << "Host connected!" << std::endl;
};
void Receive()
{
if ( !GetClientInfo() ) { return; };
if ( !SendClientInfo() ) { return; };
bool bValid = 0;
int ErrorVal = 0;
sf::IPAddress Address;
unsigned short Port = 5200;
bValid = m_ClientUDP.IsValid();
m_ClientUDP.Unbind();
bValid = m_ClientUDP.Bind( Port );
ErrorVal = WSAGetLastError();
std::cout << "WSAGetLastError: " << ErrorVal << std::endl;
while (1)
{
sf::Packet DataPacket;
if (m_ClientUDP.Receive(DataPacket, Address, Port) != sf::Socket::Done)
{
return;
}
std::cout << "Got Data: " << DataPacket.GetDataSize() << " Bytes" << std::endl;
sf::Sleep( m_Ping );
}
};
~CNetTest() { m_Listener.Close(); m_ClientTCP.Close(); m_ClientUDP.Close(); };
};
int main()
{
CNetTest NetTest;
NetTest.Receive();
std::cout << std::endl << "Host disconnected..." << std::endl;
system("pause");
return 0;
};
And the UDP-Part:
void Receive()
{
if ( !GetClientInfo() ) { return; };
if ( !SendClientInfo() ) { return; };
bool bValid = 0;
int ErrorVal = 0;
sf::IPAddress Address;
unsigned short Port = 5200;
bValid = m_ClientUDP.IsValid();
m_ClientUDP.Unbind();
bValid = m_ClientUDP.Bind( Port );
ErrorVal = WSAGetLastError();
std::cout << "WSAGetLastError: " << ErrorVal << std::endl;
while (1)
{
sf::Packet DataPacket;
if (m_ClientUDP.Receive(DataPacket, Address, Port) != sf::Socket::Done)
{
return;
}
std::cout << "Got Data: " << DataPacket.GetDataSize() << " Bytes" << std::endl;
sf::Sleep( m_Ping );
}
};
After the bind I get this WinSock-Error:
WSAENOTSOCK 10038