I am getting thoroughly confused. I am trying to make a (semi-simple) chat program, but it seems random which application it chooses to send it to (when multiple are open) (usually the second one opened, sometimes the first). It also doesn't necessarily send it to one in server mode, even when I changed the 'to server' and 'to client' ports! I have been (mostly) trying this on one computer. When I opened the server on one computer and a client on another, I sent a message and it gave me the error message (on the server). Now when I open another on the server computer and send something, it sends it to itself, A LOT. It doesn't give me the error, either. Can someone point out where in my code I am going wrong?
(By the way, I already know what happens when a message is sent while you are typing, I plan on moving to a normal window later.)
//Includes
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
//Namespaces
using namespace std;
using namespace sf;
//Constants
const int CONNECT = 1;
const int HOST = 2;
const int UNKNOWN = 3; //This part will be implemented later.
//Classes
class client_receive : public sf::Thread
{
public:
bool receiving;
sf::Packet packet;
sf::IPAddress sender;
sf::SocketUDP socket;
string buffer;
unsigned short port;
private:
virtual void Run()
{
while (receiving == true)
{
if (socket.Receive(packet,sender,port) != sf::Socket::Done)
{
cout << "Odd... It seems there was an error receiving the message.\n";
}
string buffer;
packet >> buffer;
cout << buffer << "\n";
packet.Clear();
}
}
};
int main()
{
cout << "Welcome to the SGM Chat! Please wait for the program to load...\n";
cout << "CAUTION: Program uses UDP, so some messages may not be received.\n\n";
sf::SocketUDP socket;
if (!socket.Bind(54358))
{
cout << "ERROR BINDING SOCKET\n";
}
sf::IPAddress ip = sf::IPAddress::GetLocalAddress();
bool answ_ques = false;
string answ_to;
int answ_res;
while (answ_ques == false)
{
cout << "Would you like to:\n";
cout << "\tA. Connect to a Server\n";
cout << "\tB. Host a Server\n";
getline(cin,answ_to);
if (answ_to == "A" or answ_to == "a")
{
cout << "\nWell goody! Please enter the IP address.\n";
answ_res = CONNECT;
answ_ques = true;
}
else if (answ_to == "B" or answ_to == "b")
{
cout << "\nTell people on your network to connect to " << ip << ", if they are in your network. You can go to whatismyip.org to get your external IP address, but using that you will probably need to port forward.\n\n";
answ_res = HOST;
answ_ques = true;
}
else
{
cout << "\nUh, you need to pick one of the two choices. Try READING them this time.\n\n";
answ_res = UNKNOWN;
}
}
if (answ_res == HOST)
//Host will be separated later.
{
cout << "Open another window if you want to talk, depending on how busy your server is you might not get to read much.\n\n";
//sf::SelectorUDP selector;
std::vector<sf::IPAddress> clients;
bool not_canc = true;
sf::IPAddress sender;
unsigned short port;
bool client_found = false;
while (not_canc == true)
{
sf::Packet packet;
if (socket.Receive(packet,sender,port) != sf::Socket::Done)
{
cout << "Odd... It seems there was an error receiving the message.\n";
}
string buffer;
packet >> buffer;
cout << buffer << "\n";
packet.Clear();
packet << buffer;
for (unsigned int i = 0; i < clients.size(); i++)
{
if (clients[i] == sender)
{
client_found = true;
}
else
{
socket.Send(packet, clients[i], 54359);
}
}
if (client_found == false)
{
clients.push_back(sender);
}
packet.Clear();
client_found = false;
}
}
else if (answ_res == CONNECT)
{
string str_tocon;
bool inc_ip = true;
while (inc_ip == true)
{
getline(cin,str_tocon);
sf::IPAddress ip_tocon(str_tocon);
if (ip_tocon./*sf::IPAddress::*/IsValid() and str_tocon != "" and str_tocon != " ")
{
cout << "\nYay! The IP is valid!\n";
inc_ip = false;
}
else
{
cout << "\nYou are very naughty. That IP is NOT valid. Please try again.\n";
inc_ip = true;
}
}
string str_tosen;
cout <<"Now, what is your name?\n";
string user;
getline(cin,user);
bool sen_msg = true;
sf::Packet packet;
cout << "\n";
sf::IPAddress sender;
client_receive thread;
thread.receiving = true;
thread.port = 54359;
thread.socket = socket;
thread.Launch();
while (sen_msg == true)
{
getline(cin,str_tosen);
std::stringstream stri_stre;
stri_stre << user << ": " << str_tosen;
packet << stri_stre.str();
sf::IPAddress ip_tocon(str_tocon);
/*if (*/socket.Send(packet, ip_tocon, 54358)/* != )
{
cout << "Error sending message.";
}*/;
packet.Clear();
}
}
socket.Close();
return 0;
}
(I am using SFML 1.5)