Results 1 to 8 of 8

Threaded View

  1. #1
    ♪~ ᕕ(ᐛ)ᕗ's Avatar
    Join Date
    Jun 2010
    Gender
    male
    Location
    Uterus
    Posts
    9,119
    Reputation
    1096
    Thanks
    1,970
    My Mood
    Doh

    Sending detailed information using Sockets

    C&P from my developers forum...


    Hello fellow community,
    Today I'll show you how to send a detailed amount of information using sockets in a simple and really efficient way.

    We'll be using classes to carry those infos, a formatter to de/serialize the classes and two socket-based applications.

    So first of all open up Visual Studio and go to File->New Project. Then create a 'Blank Solution', afterwards right click on the solution's name in the Solution Explorer, and choose 'Add->New Project' and create a Class Library named "test_lib".

    Now before starting over there're some notes that you must take under consideration before continuing.
    • BinaryFormatters can deserialize a class only if it's assembly is loaded.
    • Maximum length of a socket is 30KB (so watch out for this, or you can split the packet if you want)


    Now in this tutorial we will create a software which sends a Account information to the server, and the server then proceeds to store the information regarding the account.
    Code:
    [Serializable]
    public class AccountInfo
    {
          private string user_name;
          private int user_id;
          private bool active;
    
          public string Username{ get{ return user_name; } }
          public int ID{ get{ return user_id; } }
          public bool Active{ get{ return active; } }
    
          public AccountInfo(string un, int id, bool isactive)
          {
                user_name = un;
                user_id = id;
                active = isactive;
          }
    }
    This class pretty much explains itself but keep in mind to mark the class as "Serializable" in case that you want to serialize it, and also if you add a custom class inside it (ie a Stream class) then you cannot serialize it because each class member have to be serializable.

    Next step is making the communication software. Let's start with the client:
    Code:
    using System;
    using System.Net;
    using System.Net.Sockets;
    using System****;
    using System.Runtime.Serialization.Formatters.Binary;
    using test_lib; //Add our test_lib as a reference.
    
    public class Program
    {
          public static byte[] SerializeClass(object c)
          {
                MemoryStream memsr = new MemoryStream(); // A graph which will soon represent our object.
                BinaryFormatter bfmt = new BinaryFormatter(); // A binary formatter which will handle the serialization part.
                bfmt.Serialize(memsr, c); //Serialize the class.
                return memsr.ToArray();
          }
     
          public static void SendData(AccountInfo info)
          {
                byte[] data = SerializeClass(info);
                Socket s = new Socket( ... );  // '...' just to skip the parameters
                s.Connect(IPAddress.Parse("127.0.0.1"), 5588); // 127.0.0.1 is the localhost address and 5588 is our custom server's port.
                s.Send(data);
                s.Disconnect(false); //Disconnect the socket.
          }
    
          static void Main(string[] args)
          {
                Console.WriteLine("Please enter your account name:");
                string name = Console.ReadLine();
    
               int id = new Random().Next(0, 100);
               Console.Clear();
               Console.WriteLine("Sending your data...");
               SendData(new AccountInfo(name, id, true));
               Console.Write("DONE!");
               Console.Read();
          }
    }
    Now time to work with the server..
    Code:
    using System;
    using System.Net;
    using System.Net.Sockets;
    using System****;
    using System.Runtime.Serialization.Formatters.Binary;
    using test_lib; //Add our test_lib as a reference.
    using System.Threading;
    
    class Program{
    
            static object Deserialize(byte[] o)
            {
                   MemoryStream memsr = new MemoryStream(o); // A graph which represents our object.
                BinaryFormatter bfmt = new BinaryFormatter(); // A binary formatter which will handle the deserialization part.
                return bfmt.Deserialize(memsr); //Deerialize the class.
            }
    
            static void CreateRunThread()
            {
                 Socket s = new Socket( ... );
                 s.Bind((EndPoint)(new IPEndPoint(IPAddress.Any, 5588)));  //IPAddress.Any is recommended for this kind of operations.
                 s.Listen(100); //Listens for incoming connections...
                while(true)
                { 
                      Socket client = s.Accept();
                      byte[] bytes = new byte[1024]; //1KB should be enough to handle the info's size.
                      client.Receive(bytes);
                      AccountInfo acc = (AccountInfo)Deserialize(bytes);
                      Console.WriteLine("Account received!\r\nName: " + acc.Username + "\r\nID: " + acc.ID.ToString() + "\r\nIs Active: " + acc.Active.ToString());
                      Console.Read();
                }
            }
            
            static void Main(string[] args)
            {
                   new Thread(new ThreadStart(CreateRunThread)).Start(); //Starts the server stuffs.
                   while(true) { Console.Title = "Server"; }
            }
    }
    Once ran the server then the client the server's output should be:
    Name: Elio
    ID: 52
    Active: True

    Hope it helps, Elio.
    Last edited by ♪~ ᕕ(ᐛ)ᕗ; 05-05-2013 at 01:21 PM.

  2. The Following 2 Users Say Thank You to ♪~ ᕕ(ᐛ)ᕗ For This Useful Post:

    abuckau907 (05-05-2013),NLErwinZ (05-08-2013)

Similar Threads

  1. [Release] Useful Hack Information + Details to become a hack coder - [MUST READ]
    By Assassin's Creed in forum CrossFire Hack Coding / Programming / Source Code
    Replies: 74
    Last Post: 01-29-2013, 08:30 AM
  2. [Tutorial] ♥ Extracting Information Using SQLMAP ♥
    By Solo in forum General Hacking
    Replies: 1
    Last Post: 05-02-2012, 03:52 AM
  3. Can you send me some used gcoin codes?
    By Kukri in forum Alliance of Valiant Arms (AVA) Discussions
    Replies: 3
    Last Post: 05-04-2011, 11:40 PM
  4. [Help]Using GEbyID to send lines of a RTB
    By trevor206 in forum Visual Basic Programming
    Replies: 0
    Last Post: 05-11-2010, 05:58 PM
  5. [Easy][VB2008 Tutorial, Beginner friendly] Sending a Email using SMTP
    By apezwijn in forum Visual Basic Programming
    Replies: 5
    Last Post: 10-22-2008, 07:13 AM