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?
It's working for me (at least if you fix the "std<<cout" in client.cpp :P). Are you sure that you use localhost or 127.0.0.1 as an ip if you're testing in your home network? (Client and the server on the same computer).
I tried the local ip returned by the function, and the public one. All on one computer, withouth success. (the "std<<cout" thing is corrected now :roll: )
I have 'ported' it from UDP to TCP now, but still there is everything working. When using a single computer, or two here at home, using the local ip address, the networking works perfectly! But as soon as I share my public ip with anybody else and he connects to me (or vice versa), the connecting fails. I don't get any FireWall notify here (or if, I check 'allow').
This is my new code for the program (server+client):
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <SFML/Network.hpp>
sf::SelectorTCP Selector;
int main(int argc, char **argv)
{
if(argc==1) return 0;
if(!strcmp(argv[1], "-server")) // receive
{
sf::SocketTCP Listener;
sf::IPAddress Address = sf::IPAddress::GetLocalAddress();
std::cout<<"Local IP: "<<Address.ToString()<<std::endl;
Address = sf::IPAddress::GetPublicAddress();
std::cout<<"Public IP: "<<Address.ToString()<<std::endl;
//std::cout<<"Share Your IP and press enter: "<<Address.ToString()<<std::endl;
std::cout<<"Listen...\n";
if(!Listener.Listen(4567))
{
std::cout<<"Error.\n"; exit(1);
}
std::cout<<"Accept...\n";
sf::IPAddress ClientAddress;
sf::SocketTCP Client;
if(Listener.Accept(Client, &Address)!=sf::Socket::Done)
{
std::cout<<"Error.\n"; exit(1);
}
std::cout<<"Receive...\n";
char Buffer[128];
std::size_t Received;
if (Client.Receive(Buffer, sizeof(Buffer), Received) != sf::Socket::Done)
{
std::cout<<"Error.\n"; exit(1);
}
std::cout<<"Message: "<<Buffer<<std::endl;
Listener.Close();
}
else if(!strcmp(argv[1], "-client")) // send
{
sf::SocketTCP Client;
char ip[32];
std::cout<<"Type the ip address to connect: ";
gets(ip);
sf::IPAddress Address(ip);
std::cout<<"Connect...\n";
if(Client.Connect(4567, Address)!=sf::Socket::Done)
{
std::cout<<"Error!\n"; exit(1);
}
char Buffer[] = "Hello, World!";
if(Client.Send(Buffer, sizeof(Buffer))!=sf::Socket::Done)
{
std::cout<<"Error sending message!\n"; exit(1);
}
Client.Close();
}
return 0;
}