Why using Thread.Sleep(); You wanna make anything Lagg?
This is a discussion on C++ Revolution - Sockets (Part 1) within the Habbo Releases forums, part of the Habbo Hotel category; Why C++? Because it is a nice Coding language for Sockets/Sessions. Huh? Revolution? Yes, Most of all emulators are in ...

Why C++?
Because it is a nice Coding language for Sockets/Sessions.
Huh? Revolution?
Yes, Most of all emulators are in C# or Java, so I wanted to try this in C++ :)
How did you do this?
Simple, a basic Socket system which accepts the connections from the client (No big deal at all..)
But efficient for now, and soon there will be more :)
Code
SocketInit.h
Then in your Main() from your console application just add:Code:namespace Sockets { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::IO; using namespace System::Threading; using namespace System::Net; using namespace System::Security::Cryptography; using namespace System::Net::Sockets; using namespace System::Text; using namespace System::Globalization; using namespace System::Diagnostics; ref class SocketInitializer { public: static void DoSockets() { IPHostEntry^ Entry = Dns::GetHostByAddress("IPHERE"); IPEndPoint^ endPoint = gcnew IPEndPoint(Entry->AddressList[0], PORTHERE); Socket ^SocketInit = gcnew Socket(endPoint->Address->AddressFamily, SocketType::Stream, ProtocolType::Tcp); try { SocketInit->Bind(endPoint); SocketInit->Listen(5); Console::WriteLine("Intialized Sockets successfull"); } catch(Exception ^e) { Console::WriteLine("Exception: " + e->StackTrace); while(true) { Thread::Sleep(100); } } try { SocketInit->BeginAccept(gcnew AsyncCallback(&OnAccept), SocketInit); } catch(Exception ^e) { Console::WriteLine("Exception: " + e->StackTrace); } } static void OnAccept(IAsyncResult^ asyncResult) { Console::WriteLine("Incoming connection!"); } }; };
Replace IPHERE and PORTHERE to your LOCAL Ip and REMOTE Port.Code:Sockets::SocketInitializer::DoSockets();
Screen
Questions? ask them down :)
Last edited by BetterWay; 21-12-11 at 07:58 AM. Reason: Changes
Why using Thread.Sleep(); You wanna make anything Lagg?
This Thread::Sleep(); only act when there is an Exception by listening or binding..
This is C# LOL
The whole point of C++ is that it's cross platform...
Write your sockets for unix as well.
Posted via Mobile Device
Well, I hope you go well :] - I'm going to PM you if you're interested in something.
I was just wondering about packet reading in C++ and I think I got it well done :)
StateObject() ref class from msdn @ Microsoft.
Add to your console application Main thread:Code:namespace SocketManager { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::IO; using namespace System::Threading; using namespace System::Net; using namespace System::Security::Cryptography; using namespace System::Net::Sockets; using namespace System::Text; using namespace System::Globalization; using namespace System::Diagnostics; public ref class StateObject { public: literal int BUFFER_SIZE = 1024; Socket^ workSocket; array<Byte>^ buffer; StringBuilder^ sb; StateObject() : workSocket( nullptr ) { buffer = gcnew array<Byte>(BUFFER_SIZE); sb = gcnew StringBuilder; } }; ref class SocketInitializer { public: static void DoSockets() { array<Byte> ^PacketArray = gcnew array<Byte>(5000); IPHostEntry^ Entry = Dns::GetHostByAddress("172.16.0.6"); IPEndPoint^ endPoint = gcnew IPEndPoint(Entry->AddressList[0], 30002); Socket ^SocketInit = gcnew Socket(endPoint->Address->AddressFamily, SocketType::Stream, ProtocolType::Tcp); try { SocketInit->Bind(endPoint); SocketInit->Listen(1000); Console::WriteLine("Intialized Sockets successfull"); } catch(Exception ^e) { Console::WriteLine("Exception: " + e->StackTrace); while(true) { Thread::Sleep(100); } } try { SocketInit->BeginAccept(gcnew AsyncCallback(&OnAccept), SocketInit); } catch(Exception ^e) { Console::WriteLine("Exception: " + e->StackTrace); while(true) { Thread::Sleep(100); } } } static void OnAccept(IAsyncResult^ asyncResult) { Socket^ SocketH = safe_cast<Socket^>(asyncResult->AsyncState); Socket^ hSocket = SocketH->EndAccept(asyncResult); StateObject^ hSocketS = gcnew StateObject(); hSocketS->workSocket = hSocket; hSocket->BeginReceive(hSocketS->buffer, 0, hSocketS->BUFFER_SIZE, SocketFlags::None, gcnew AsyncCallback(&OnReceive), hSocketS); Console::WriteLine("Incoming connection!"); } static void OnReceive(IAsyncResult^ asyncResult) { StateObject^ so = safe_cast<StateObject^>(asyncResult->AsyncState); Socket^ s = so->workSocket; int read = s->EndReceive(asyncResult); if(read > 0) { so->sb->Append( Encoding::ASCII->GetString( so->buffer, 0, read ) ); String ^StringsInPacket = so->sb->ToString(); s->BeginReceive( so->buffer, 0, StateObject::BUFFER_SIZE, SocketFlags::None, gcnew AsyncCallback( &OnReceive ), so ); if(StringsInPacket->ToString()->Contains("<policy-file-request/>")) { array<Byte>^ GotBytesFromPacket = Encoding::ASCII->GetBytes("<?xml version=\"1.0\"?> " + "<!DOCTYPE cross-domain-policy SYSTEM \"/xml/dtds/cross-domain-policy.dtd\"> " + "<cross-domain-policy> " + "<allow-access-from domain=\"*\" to-ports=\"*\" /> " + "</cross-domain-policy>\x0"); s->Send(GotBytesFromPacket, GotBytesFromPacket->Length, SocketFlags::None); Console::WriteLine("Send policy to client, Wait for response."); s->BeginReceive( so->buffer, 0, StateObject::BUFFER_SIZE, SocketFlags::None, gcnew AsyncCallback( &OnReceive ), so ); } } } }; };
This evening or tomorrow I'll start on the Packethandlers and Registering of them :)Code:SocketManager::SocketInitializer::DoSockets();
The problem is that you have <sys/types> and <sys/sockets> at your disposal, which will make it cross-platform, rather than rely on the winsock windows-only sockets. Unix also opens up a greater number of sockets than Windows. Just a suggestion though.
I'm glad someone is finally bringing substance and originality to this section.
Sent from my DROID2 GLOBAL using Tapatalk
I coded only things like Memory Edit/Bypasses (Assembly) for FPS Games in C++ before, so all this stuff is very new for me right now, and I thought when ppl code Emulators in C# Why it can't be in C++? thats my point of this whole thing, or I'll fix it for Unix, got no idea.
Added Thread::Sleep(21) and while(true) for optimal receiving (Thread:Sleep(21) wont make it laggy at all, the problem is, if you don't add sleeps in C++ the program overloads, and will crash after a while..)
Screen:Code:namespace SocketManager { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::IO; using namespace System::Threading; using namespace System::Net; using namespace System::Security::Cryptography; using namespace System::Net::Sockets; using namespace System::Text; using namespace System::Globalization; using namespace System::Diagnostics; public ref class StateObject { public: literal int BUFFER_SIZE = 1024; Socket^ workSocket; array<Byte>^ buffer; StringBuilder^ sb; StateObject() : workSocket( nullptr ) { buffer = gcnew array<Byte>(BUFFER_SIZE); sb = gcnew StringBuilder; } }; ref class SocketInitializer { public: static void DoSockets() { array<Byte> ^PacketArray = gcnew array<Byte>(5000); IPHostEntry^ Entry = Dns::GetHostByAddress("172.16.0.6"); IPEndPoint^ endPoint = gcnew IPEndPoint(Entry->AddressList[0], 30002); Socket ^SocketInit = gcnew Socket(endPoint->Address->AddressFamily, SocketType::Stream, ProtocolType::Tcp); try { SocketInit->Bind(endPoint); SocketInit->Listen(1000); Console::WriteLine("Intialized Sockets successfull"); } catch(Exception ^e) { Console::WriteLine("Exception: " + e->StackTrace); while(true) { Thread::Sleep(100); } } try { while(true) { SocketInit->BeginAccept(gcnew AsyncCallback(&OnAccept), SocketInit); Thread::Sleep(21); } } catch(Exception ^e) { Console::WriteLine("Exception: " + e->StackTrace); while(true) { Thread::Sleep(100); } } } static void OnAccept(IAsyncResult^ asyncResult) { Socket^ SocketH = safe_cast<Socket^>(asyncResult->AsyncState); Socket^ hSocket = SocketH->EndAccept(asyncResult); StateObject^ hSocketS = gcnew StateObject(); hSocketS->workSocket = hSocket; hSocket->BeginReceive(hSocketS->buffer, 0, hSocketS->BUFFER_SIZE, SocketFlags::None, gcnew AsyncCallback(&OnReceive), hSocketS); Console::WriteLine("Incoming connection!"); } static void OnReceive(IAsyncResult^ asyncResult) { StateObject^ so = safe_cast<StateObject^>(asyncResult->AsyncState); Socket^ s = so->workSocket; int read = s->EndReceive(asyncResult); if(read > 0) { so->sb->Append( Encoding::ASCII->GetString( so->buffer, 0, read ) ); String ^StringsInPacket = so->sb->ToString(); s->BeginReceive( so->buffer, 0, StateObject::BUFFER_SIZE, SocketFlags::None, gcnew AsyncCallback( &OnReceive ), so ); if(StringsInPacket->ToString()->Contains("<policy-file-request/>")) { array<Byte>^ GotBytesFromPacket = Encoding::ASCII->GetBytes("<?xml version=\"1.0\"?> " + "<!DOCTYPE cross-domain-policy SYSTEM \"/xml/dtds/cross-domain-policy.dtd\"> " + "<cross-domain-policy> " + "<allow-access-from domain=\"*\" to-ports=\"*\" /> " + "</cross-domain-policy>\x0"); s->Send(GotBytesFromPacket, GotBytesFromPacket->Length, SocketFlags::None); Console::WriteLine("Send policy to client, Wait for response."); s->BeginReceive( so->buffer, 0, StateObject::BUFFER_SIZE, SocketFlags::None, gcnew AsyncCallback( &OnReceive ), so ); } else { Console::WriteLine("[RCV] " + StringsInPacket->ToString()); } } } }; };
![]()
Last edited by BetterWay; 21-12-11 at 01:08 PM.
If you want to make the source cross-platform...
Tutorial: Using the C++ Sockets library
I've been reading into this, seems interesting.