Welcome to the RaGEZONE - MMORPG development forums.

C++ Revolution - Sockets (Part 1)

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 ...

LyncusMU
Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    Alpha
    Rank
    Member
    Join Date
    Dec 2011
    Location
    The Netherlands
    Posts
    139
    Liked
    56

    C++ Revolution - Sockets (Part 1)

    Tabo Hotel
    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
    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!");
    		}
    	};
    };
    Then in your Main() from your console application just add:
    Code:
    Sockets::SocketInitializer::DoSockets();
    Replace IPHERE and PORTHERE to your LOCAL Ip and REMOTE Port.

    Screen

    Questions? ask them down :)
    Last edited by BetterWay; 21-12-11 at 07:58 AM. Reason: Changes

  2. HostKey.com: Unmetered Dedicated servers in the Netherlands
  3. #2
    Alpha
    Rank
    Member
    Join Date
    Dec 2011
    Posts
    134
    Liked
    39

    Re: C++ Revolution - Sockets (Part 1)

    Why using Thread.Sleep(); You wanna make anything Lagg?
    caja likes this.

  4. #3
    Alpha
    Rank
    Member
    Join Date
    Dec 2011
    Location
    The Netherlands
    Posts
    139
    Liked
    56

    Re: C++ Revolution - Sockets (Part 1)

    This Thread::Sleep(); only act when there is an Exception by listening or binding..

  5. #4
    [R8]ℓσℓ32
    Rank
    Subscriber
    Join Date
    Oct 2008
    Location
    Here!
    Posts
    1,449
    Liked
    166

    Re: C++ Revolution - Sockets (Part 1)

    This is C# LOL

  6. #5
    Azure subscription
    Rank
    Subscriber
    Join Date
    Dec 2011
    Location
    єαятн
    Posts
    1,733
    Liked
    263

    Re: C++ Revolution - Sockets (Part 1)

    Quote Originally Posted by caja View Post
    This is C# LOL
    You must have problems, this is for C++, not C#.

    Cool release, I do enjoy seeing people coding in C++ in the Habbo section.

  7. #6
    megaman
    Rank
    Member +
    Join Date
    May 2011
    Location
    London
    Posts
    997
    Liked
    198

    Re: C++ Revolution - Sockets (Part 1)

    The whole point of C++ is that it's cross platform...
    Write your sockets for unix as well.
    Posted via Mobile Device

  8. #7
    Alpha
    Rank
    Member
    Join Date
    Dec 2011
    Location
    The Netherlands
    Posts
    139
    Liked
    56

    Re: C++ Revolution - Sockets (Part 1)

    Quote Originally Posted by caja View Post
    This is C# LOL
    I think u really got no idea of coding languages, but anyways, it is C++. like said by others.

    Quote Originally Posted by MyKi View Post
    You must have problems, this is for C++, not C#.

    Cool release, I do enjoy seeing people coding in C++ in the Habbo section.
    Thanks :)

    Quote Originally Posted by Caustik View Post
    The whole point of C++ is that it's cross platform...
    Write your sockets for unix as well.
    Posted via Mobile Device
    I'll take a look, but I don't promise anything ^^

  9. #8
    Azure subscription
    Rank
    Subscriber
    Join Date
    Dec 2011
    Location
    єαятн
    Posts
    1,733
    Liked
    263

    Re: C++ Revolution - Sockets (Part 1)

    Well, I hope you go well :] - I'm going to PM you if you're interested in something.

  10. #9
    Alpha
    Rank
    Member
    Join Date
    Dec 2011
    Location
    The Netherlands
    Posts
    139
    Liked
    56

    Re: C++ Revolution - Sockets (Part 1)

    I was just wondering about packet reading in C++ and I think I got it well done :)
    StateObject() ref class from msdn @ Microsoft.

    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 );
    				}
    			}
    		}
    	};
    };
    Add to your console application Main thread:
    Code:
    SocketManager::SocketInitializer::DoSockets();
    This evening or tomorrow I'll start on the Packethandlers and Registering of them :)

  11. #10
    megaman
    Rank
    Member +
    Join Date
    May 2011
    Location
    London
    Posts
    997
    Liked
    198

    Re: C++ Revolution - Sockets (Part 1)

    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.

  12. #11
    Web Application Developer
    Rank
    Moderator
    Join Date
    Apr 2010
    Posts
    2,982
    Liked
    1000
    I'm glad someone is finally bringing substance and originality to this section.

    Sent from my DROID2 GLOBAL using Tapatalk

  13. #12
    Alpha
    Rank
    Member
    Join Date
    Dec 2011
    Location
    The Netherlands
    Posts
    139
    Liked
    56

    Re: C++ Revolution - Sockets (Part 1)

    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..)

    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());
    				}
    			}
    		}
    	};
    };
    Screen:
    Last edited by BetterWay; 21-12-11 at 01:08 PM.

  14. #13
    Alpha
    Rank
    Member
    Join Date
    Dec 2011
    Posts
    134
    Liked
    39

    Re: C++ Revolution - Sockets (Part 1)

    How did you make your Console Orange?

    Quote Originally Posted by Squard View Post
    How did you make your Console Orange?
    OMG my fail. Its windows srry.

  15. #14
    PHP, HTML5, CSS3, JS, C#
    Rank
    Alpha Member
    Join Date
    Jun 2010
    Location
    The Netherlands
    Posts
    1,814
    Liked
    1013

    Re: C++ Revolution - Sockets (Part 1)

    Quote Originally Posted by Squard View Post
    Why using Thread.Sleep(); You wanna make anything Lagg?
    He's using it in a while true, If he didn't do that, you get a high cpu as result

  16. #15
    megaman
    Rank
    Member +
    Join Date
    May 2011
    Location
    London
    Posts
    997
    Liked
    198

    Re: C++ Revolution - Sockets (Part 1)

    If you want to make the source cross-platform...
    Tutorial: Using the C++ Sockets library
    I've been reading into this, seems interesting.

 

 
Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •