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

Author Topic: [SMFL 2] Using TcpListener (FIXED)  (Read 1753 times)

0 Members and 1 Guest are viewing this topic.

CoderUK

  • Newbie
  • *
  • Posts: 3
    • View Profile
[SMFL 2] Using TcpListener (FIXED)
« on: April 26, 2013, 05:02:21 pm »
Hi guys.

I'm trying to use the network module of the 2.0 rc to create a simple Server/Client that can send and receive some basic data. I've read through the tutorial and searched the forum best I can, but I can't find anything similar to the problem. First off, I'll post some code snips.

Server code:
Code: [Select]
TcpListener Host;
if(!Host.listen(50000))
return;

TcpSocket socket;
Host.accept(socket);
cout << "New Client Connected: " << socket.getRemoteAddress() << endl;
char ToSend[] = "Success";
socket.send(ToSend, sizeof(ToSend));
cout << "Message sent to client : \"" << ToSend << "\"" << endl;

Client code:
Code: [Select]
TcpSocket Client;
Client.setBlocking(false);
if(Client.connect("localhost", 50000))
        return;
char Message[128];
size_t Received;
Client.receive(Message, sizeof(Message), Received);
cout << "Message received! = \"" << Message << "\"" << endl;

char ToSend[] = "HELLOOOOOOOO";
Client.send(ToSend, sizeof(ToSend));
cout << "Message sent to server : \"" << ToSend << "\"" << endl;

Client.disconnect();

Basically, what happens is the code just hits the return and from what I can tell stops listening (Neglected to give its own thread, so if its listening it causes my UI to lock up atm). Sorry, about the pretty amateurish post,  new to C++/Networking
« Last Edit: April 26, 2013, 06:07:35 pm by CoderUK »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: [SMFL 2] Using TcpListener
« Reply #1 on: April 26, 2013, 05:34:21 pm »
Please use the [code = cpp] tags for code blocks instead of [ quote ].

Your problem can easily be solved by reading the documentation and asking yourself the good questions. If your code hits the return statement, it means that the connect function returns a non-zero value. But what is it supposed to return if it succeeds? ... ;)
Laurent Gomila - SFML developer

CoderUK

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: [SMFL 2] Using TcpListener
« Reply #2 on: April 26, 2013, 05:46:16 pm »
Ok, so am I right in thinking that we want to keep listening until it returns 0 (Socket has been sent or received). Once listen returns 0, then we send/receive our data?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: [SMFL 2] Using TcpListener
« Reply #3 on: April 26, 2013, 05:57:55 pm »
No, there's no "0" here. Seriously, have you read the doc?
Laurent Gomila - SFML developer

CoderUK

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: [SMFL 2] Using TcpListener
« Reply #4 on: April 26, 2013, 06:05:48 pm »
I meant Socket.Done (Also = 0 in the Status enum). Anywho, got it working now anyway.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: [SMFL 2] Using TcpListener
« Reply #5 on: April 26, 2013, 06:07:29 pm »
Quote
I meant Socket.Done
That's better ;)
Laurent Gomila - SFML developer