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

Author Topic: How do I use OnReceive? Also, Client.Connect problem  (Read 3527 times)

0 Members and 1 Guest are viewing this topic.

azkay

  • Newbie
  • *
  • Posts: 9
    • MSN Messenger - final_fantasie_2@hotmail.com
    • View Profile
How do I use OnReceive? Also, Client.Connect problem
« on: October 15, 2010, 06:47:51 pm »
Firstly, introduction on my situation.

I decided to attempt to learn C++ (again) and ended up looking at SFML.
Im planning on porting a flash client, ive done it before in another language, though I did only the TCP parts, never made the GUI or anything.

So I decided the logical place to start would be porting over the TCP stuff, as I already know it from before.

Anyway, ive spent the last few hours looking at http://www.sfml-dev.org/tutorials/1.6/network-sockets.php and the next few pages and ive sort of got "something" working.

This was just a test to figure out how to get the OnReceive working, which pretty much failed.

Code: [Select]

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

class MyEncryptedPacket : public sf::Packet{
private :

virtual void OnReceive(){
std::cout << "received" << std::endl;
}
};

void runClient(){
sf::SocketTCP Client;

    if (!Client.Connect(80, "175.107.140.28"))
        std::cout << "test" << std::endl;//return;

std::cout << "Connected to server " << std::endl;

char Buffer[] = "GET / HTTP/1.1\r\nHost: www.azkay.com\r\n\r\n";
if (Client.Send(Buffer, sizeof(Buffer)) != sf::Socket::Done)
return;

/*char Buffer2[1024];
std::size_t Received;
if (Client.Receive(Buffer2, sizeof(Buffer2), Received) != sf::Socket::Done){
   
}

std::cout << Buffer2 << std::endl;*/
sf::Packet RegularPacket;
    if (Client.Receive(RegularPacket) != sf::Socket::Done)
        return;

MyEncryptedPacket EncryptedPacket;
    if (Client.Receive(EncryptedPacket) != sf::Socket::Done)
        return;

Client.Close();
};

int main(){
runClient();

    std::cout << "Press enter to exit..." << std::endl;
    std::cin.ignore(10000, '\n');

    return EXIT_SUCCESS;
};


Keeping in mind most of this was "hacked" together with parts from the tutorial pages and its 3am and I started "learning" at around 5pm, I could be just not taking anything in properly and the whole beginner "background in other languages, 5th try over the last few years at C++, just finished my 6th or so hello world".

Basically, I sent a GET to my azkay.com, it was the only "quick" thing I could think of at the time, anything to test out the receive.
I just cant figure out exactly what "tells" it to use the OnReceive.

Also, could also be due to 3am, if I use:
Code: [Select]

    if (!Client.Connect(80, "175.107.140.28"))
        return;


It seems to exit runClient once it hits that, the return gets called. Shouldnt that only be called if theres an error in the connect?
If I take the return out and replace it with anything; eg; the cout, it keeps going and "works".

Also, if I uncomment out the commented receive, I see the response fine there, so im really confused at why the .Connect is "erroring". Is there anything I can output that tells me the error code?

Once again, if anything needs clarification just tell me, 3am, probably not making much sense.

Thanks

EDIT::
While im here;
In my original "port", I would send packets something like this:
Code: [Select]

TCPSend($Socket, StringToHex("S55FLASH10") & "00")


So, I would be sending 53353502464C415348023102300300 to the server, not the plaintext.
How would I do this? Same as the other one, just convert the string first?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
How do I use OnReceive? Also, Client.Connect problem
« Reply #1 on: October 15, 2010, 07:03:49 pm »
Quote
Basically, I sent a GET to my azkay.com, it was the only "quick" thing I could think of at the time, anything to test out the receive.

This is not the easiest thing to do to test Receive, because you have to handle the HTTP protocol properly. You should rather open a simple client and a simple server on localhost, and make them exchange data (like in the Sockets sample of the SFML SDK).
And if you really want to do HTTP, there's a sf::Http class.

Quote
I just cant figure out exactly what "tells" it to use the OnReceive.

It's called automatically by SocketTCP::Receive(sf::Packet&).
But you can't receive a sf::Packet if the server didn't send one (they have their own "header").

Quote
It seems to exit runClient once it hits that, the return gets called. Shouldnt that only be called if theres an error in the connect?

Connect doesn't return a bool, check the documentation ;)
Laurent Gomila - SFML developer

azkay

  • Newbie
  • *
  • Posts: 9
    • MSN Messenger - final_fantasie_2@hotmail.com
    • View Profile
How do I use OnReceive? Also, Client.Connect problem
« Reply #2 on: October 15, 2010, 07:11:51 pm »
Quote from: "Laurent"
And if you really want to do HTTP, there's a sf::Http class.


I looked at the sf::Http before the sockets, I only thought of using HTTP because it was "already there", I knew it should give SOME response.

Ill try what you suggested with the server. Thanks ;3

EDIT::

I think I get it now.

sf::Packet RegularPacket;

Makes an SFML packet, so it has its own header and what not which you can just send with .Send(whatever). You put data into that packet with RegularPacket << "somedata";
and the onReceive only works if your using sf::Packet to receive the SFML packet, yeah?

Also, reason I got confused with the .Connect problem was, on the next page there was a .cpp and it had
Code: [Select]

// Connect to the specified server
    if (!Client.Connect(Port, ServerAddress))
        return;

in it, so I mustve forgotten about the page before which had the if (Client.Connect(4567, "192.168.0.2") != sf::Socket::Done)

I mustve also missed this part of the conclusion:
Quote

It is important to remember that SFML packets use their own endianess and structure, so you cannot use them to communicate with servers that are not using them. To send raw data, HTTP / FTP requests, or whatever not built with SFML, use arrays of bytes instead of SFML packets.


Funny how someone elses input, even if youve been over it several times, makes it clearer.

azkay

  • Newbie
  • *
  • Posts: 9
    • MSN Messenger - final_fantasie_2@hotmail.com
    • View Profile
How do I use OnReceive? Also, Client.Connect problem
« Reply #3 on: October 15, 2010, 08:01:25 pm »
aaaaah this is so confusing.

Dont suppose I could get a quick "example" of a receiver client?
Basically;

Connects to a server
Sends a packet

start loop
if receive != "" echo receive;
endloop

So, it would connect to IP:PORT, send a packet, then keep looping a "receiver", if the data received isnt blank, then show it?

Ive just coded another page of something that is just confusing me.
And with that, im going to bed before I throw something at my monitor.

Night.

azkay

  • Newbie
  • *
  • Posts: 9
    • MSN Messenger - final_fantasie_2@hotmail.com
    • View Profile
How do I use OnReceive? Also, Client.Connect problem
« Reply #4 on: October 16, 2010, 09:28:23 am »
New day, catching on faster.
Ive been awake for maybe 30 minutes and things are already clearer.

Thanks to (http://www.sfml-dev.org/forum/viewtopic.php?t=1581), I think ive gotten the "receiver" finished, as in, "working".

Code: [Select]

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

int main(){
sf::SocketTCP Client;
std::string ReceivedStr;

Client.Connect(8080, "");

char Buffers[] = "S55FLASH10";
Client.Send(Buffers, sizeof(Buffers));
size_t Size = 0;

while(1){
char Buffer[1024];
while(Client.Receive(Buffer, sizeof(Buffer), Size) == sf::Socket::Done){
ReceivedStr.append(Buffer, Buffer + Size);
if (Size < sizeof(Buffer)){
std::cout << ReceivedStr << std::endl;
ReceivedStr = "";
break;
}
}
}

    std::cout << "Press enter to exit..." << std::endl;
    std::cin.ignore(10000, '\n');
Client.Close();

    return EXIT_SUCCESS;
};

 

anything