[TuT][C#]Creating your own proxy - Part 2

01/23/2012 20:00 I don't have a username#1
Part 1: [Only registered and activated users can see links. Click Here To Register...]

The Client Socket
Quote:
  • Client Logic
  • Connection
  • Testing
Client Logic
So now that we have been looking at server-side programming, then we will look at client-side programming. I will explain basic things about the client and then how we code the connection for a client etc. There won't be so much content in this part of the tutorial, because pretty much a lot of it has already been covered in the first part of the tutorial, which is server-side programming, but there is still a few things that you should be aware of etc.

First of all open up the current solution, in my case "ProxyExample_Server", however once it's been opened, then create a new project, which should be the client. I will call mine "ProxyExample_Client".

Basically we have already created our clientsocket, except one thing. The SocketClient class from the server can be used here in exactly same way, there is only a minor difference. We need to switch EndAccept out with a new method called Connect.

This connect method should contain 2 parameters. A string as IPAddress and an int as port. Within this method the Socket is created and connecting to a specific endpoint calling Socket.Connect() and that's basically the difference of a server-side client and an actual client.

Now there is some logic behind a client tho. The client should not run a lot checks etc. it is the server that should check for things. The client should send data to the server, the server handles the data and sends it back to the client or new data, the client then handles the new data. The way the client should handle data it receives is not by modifying it like the server, but displaying it to the user.

An example could be a chat.

User input "Hello World!"
Client sends "Hello World!" to the server
Server checks if it's a valid message
If the message is valid it sends it to the other Clients
The clients displays the text "Hello World!"

It's a really simple way of explaining it and there is a lot more backwards how a client work, but it's important that you should just understand a client is not handling things, but more like sending inputs and displaying.

Just like your PC works, it has a system which is running in the background, which handles all the input you give it as a user.
If you switch the system out with the server and the user out with the client, then it is basically same way this works.

I might said it a bit wrong or confusing, but I hope you get it.

Connection
Now let's get to the coding part with the connect.
I assume you by now have modified the SocketClient and created the connect method already. The connect method should also have a try/catch block and be a bool, return false in catch, otherwise return true. It's used to check if the client has connected.
Code:
        public bool Connect(string IP, int Port)
        {
            ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                ClientSocket.Connect(new IPEndPoint(IPAddress.Parse(IP), Port));
                return true;
            }
            catch
            {
                return false;
            }
        }
Now let's use this. We do not need a connect event as it will connect in the main method in our case, however if it was a real client with login etc. it would probably be in the login buttons event, but we need a receive and a disconnect event.

You already know how the event works from the server tutorial, so it's basically just creating them again.
Code:
        static void OnDisconnection(SocketClient sClient)
        {
            Console.WriteLine("The client has been disconnected...");
            try
            {
                if (sClient.ClientSocket.Connected)
                    sClient.ClientSocket.Disconnect(false);
            }
            catch
            {
            }
        }
        static void OnReceive(SocketClient sClient, byte[] Buffer)
        {
            Console.WriteLine("The client has received data from the server...");

            foreach (byte b in Buffer)
                Console.Write(b + "\t");
            Console.Write("\n\n\n");
        }
Now we just need to create the connect, which we will just do in the main method. After that we will make a while loop and then put a Console.ReadLine() and for every time we press enter we will just send a small data packet with some numbers to the server. We should then receive the packet back from the server and then displays it as shown above in the receive event.

First we call the connect method.
Code:
SocketClient client = new SocketClient();
client.Connect("127.0.0.1", 7788);
However as it's a bool then we wrap an if check around it.
Code:
            SocketClient client = new SocketClient();
            if (client.Connect("127.0.0.1", 7788))
            {
                Console.WriteLine("Success!!");
            }
            else
            {
                Console.WriteLine("Failed to connect to the server...");
                Console.ReadLine();
            }
Now we call the BeginReceive method as we want to be able to receive packets from the server. We also need to set the 2 events for disconnection and receive.
Code:
                client.onDisconnection = new ConnectionEvent(OnDisconnection);
                client.onReceive = new BufferEvent(OnReceive);
                client.BeginReceive();
Then we create the while loop and put a Console.ReadLine in the top of it.
Code:
                while (true)
                {
                    Console.ReadLine();
                }
After the ReadLine we will just create a packet with some numbers. It's up to you what size you want to give your packet and what numbers etc. However this is my packet.
Code:
byte[] packet = new byte[10];
                    for (int i = 0; i < 10; i++)
                        packet[i] = (byte)i;
When that's done we just call the Send method.
Code:
client.Send(packet);
This should be the final main method.
Code:
        static void Main(string[] args)
        {
            SocketClient client = new SocketClient();
            if (client.Connect("127.0.0.1", 7788))
            {
                Console.WriteLine("Success!!");

                client.onDisconnection = new ConnectionEvent(OnDisconnection);
                client.onReceive = new BufferEvent(OnReceive);
                client.BeginReceive();

                while (true)
                {
                    Console.ReadLine();
                    byte[] packet = new byte[10];
                    for (int i = 0; i < 10; i++)
                        packet[i] = (byte)i;

                    client.Send(packet);
                }
            }
            else
            {
                Console.WriteLine("Failed to connect to the server...");
                Console.ReadLine();
            }
        }
Testing
Now we need to test it, but as default VS will only have one main project, so we need to set both the server and the client to start up on debug.

Right click your solution and then properties.

Choose "Multiple startup projects:" and then just make both the projects to "Start".

Now debug and test if the client/server works with each other.

As you can see it should work just perfect and display like below.

[Only registered and activated users can see links. Click Here To Register...]

That's it for now, good luck and see you in part 3 where we will look at proxy logic.
The last part which will be part 4 we create a basic proxy between our now coded client and server.

However now you should be able to figure most of it out yourself as a proxy is basically a server for the client and a client for the server, then it's most likely about logical thinking.
01/25/2012 11:03 koko20#2
Good work we are waiting part 3
01/29/2012 22:19 Heroka#3
[Only registered and activated users can see links. Click Here To Register...]
thx bro . part3 w8ing for it =)
02/03/2012 11:18 I don't have a username#4
Quote:
Originally Posted by koko20 View Post
Good work we are waiting part 3
I will make part 3 later today :) Maybe part 4 if I can find time for it.
02/03/2012 14:44 injection illusion logic#5
great tuts buddy :) im waiting part 3 , for who know some asm and need help in getting the dhkey/encryption and its alg. , give them a part 5 XD -not the current encryption but an old one just to learn how this goes :D- goodluck
02/03/2012 17:08 I don't have a username#6
Quote:
Originally Posted by injection illusion logic View Post
great tuts buddy :) im waiting part 3 , for who know some asm and need help in getting the dhkey/encryption and its alg. , give them a part 5 XD -not the current encryption but an old one just to learn how this goes :D- goodluck
There is no need for a part 5, because encryptions and packethandling are in a different category and there is already tutorials on that.
02/03/2012 17:10 injection illusion logic#7
*cough* never knew about that other category , well where it exist o-0 like gimme tags or links or location :) need to read them badly
02/03/2012 17:11 I don't have a username#8
Pro4nevers threads.
02/06/2012 23:59 shitboi#9
Just for general knowledge, if one has learnt of how to write a simple client/server program as shown above, he can easily move on to a Simple HTTP-server.

Since http is built on top of tcp/ip, the program shown above can serve as a retarded http server.


To test,
start the server
type in localhost:7788 into address bar and hit enter (port number quoted from the above example)

you should see some weird output in your browser. The only reason for not seeing 0-9 is because the browser is comprehending it as ASCII codes and not integers.:)
02/07/2012 08:33 I don't have a username#10
Argh, haven't got much time to finish the 3rd part, but some what half way I guess. Been quite busy atm. I apologize for the delay.
02/07/2012 08:58 m7mdxlife#11
Quote:
Originally Posted by I don't have a username View Post
Argh, haven't got much time to finish the 3rd part, but some what half way I guess. Been quite busy atm. I apologize for the delay.
[Only registered and activated users can see links. Click Here To Register...]

your apology is not accepted!!
02/07/2012 10:15 I don't have a username#12
I wouldn't care if it was or not anyway xD