1
System / Best way to setup GUI/TCP?
« on: November 05, 2010, 02:59:15 pm »
But, does anyone see where/if I went wrong in the code I posted?
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
Maybe you can have a look at punBB. I used it once and it was a light and simple forum engine. But I'm not sure that it could solve your problems, I don't remember its features exactly.Better yet, FluxBB.
#define _CRT_SECURE_NO_WARNINGS
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#include <sstream>
#include "misc.h"
#include "f.h"
#include "md5.h"
sf::SocketTCP Client;
sf::SelectorTCP Selector;
std::string STX = "\x02";
std::string ETX = "\x03";
std::string SOH = "\x01";
int main(){
std::vector<std::string> results;
std::stringstream all;
std::string ReceivedStr;
size_t Size = 0;
char Buffer[1];
std::vector<std::string> gInfo = fLogin("", "");
std::string hood = "";
std::string server = fServer(hood);
Client.Connect(8080, server);
//Client.SetBlocking(false);
Selector.Add(Client);
fSend(Client, "some,data");
fSend(Client, "some,data");
fSend(Client, "some,data");
sf::Event Event;
sf::RenderWindow App(sf::VideoMode(1024, 1024, 32), "SFML Graphics");
sf::Image Map;
sf::Image avatarMe;
Map.LoadFromFile("image1.jpg");
avatarMe.LoadFromFile("1aff79cba4bac.png");
sf::Sprite Flash(Map);
sf::Sprite Sprite(avatarMe);
while(App.IsOpened()){
while(App.GetEvent(Event)){
if (Event.Type == sf::Event::Closed)
App.Close();
}
float ElapsedTime = App.GetFrameTime();
if (App.GetInput().IsKeyDown(sf::Key::Left)) Sprite.Move(-100 * ElapsedTime, 0);
if (App.GetInput().IsKeyDown(sf::Key::Right)) Sprite.Move( 100 * ElapsedTime, 0);
if (App.GetInput().IsKeyDown(sf::Key::Up)) Sprite.Move(0, -100 * ElapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::Down)) Sprite.Move(0, 100 * ElapsedTime);
App.Clear();
App.Draw(Flash);
App.Draw(Sprite);
App.Display();
if(Selector.Wait()){
Client.Receive(Buffer, sizeof(Buffer), Size);
ReceivedStr.append(Buffer, Buffer + Size);
if(Size <= sizeof(Buffer)){
if(ReceivedStr != ETX){
all << ReceivedStr;
}else{
if(all.str().length() > 2){
if(!isdigit(all.str().substr(0, 1)[0]))
all.str(all.str().substr(1, all.str().length()));
StringExplode(all.str(), STX, &results);
switch(atoi(results[0].c_str())){
case 35:
std::cout << all.str() << std::endl;
break;
default:
std::cout << all.str() << std::endl;
break;
}
}
all.str("");
}
ReceivedStr = "";
results.clear();
}
}
}
std::cout << "Press enter to exit..." << std::endl;
std::cin.ignore(10000, '\n');
Client.Close();
return EXIT_SUCCESS;
}
#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;
};
And if you really want to do HTTP, there's a sf::Http class.
// Connect to the specified server
if (!Client.Connect(Port, ServerAddress))
return;
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.
#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;
};
if (!Client.Connect(80, "175.107.140.28"))
return;
TCPSend($Socket, StringToHex("S55FLASH10") & "00")