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

Author Topic: Chat is not working properly  (Read 2466 times)

0 Members and 1 Guest are viewing this topic.

P@u1

  • Jr. Member
  • **
  • Posts: 83
    • View Profile
Chat is not working properly
« on: April 27, 2011, 07:05:41 pm »
Hi everyone,

I wrote a chat with the sfml network package using an additional thread (so I have 1 for sending and one for receiving).
I tested it and there are still some problems:
When using localhost or my local ip i can chat with myself without a problem.
When trying to chat between my pc and my laptop or between my pc and the pc of a friend who is on local lan it is only partially working:
On my pc everything is working.
On the other pcs when starting it prints
Quote

error connecting
error sending

When I send from my pc, the other pcs can receive it, but the other pcs cant send themselves, when they try it prints
Quote

error sending

Note: I'm printing this messages myself (see below)


My code is this:
Code: [Select]

int main(void)
{
std::string ipStr;
std::cout << "ip eingeben: ";
std::cin >> ipStr;
std::cout << std::endl;
sf::IPAddress address = ipStr;
if (!address.IsValid())
{
std::cerr << "invalid address" << std::endl;
}
SendeThread thread(address);
thread.Launch();

sf::SocketTCP Listener;
if (!Listener.Listen(4567))
{
std::cerr << "error listening" <<std::endl;
}

sf::SocketTCP Client;
if (Listener.Accept(Client, &address) != sf::Socket::Done)
{
std::cerr <<"error accepting" <<std::endl;
}
std::string line;
while(true)
{
sf::Packet toReceive;
Client.Receive(toReceive);
if(!(toReceive >> line))
{
std::cerr <<"error receiving" <<std::endl;
}
std::cout << "received: " << line << std::endl;
if(line == "quit")
{
break;
}
}
std::cout << "finished" << std::endl;
std::cin.peek();
return EXIT_SUCCESS;
}


Code: [Select]

void SendeThread::Run()
{
sf::SocketTCP Client;
if (Client.Connect(4567, myAddress) != sf::Socket::Done)
{
std::cerr <<"error connecting" <<std::endl;
}
while(std::cin)
{
sf::Packet toSend;
std::string line;
getline(std::cin, line);
toSend << line;
if (Client.Send(toSend) != sf::Socket::Done)
{
std::cerr <<"error sending" <<std::endl;
}
if(line == "quit")
{
return;
}
}
}


Please help me :-)
Is it a problem in the code, or just something with firewall or so?
I turned off firewalls and it didn't help, what else could it be?
Many thanks in advance!

P@u1

  • Jr. Member
  • **
  • Posts: 83
    • View Profile
Chat is not working properly
« Reply #1 on: April 27, 2011, 10:33:49 pm »
Edit:
I did not understand the distinction between server and client (which as I think I know now, must be done).
So I rewrote the program:

Code: [Select]

const int PORT = 3456;

void keepWindowOpen(int exitCode)
{
std::cout << "Press Enter to close the program" << std::endl;
std::cin.ignore(1000, '\n');
std::cin.peek();
exit(exitCode);
}

sf::SocketTCP initServer(int port)
{
sf::SocketTCP Server;
if (!Server.Listen(port))
{
std::cerr << "And error occurred while trying to search for incoming connections at Port" << port << std::endl;
keepWindowOpen(1);
}
std::cout << "Server is listening to port " << port << ", waiting for connections... " << std::endl;
sf::SocketTCP Client;
sf::IPAddress ip;
if (Server.Accept(Client, &ip) != sf::Socket::Done)
{
std::cerr << "And error occurred while trying connect to " << ip << std::endl;
keepWindowOpen(1);
}
std::cout << "Client connected : " << ip << std::endl;
return Client;
}

sf::SocketTCP initClient(int port)
{
std::string input;
sf::IPAddress ip;
do
{
std::cout << "Enter ip: ";
std::cin >> input;
ip = input;
} while (!ip.IsValid());

std::cout << "Using ip: " << input << std::endl;

sf::SocketTCP Client;
std::cout << "Trying to connect to " << ip << std::endl;
if (Client.Connect(port, ip) != sf::Socket::Done)
{
std::cerr << "And error occurred while trying to connect to " << ip << std::endl;
keepWindowOpen(1);
}
std::cout << "Connected to server " << ip << std::endl;
return Client;
}

int doChat(sf::SocketTCP& socket)
{
sf::Packet toSend;
toSend << "hallo";
if((socket.Send(toSend)) != sf::Socket::Done)
{
std::cerr << "Error at sending" << std::endl;
//dont terminate
}
else
{
std::cout << "Sent" << "hallo" << std::endl;
}
sf::Packet toReceive;
socket.Receive(toReceive);
std::string line;
if(! (toReceive >> line))
{
std::cerr << "Error at receiving" << std::endl;
//dont terminate
}
else
{
std::cout << "Received: " << line << std::endl;
}
return EXIT_SUCCESS;
}

int main(void)
{
bool server;
std::string input;
do
{
std::cout << "Server Mode (s) or Client Mode (c) ? ";
std::cin >> input;
} while (input != "s" && input != "c");
server = (input == "s");
std::cout << "Using " << (server ? "Server":"Client") << " Mode" << std::endl;

sf::SocketTCP * socket = 0;
if(server)
{
socket = &initServer(PORT);
}
else
{
socket = &initClient(PORT);
}
if(!socket)
{
std::cerr << "Error: Socket is nullptr" << std::endl;
keepWindowOpen(1);
}

int exitCode = doChat(*socket);

socket->Close();
delete socket;
keepWindowOpen(exitCode);
}


Unfortunately it still doesent work...
Using the server to listen seems to work.
Connecting with the client to 127.0.0.1 (for testing) immediately returns with error if I didn't open a listening server before.
If I open a listening server and connect to it with a client connecting to 127.0.0.1 I get on both windows "Debug assertion failed" (something with vector).
I don't use vector in this code, so probably it happens somewhere in sfml, maybe because of incorrect usage.
Or should it not work for 127.0.0.1?
Please help :-)

I didnt use edit, because the first post would become too freaky long...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Chat is not working properly
« Reply #2 on: April 27, 2011, 10:45:23 pm »
Code: [Select]
  if(server)
   {
      socket = &initServer(PORT);
   }
   else
   {
      socket = &initClient(PORT);
   }

You're storing the address of variables that don't exist anymore after the functions return, when you use them it's an undefined behaviour, and that's probably why you get a "debug assertion failed".

Don't use a pointer, in SFML 1.6 sockets can be copied.

Code: [Select]
  sf::SocketTCP socket;
   if(server)
   {
      socket = initServer(PORT);
   }
   else
   {
      socket = initClient(PORT);
   }

I didn't read the rest of your code, so there might be other errors after you fix this.
Laurent Gomila - SFML developer

P@u1

  • Jr. Member
  • **
  • Posts: 83
    • View Profile
Chat is not working properly
« Reply #3 on: April 27, 2011, 10:48:39 pm »
Thx for help, but I think this was not the problem.

The return values of
initServer
and
initClient
are sf::SocketTCP.
They return the sockets by copying a local object.
They don't return a pointer.
I just use & because I store the socket I get from the function from with a pointer.

Would be very nice if you can provice further help.

Edit: I did minor changes and now its working.
Thanks for your help!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Chat is not working properly
« Reply #4 on: April 27, 2011, 10:57:08 pm »
Quote
The return values of
initServer
and
initClient
are sf::SocketTCP.
They return the sockets by copying a local object.
They don't return a pointer.
I just use & because I store the socket I get from the function from with a pointer.

They return a temporary object that will be destroyed right after the function call, since you don't copy it on the caller side. Therefore, you store the address of sockets that don't exist anymore after the call, your pointer refers to a memory location that contains destroyed data.

This is a common mistake in C++, and your compiler should even warn you about that; your warning level is probably too low.
Laurent Gomila - SFML developer

P@u1

  • Jr. Member
  • **
  • Posts: 83
    • View Profile
Chat is not working properly
« Reply #5 on: April 27, 2011, 11:16:38 pm »
Ok, than you were right :-)
Thank you for your help.
I will try to find out how I can increase the warn level in visual studio 2010.

It works now over lan, then I tried it over internet with 2 different persons and it does not work :-(
Do you think is is because of firewall settings or such things, or what might be the problems?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Chat is not working properly
« Reply #6 on: April 28, 2011, 07:49:35 am »
Quote
Do you think is is because of firewall settings or such things, or what might be the problems?

Probably, yes. You must open the ports on your firewall/router.
Laurent Gomila - SFML developer

P@u1

  • Jr. Member
  • **
  • Posts: 83
    • View Profile
Chat is not working properly
« Reply #7 on: April 28, 2011, 06:40:16 pm »
I got it working now.
If any1 cares:
I had to setup my router so that it forwards the port I use to my local ip (this only has to be done for the one who acts as server).

 

anything