Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Send() goes to... which executable?  (Read 2898 times)

0 Members and 1 Guest are viewing this topic.

spacechase0

  • Newbie
  • *
  • Posts: 39
    • AOL Instant Messenger - thespacechase0
    • View Profile
    • http://spacechase0.com/
Send() goes to... which executable?
« on: January 04, 2010, 03:29:33 am »
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.)

Code: [Select]

//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)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Send() goes to... which executable?
« Reply #1 on: January 04, 2010, 11:26:17 am »
Hi

Have you tried the "chat" sample in the SFML SDK?
Laurent Gomila - SFML developer

spacechase0

  • Newbie
  • *
  • Posts: 39
    • AOL Instant Messenger - thespacechase0
    • View Profile
    • http://spacechase0.com/
Send() goes to... which executable?
« Reply #2 on: January 04, 2010, 07:24:44 pm »
I don't think so, considering I had no idea there was one.... :X

Sorry for bothering you.