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

Author Topic: How to transfer data from a non-SFML client to a SFML Server  (Read 7130 times)

0 Members and 1 Guest are viewing this topic.

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
How to transfer data from a non-SFML client to a SFML Server
« on: January 25, 2015, 12:29:46 am »
Im currently learning java, and I'm developing a chat application for android (in java).
The chat application takes a username and text from the user and sends it to a server application on my computer.
The server application is written in C++ with the SFML Network library.

Here's my very simple Server (It's supposed to just write out the sent text at the moment):
#include <SFML/Network.hpp>
#include <iostream>

using namespace std;

int main()
{

    sf::TcpListener listener;
    if (listener.listen(4242) != sf::Socket::Done)
    {
        cerr<< endl << "Error: Couldn't set up TCP listener";
    }

    sf::TcpSocket client;
    listener.accept(client);
    cout << endl <<"New client connected: " << client.getRemoteAddress() << endl;
    char data[100];
    std::size_t received;

    if (client.receive(data, 100, received) != sf::Socket::Done)
    {
    cerr<<endl<<"Error while receiving data";
    }
    else
    cout << endl << "Data received: " << data;


}
 
Here's a snippet of my java code in which the message is being sent:
            try {
                Socket socket = new Socket("192.168.1.126",4242);
                OutputStream out = socket.getOutputStream();

                PrintWriter output = new PrintWriter(out);
                output.println("Hello World!"); // Hello World will be replaced by the actual text message in the future

                socket.close();

            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
 

When I run both applications, the phone actually connects to my server, and the server writes "New client connected..." to the console, but I'm unable to send the "Hello World!" text. The server doesn't receive any data.

So my question is: Is it possible that the Server receives the data? And If yes, how can I achieve that and what do I have to keep in mind?
« Last Edit: January 25, 2015, 04:33:38 pm by Cadisol87 »

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: Help with Java client
« Reply #1 on: January 25, 2015, 02:35:33 pm »
Just to be clear I'm not looking for a java-specific solution. I just want to know how to transfer data between a SFML Server and a non-SFML client.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: Help with Java client
« Reply #3 on: January 25, 2015, 04:28:36 pm »
Yes, it should work, shouldn't it? The client successfully connects to my server, but the data isn't received.
And of course I read the tutorials, and I'm sure that I could write a SFML-Client and a SFML-Server that would communicate just fine. But that's not the case here, I try to connect with a client written in Java, which is not using SFML. If someone could point me in the right direction please, It would be very nice :)
Also, as the FAQ states:
Quote
9. When all else fails, you can always come to the SFML forum and ask for help there.
  ;)

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: How to transfer data from a non-SFML client to a SFML Server
« Reply #4 on: January 25, 2015, 04:48:16 pm »
Ok, now somehow my SFML server receives some data, although I didn't change anything in my code. But it shows "Error while receiving data", and the char array just contains unreadable characters.

Nevermind, my SFML server doesn't receive any data. "Error receiving data" is shown when I disconnect the client.
« Last Edit: January 25, 2015, 05:10:36 pm by Cadisol87 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Help with Java client
« Reply #5 on: January 25, 2015, 05:02:02 pm »
Also, as the FAQ states:
Quote
9. When all else fails, you can always come to the SFML forum and ask for help there.
  ;)
Question is: Did you go through 1. - 8. first, because you never told us what you've tried so far and instead just provided the code in hope we'd fix things for you. ;)

Install Wireshark on your server and see what you actually receive.

Additionally just comparing your code to what the official tutorials teach, I don't see you checking whether the connection was accepted properly:
sf::TcpSocket client;
listener.accept(client);
vs
sf::TcpSocket client;
if (listener.accept(client) != sf::Socket::Done)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: How to transfer data from a non-SFML client to a SFML Server
« Reply #6 on: January 25, 2015, 06:19:11 pm »
Okay, now the server checks whether the connection was accepted, and now I know for sure that the connection was accepted properly.
I installed wireshark, and it shows me six packets exchanged between the client and the server, but honestly I don't know what to do with that information.  :-\

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: How to transfer data from a non-SFML client to a SFML Server
« Reply #7 on: January 25, 2015, 06:39:21 pm »
The first 3 packets are most likely the 3-way TCP handshake. Just take a look at the highest level protocol info, there you should somewhere see the data that is being received. If you see the correct data and your server doesn't get it, then you know there's something wrong with your server code.

If you can't figure it out, look up some basic Wireshark tutorial for TCP. Wireshark is for network what your debugger is for your application. If you want to do some networking and don't know how to utilize Wireshark, your life will become rather tedious very quickly. So it's definitely not a waste learning a few things. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: How to transfer data from a non-SFML client to a SFML Server
« Reply #8 on: January 25, 2015, 09:15:58 pm »
Honestly, i don't see any data. I read some tutorials and searched for the string, with no result. I will have to have a look on the java code...  :-[

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to transfer data from a non-SFML client to a SFML Server
« Reply #9 on: January 25, 2015, 09:30:07 pm »
Have you tried to keep your socket alive for a while, instead of closing the connection immediately after sending the data?
Laurent Gomila - SFML developer

Cadisol87

  • Full Member
  • ***
  • Posts: 129
  • C++ Programmer
    • View Profile
Re: How to transfer data from a non-SFML client to a SFML Server
« Reply #10 on: January 25, 2015, 09:53:50 pm »
Okay, my bad, I just discovered the error. I looked up the PrintWriter class at the oracle java documentation, and changed it accordingly to the example on the page.
Now it looks like this:
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
and it works. The strange thing is, that according to my java book, the code I used before should work too.
Anyways, I'm happy that it works now, and thank everyone that helped me with my problem.
Sorry again for bothering you, but I thought that it had to be a problem with SFML or my server program, since I just followed the instructions from my java book :(

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: How to transfer data from a non-SFML client to a SFML Server
« Reply #11 on: February 14, 2015, 09:11:49 pm »
Yep documentation is usually more up to date than a printed book so the thing that most likely happened was you either ran into an error in the book's code or it was out of date due to having been replaced with a better or different setup.  I hate those types of errors too so checking the doc every now and than when something looks like it should work but doesn't does help. :)
I have many ideas but need the help of others to find way to make use of them.