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

Author Topic: Simple chat program losing connection.  (Read 4078 times)

0 Members and 1 Guest are viewing this topic.

wicked357

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • http://www.eternalcoding.com
Simple chat program losing connection.
« on: July 13, 2010, 06:17:31 am »
I just started working on a simple client/server chat program, right now I can connect using the client, no problem here, I send a message to the server and here is where I get disconnected after that, along side I cannot type a new message in on the client either, why is this happening? What am I missing? I have looked at the tutorial and sample, both those have same issue so I don't know what to do to loop it right, any suggestions would be helpful, thank you.

Client code:
Code: [Select]

#include <SFML/Network.hpp>
#include <iostream>
#include <string>

using namespace std;

//Create the TCP socket
sf::SocketTCP Server;
sf::SocketTCP Client;

//Set the port to listen on
const unsigned short Port = 2435;

int main()
{
//Ask user for IP address of the server to connect to
sf::IPAddress ServerAdd;
do
{
cout << "Enter server IP: ";
cin >> ServerAdd;
cout << endl;
if(!ServerAdd.IsValid())
cout << "Error: " << "Invalid IP address..." << endl;
}while(!ServerAdd.IsValid());

//Connect to the server specified by IP address
if(Client.Connect(Port, ServerAdd) != sf::Socket::Done)
cout << "Error connecting to server: " << ServerAdd << endl;

cout << "Connected to server " << ServerAdd << endl;

while(true) //I know this is bad practice, I will fix later!
{
//Connect to the server specified by IP address
if(Client.Connect(Port, ServerAdd) != sf::Socket::Done)
cout << "Error connecting to server: " << ServerAdd << endl;

//Receive a message from the server
char Message[255];
std::size_t Received;
if(Client.Receive(Message, sizeof(Message), Received) != sf::Socket::Done)
cout << "Error receiving message from server: " << ServerAdd << endl;
else
cout << "User: " << Message << endl;

//Send a message to the server
char Sent[255];
cout << "You: ";
cin >> Sent;
cout << endl;
if(Client.Send(Sent, sizeof(Sent)) != sf::Socket::Done)
cout << "Error sending message to server: " << ServerAdd << endl;
}

return EXIT_SUCCESS;
}


Server code:
Code: [Select]

#include <SFML/Network.hpp>
#include <iostream>
#include <string>

using namespace std;

//Create the TCP socket
sf::SocketTCP Server;

//Set the port to listen on
const unsigned short Port = 2435;

int main()
{
//Open a socket and listen to a specified port
if(!Server.Listen(Port))
cout << "Error listing on Port: " << Port << endl;

cout << "Server is listening to port " << Port << ", waiting for connections..." << endl;

while(true) //I know this is bad practice, I will fix later!
{
//Wait for a connection to be established by a client
sf::IPAddress ClientAdd;
sf::SocketTCP Client;
if(Server.Accept(Client, &ClientAdd) != sf::Socket::Done)
cout << "Error not ready for listening..." << endl;
else
{
cout << "Client connected: " << ClientAdd << endl;
char buffer[] = "Connected to Server";
if(Client.Send(buffer, sizeof(buffer)) != sf::Socket::Done)
cout << "Error sendind message to client: " << ClientAdd << endl;
}

char Buffer[128];
std::size_t Received;
if (Client.Receive(Buffer, sizeof(Buffer), Received) != sf::Socket::Done)
cout << "Error receiving message from client: " << ClientAdd << endl;
else
cout << ClientAdd << ": " << Buffer << endl;
}

return EXIT_SUCCESS;
}

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
Simple chat program losing connection.
« Reply #1 on: July 13, 2010, 07:26:31 am »
Wow, I can list a lot of things, but :
- The client is connecting twice to the server : why ?

- If the client have a connection problem, the code is going on : make it stopping execution !
Mindiell
----

wicked357

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • http://www.eternalcoding.com
Simple chat program losing connection.
« Reply #2 on: July 13, 2010, 07:41:23 am »
My mistake on the second connection in the while loop, but what you stated in the last part is completely obvious it is stopping... I am wanting to know why this is occurring, I also didn't ask for a critique of code just a point in the right direction to why this is happening. If you don't want to do that or don't know why please don't respond, I am here for serious help only.

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
Simple chat program losing connection.
« Reply #3 on: July 13, 2010, 07:46:27 am »
I'm here to help seriously ;)
Now that your second connection is over, let's take your Accept part in the server out of your loop.

I'm not giving you THE answer, I'm trying to make you thinking about your errors. BTW, a ritique of code is always welcome, we are all doing mistakes, and we are all doing different, maybe what you did is very good and I'm just unable to understand it, so explain it to me  :wink:

But, with this code, the execution is not stopped, or I'm really missing something :
Code: [Select]
//Connect to the server specified by IP address
   if(Client.Connect(Port, ServerAdd) != sf::Socket::Done)
      cout << "Error connecting to server: " << ServerAdd << endl;

   cout << "Connected to server " << ServerAdd << endl;
Mindiell
----

 

anything