char avsluta = 'n';
char serverOrClient = '0';
char buffer[200] = "";
std::string message = "";
sf::IpAddress IP = sf::IpAddress::getLocalAddress();
unsigned short serverPort = sf::UdpSocket::AnyPort;
unsigned short clientPort = sf::UdpSocket::AnyPort;[/glow]
std::cout << "Your IP-address: " << IP << std::endl;
std::cout << "Server (s) or client (c)?: ";
std::cin >> serverOrClient;
bool done = false;
while (!done)
{
if (serverOrClient == 's')
{
sf::UdpSocket socket;
if (socket.bind(serverPort) != sf::Socket::Done)
{
std::cout << "server kunde inte binda till port\n\n";
}
else
{
std::cout << "Server - " << "port: " << socket.getLocalPort() << " IP-address: " << sf::IpAddress::getLocalAddress() << "\n\n";
}
std::cout << "\n\nSend message: ";
std::cin >> buffer;
if (socket.send(buffer, sizeof(buffer), IP, clientPort) != sf::Socket::Done)
{
std::cout << "Kunde inte senda! \n\n";
}
else
{
std::cout << "Message sent: '" << buffer << "' to client at port: " << clientPort << "\n\n";
}
}
else if (serverOrClient == 'c')
{
sf::UdpSocket socket;
if (socket.bind(clientPort) != sf::Socket::Done)
{
std::cout << "Kunde inte binda! \n\n";
}
else
{
std::cout << "Client - " << "port: " << socket.getLocalPort() << " IP-address: " << sf::IpAddress::getLocalAddress() << "\n\n";
}
std::size_t received;
if (socket.receive(buffer, sizeof(buffer), received, IP, serverPort) != sf::Socket::Done)
{
std::cout << "kunde inte receiva! \n\n";
}
else
{
std::cout << "Sender: " << IP << " || Port : " << serverPort << std::endl;
std::cout << "meddelande mottaget: " << buffer << std::endl;
}
}
std::cout << "avsluta? j/n: ";
std::cin >> avsluta;
if (avsluta == 'j')
{
done = true;
}
}
Hello! if i assign clientPort and serverPort manually with values like:
unsigned short serverPort = 4000;
unsigned short clientPort = 4001;
this code manages to send information from server to client.
but when i do this:
unsigned short serverPort = sf::UdpSocket::AnyPort;
unsigned short clientPort = sf::UdpSocket::AnyPort;
The server sends to port 0 and not the actuall clientPort.
So no message is reveived by the client.
why is this?