Mad Library offers you very usable part of itself which is named Networking. Networking is primary targeted on Client<->Server communication, but it also contains P2P communication functions and communication by more protocols, eg. HTTP (builders of HTTP request and HTTP response), NODA, …

3.1 Client–Server communication

Client – Server communication is many times simple, but from experiences you certainly know that you must spend lot of time when you’re working on it. Sometimes, when you’re creating application which is primary based on Client - Server communication you must program your own code because you have some special functions, methods or properties which you must do. But sometimes you’re using this kind of communication only for small operations, or this kind of communications is not so important part of your application. Then you can use preprogrammed options of Mad Library.

Code:

Communicator cm = new Communicator(new Requester(new IPEndPoint(IPAddress.Loopback, 1025)));
cm.RequestData = new MadLibrary.Net.Client.Request("GIVE ME RESPONSE");
ExampleServer srv = new ExampleServer();
srv.Start();
cm.Open();
cm.Request();
Console.WriteLine(cm.ResponseData);
cm.Close();
srv.End();
Console.Read();

ExampleServer:

public class ExampleServer : Server
{
    public ExampleServer()
        : base(1025)
    { 
            
    }
    public override void Process(Socket client)
    {
        byte[] data = new byte[5];
        client.Receive(data);
        client.Send(data);
    }
}

 

3.2 HTTP

HTTP protocol is mostly used protocol in the world. It’s our daily part. Daily part of professional programmers and also daily part of normal users. So these utterance is reason why was build HTTP manager in Mad Library. For now, we have just holders, builders and parsers in Mad Library, but these types are compatible with Request and Response classes in Client-Server communication by the operators (for now implicit). By these conversions you can send HTTP requests and get HTTP responses by the communicator and server. Down is written example about few functions of HTTP in Mad Libraries.

 

HttpRequest request = new HttpRequest();
request.Method = "GET";
request.HttpVersion = "1.1";
request.Options.Add("User-Agent", "unknown");
request.RequestedFile = "index.html";
request.Data.Add("variable", "value");
Console.WriteLine(request.Build());

HttpResponse resp = new HttpResponse();
resp.HttpVersion = "1.1";
resp.Content = "";
resp.Options.Add("Server", "this one");
resp.StatusCode = 200;
resp.StatusMessage = "OK";
Console.WriteLine(resp.Build());

Console.Read();

 

3.3 NDMP

NDMP (Network Data Managing Protocol) is free small networking protocol for Client-Server communication which offers you simple methods for data managing through network. Down is example of usage and in Mad Library assembly comments you can find documentation for this protocol (near methods, types and enums).

Code:

ServiceListener<ExampleService> listener = new ServiceListener<ExampleService>(1025);
listener.Start();
Client client = new Client(new IPEndPoint(IPAddress.Loopback, 1025));
client.Connect();
MadLibrary.Net.Ndmp.Response resp = client.Select("wWwWwWw");
client.Disconnect();
Console.WriteLine(resp.ToString());
Console.Read();

 

ExampleService:

public class ExampleService : Service
{
    public ExampleService() : base()
    { 
            
    }
    public ExampleService(IPEndPoint client)
        : base(client)
    { 
            
    }
    public override Response ProcessClient(Request request)
    {
        return base.ProcessClient(request);
    }
}

 

3.4 Peer-to-Peer

Peer-to-Peer communication is also important part of network communication and programming. Mad Library offers you classes which can hold P2P communication informations and which can manage/mediate that kind of communication. So here is example:

 

Peerer p = new Peerer(new PeererInfo(1025, System.Net.IPAddress.Loopback));
new Thread(new ThreadStart(() =>
{
    Thread.Sleep(100);
    p.Connect();
    new Thread(new ThreadStart(() => { Console.WriteLine(p.Receive(3)); })).Start();
    p.Send("LOL");
    Thread.Sleep(100);
})).Start();
Console.ReadLine();

Last edited Jun 17, 2012 at 6:02 PM by FrewCen, version 5

Comments

No comments yet.