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

Author Topic: Problem with TCP server/client [v1.6]  (Read 4409 times)

0 Members and 1 Guest are viewing this topic.

JohnGreek

  • Newbie
  • *
  • Posts: 43
    • View Profile
Problem with TCP server/client [v1.6]
« on: August 01, 2011, 03:07:56 pm »
Hello!
I'm using SFML network for a server/client system but i'm stuck in a very weird point. When i try to connect the client to localhost, even without the server open i get a connection(!) and when i open the server and i recieve data i get some system paths and hardware ids. ex my CPU data or a path to my gfx card panel from the socket. More detailed, the cases:

(1)
Server is offline
Client connects to localhost sucessfully like the server is online

(2)
Server is online
Client connects and before i manage to send data
Server recieves weird data from socket(supposed to be empty)
CPU hardware details and some system32 paths to some executables(!)


I tried using the tutorial code but it's not suitable since it expects the server to be online i need the opposite.
What really is the issue:

1. Client connects to localhost without the server online
2.Server recieves data from client when it's connected without the client to send anything


Any help is appreciated.
Thanks in advance.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problem with TCP server/client [v1.6]
« Reply #1 on: August 01, 2011, 03:24:58 pm »
Which port do you connect to? What is your OS?
Laurent Gomila - SFML developer

JohnGreek

  • Newbie
  • *
  • Posts: 43
    • View Profile
Problem with TCP server/client [v1.6]
« Reply #2 on: August 01, 2011, 03:52:29 pm »
i try with any port, it's the same.
OS is Win7 x32

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problem with TCP server/client [v1.6]
« Reply #3 on: August 01, 2011, 04:11:20 pm »
Can you show a minimal and complete code that reproduces the problem?
Laurent Gomila - SFML developer

JohnGreek

  • Newbie
  • *
  • Posts: 43
    • View Profile
Problem with TCP server/client [v1.6]
« Reply #4 on: August 01, 2011, 04:43:27 pm »
This is the server sc:
Code: [Select]
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;
using namespace sf;


class Server
{
    private:
        SocketTCP InSocket;
        SocketTCP OutSocket;
        unsigned short ClientPort;

        IPAddress ClientAddress;
        char RecievedData[10];
        size_t RecievedDataSize;

    public:
        Server();
        ~Server();

        void RecieveData()
        {
            InSocket.Receive( RecievedData, sizeof(RecievedData), RecievedDataSize);
        }
        string WaitConnections(int port);
        bool StartServer();
};


bool run = true;
Server *server= NULL;

Server::Server()
{
    InSocket.SetBlocking(true);
    OutSocket.SetBlocking(true);
}
Server::~Server()
{

}
string Server::WaitConnections(int port)
{
    if(!InSocket.Listen(port))
        return "ERROR";

    cout << "Waiting for connections on port " << port << endl;

    InSocket.Accept(OutSocket, &ClientAddress);
    cout << "Client connected: " << ClientAddress << endl;

    cout << "Connection established!" << endl;
}
bool Server::StartServer()
{
    RecieveData();

    cout << "Data from client: " << RecievedData << endl;

    cout << "ALL IS GOOD!" << endl;
}




int main()
{
    server= new Server();
    server->WaitConnections(1025);
    server->StartServer();
    system("pause");
}


And this is the client sc:

Code: [Select]
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;
using namespace sf;

class Client
{
    private:
        SocketTCP ClientSocket;
        unsigned short ServerPort;

        IPAddress ServerAddress;
        char ToSendData[10];

    public:
        Client();
        ~Client();

        void InitClient(string address, unsigned short port);
        bool StartClient();
};

bool run = true;
string server_address;
Client *client= NULL;



Client::Client()
{

}
Client::~Client()
{

}
void Client::InitClient(string address, unsigned short port)
{
    ServerAddress= address;

    ServerPort= port;
}
bool Client::StartClient()
{
    cout << "Connecting on server at address " << ServerAddress << endl;
    do
    {
        ClientSocket.Connect(ServerPort, ServerAddress);
    }
    while (ClientSocket.Connect(ServerPort, ServerAddress)== Socket::Disconnected );

    cout << "You have been connected to the server!" << endl;

    cout << "Enter the data to send to the server: ";
    cin >> ToSendData;

    ClientSocket.Send( ToSendData, sizeof(ToSendData) );
}



int main()
{
    client= new Client();
    client->InitClient("127.0.0.1", 1025);
    client->StartClient();
    system("pause");
}


If you have any question tell me.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problem with TCP server/client [v1.6]
« Reply #5 on: August 01, 2011, 04:55:21 pm »
Code: [Select]
   do
    {
        ClientSocket.Connect(ServerPort, ServerAddress);
    }
    while (ClientSocket.Connect(ServerPort, ServerAddress)== Socket::Disconnected );

What is this supposed to do? :?

Code: [Select]
string Server::WaitConnections(int port)
{
    if(!InSocket.Listen(port))
        return "ERROR";

    cout << "Waiting for connections on port " << port << endl;

    InSocket.Accept(OutSocket, &ClientAddress);
    cout << "Client connected: " << ClientAddress << endl;

    cout << "Connection established!" << endl;
}

You're never returning anything, therefore the caller will get random data (hardware info, etc. ;)).
Laurent Gomila - SFML developer

JohnGreek

  • Newbie
  • *
  • Posts: 43
    • View Profile
Problem with TCP server/client [v1.6]
« Reply #6 on: August 01, 2011, 05:01:39 pm »
i noticed that when i use connect if there is no connection possible it returns so the application terminates. So i wrap it in this loop to check if the connection is sucessfull step out and send data etc etc.

I don't really get your second tip. The caller get's random data? Since the server is in accept it should block until a client connects, so it should wait until the client connects. Indeed, when i connect it exits from accept but i get the random data.
I'm trying to learn SFML cause it's much more better than SDL(using SDL for about 2 years), the most important is the OO structure, not to mention the graphics manipulation. But i wish it had a bigger community..  :(

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problem with TCP server/client [v1.6]
« Reply #7 on: August 01, 2011, 05:08:07 pm »
Quote
i noticed that when i use connect if there is no connection possible it returns so the application terminates. So i wrap it in this loop to check if the connection is sucessfull step out and send data etc etc.

You're calling Connect twice per iteration, so the first one is always ignored. And you're looping while it returns Disconnected, but it could as well return Error or NotReady.

So :
Code: [Select]
sf::Socket::Status status = ClientSocket.Connect(ServerPort, ServerAddress);
while (status != sf::Socket::Done)
    status = ClientSocket.Connect(ServerPort, ServerAddress);


Quote
I don't really get your second tip.

You declare a function that returns a string so you should have "return xxx;" somewhere at the end of the function. Here you have nothing, so the caller which calls "string xxx = WaitConnections(port)" gets random stuff in the string.
Your project is not well configured if the compiler doesn't even produce a warning for this.
Laurent Gomila - SFML developer

JohnGreek

  • Newbie
  • *
  • Posts: 43
    • View Profile
Problem with TCP server/client [v1.6]
« Reply #8 on: August 01, 2011, 05:53:27 pm »
Quote from: "Laurent"
Quote
i noticed that when i use connect if there is no connection possible it returns so the application terminates. So i wrap it in this loop to check if the connection is sucessfull step out and send data etc etc.

You're calling Connect twice per iteration, so the first one is always ignored. And you're looping while it returns Disconnected, but it could as well return Error or NotReady.

but even without the loop it returns.


I'm messing with networking for quite some time now, but i always end up in dead-ends and it's really annoying. I am working with Winsock too, very well documented but extremely complicated. SFML network is easier than Winsock but i don't really see light. The thing is that i am working with c++ for 4 years and never stuck into something for such a long time. I think i may need to look for a general network programming tutorial. If you have any tips they are welcome...  :?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Problem with TCP server/client [v1.6]
« Reply #9 on: August 01, 2011, 06:28:06 pm »
Quote
I think i may need to look for a general network programming tutorial

I think it's mandatory. Unlike graphics or audio modules, which have classes that are easily understandable and quite high level (sprite, font, sound, music, ...), the network module sticks to the usual low-level concepts: sockets, selectors, TCP, UDP. If you haven't read any documentation about these things you will hardly understand something ;)
Laurent Gomila - SFML developer

JohnGreek

  • Newbie
  • *
  • Posts: 43
    • View Profile
Problem with TCP server/client [v1.6]
« Reply #10 on: August 01, 2011, 06:40:37 pm »
It's really pain in the ass. I'm gonna stick to the basics first and then go for the rest. Thanks a lot Laurent :)