Part 1: [Only registered and activated users can see links. Click Here To Register...]
The Client Socket
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.
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.
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.
However as it's a bool then we wrap an if check around it.
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.
Then we create the while loop and put a Console.ReadLine in the top of it.
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.
When that's done we just call the Send method.
This should be the final main method.
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.
The Client Socket
Client LogicQuote:
- Client Logic
- Connection
- Testing
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;
}
}
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");
}
First we call the connect method.
Code:
SocketClient client = new SocketClient();
client.Connect("127.0.0.1", 7788);
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();
}
Code:
client.onDisconnection = new ConnectionEvent(OnDisconnection);
client.onReceive = new BufferEvent(OnReceive);
client.BeginReceive();
Code:
while (true)
{
Console.ReadLine();
}
Code:
byte[] packet = new byte[10];
for (int i = 0; i < 10; i++)
packet[i] = (byte)i;
Code:
client.Send(packet);
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();
}
}
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.