Hi, I'm new to network programming and I was trying to get working a little test. I'm planning to write a small game which I can play with a friend without any central server between. Neither a test with UDP nor with TCP is working for me. I'm testing it locally on my computer.
This is my code for the server, who receives something in this case:
#include <iostream>
#include <stdio.h>
#include <SFML/Network.hpp>
int main(int argc, char **argv)
{
sf::IPAddress Address = sf::IPAddress::GetPublicAddress();
std::cout<<"Share Your IP and press enter: "<<Address.ToString()<<std::endl;
std::cin.get();
sf::SocketUDP Socket;
std::cout<<"Bind Socket\n";
if(!Socket.Bind(4567))
{
std::cout<<"Error!\n";
exit(1);
}
char Buffer[20];
std::size_t Received;
sf::IPAddress Sender;
u16 Port;
std::cout<<"Receive...\n";
if(Socket.Receive(Buffer, sizeof(Buffer), Received, Sender, Port)!=sf::Socket::Done)
{
std::cout<<"Error!\n";
exit(1);
}
std::cout<<Sender<<":"<<Port<<std::endl;
std::cout<<Buffer<<std::endl;
Socket.Close();
return 0;
}
And my client, which sends "Hello, World!":
#include <iostream>
#include <stdio.h>
#include <SFML/Network.hpp>
int main(int argc, char **argv)
{
char ip[32];
std::cout<<"Type the ip address: ";
gets(ip);
sf::IPAddress Address(ip);
std::cout<<"You'll send to: "<<Address.ToString()<<std::endl;
sf::SocketUDP Socket;
char Buffer[] = "Hello, World!";
std::cout<<"Send Text\n";
if(Socket.Send(Buffer, sizeof(Buffer), Address, 4567)!=sf::Socket::Done)
{
std::cout<<"Error Sending Text!\n";
exit(1);
}
Socket.Close();
return 0;
}
I tried connected using the public ip as well as using the local ip. I start the server, outputting "Receive..." with no end, and the client. The client doesn't output any text and seem to terminate regularly. My message seems to be sent correctly, but the server doesn't receive anything and I don't get any notify by the Firewall or something.
Anybody an idea?