Easy Networing Programming tutorial in eC?

Help with the Ecere networking library: Socket, Service, SSL, Distributed Objects, HTTP, XML protocols, etc.
Post Reply
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Easy Networing Programming tutorial in eC?

Post by jerome »

Sam,

I'll try to give an overview of the available resources for networking with Ecere.
First, Ecere has built-in networking functionality that might be interesting:

- A Socket class, that works with both TCP/IP and UDP sockets. The Service class is used when writing a TCP/IP service. Useful for implementing a particular or custom network protocol. There is a quick overview on the wiki . That thread is similar to this one, with a list of all the resources available for networking. Of particular interest to you might be the threads/threadsAndListBoxes sample, which teaches threading and networking together. UDPSample is a very nice short and sweet sample for UDP, or just getting started with the Ecere sockets. games/Othello is a nice and simple sample of how to use the Ecere Socket classes. Ecere Chess uses them as well, but is a bit more complex game (though the networking code is similar).

- The Ecere Distributed Component Object Model, or Ecere DCOM: This lets you invoke methods of objects living on a server, remotely. With EcereDCOM you can write network applications without writing a single line of networking code. This is done by declaring your class with 'remote class', and importing the module that declares it from the Client side with 'import remote'. The best sample to start on this would be net/DCOMSample: it implements a very simple chat client. There are some outstanding ASCII values/case sensitive issues regarding the project file name and ordering of the files in the project that have yet to be resolved with EcereDCOM: beware. games/ruff and games/scrabble are fully networked using EcereDCOM.

- Built in HTTP as file objects: anywhere an Ecere API takes in a file name, a URL starting with "http://" can be passed in. This applies e.g. to the FileOpen function, which will return a File object. Thus any HTTP URL can be opened as a File. HTTPFile and HTTPFile::OpenURL can be used directly as well for more options.

- Ecere includes 'NetworkClientFile' as well, a class meant to be used to access a file on a server, with seeking support. It was originally developed for buffered video playback. We should probably move this class outside of the runtime library into extras. samples\net\networkFile contains the Server code as well as the Client sample on how to use this functionality.

I hope this paves the way for you to learn Ecere networking easily :D Please ask if you have any specific questions or require clarifications on how any of it works.

All the best,

Jerome
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: Easy Networing & Threading Programming tutorial in eC?

Post by jerome »

Perhaps just a little explanation for the Socket class.
Follow the examples @ http://ecere.com/wiki/index.php?title=Networking .

The return value that is returned from Socket::OnReceive specifies how many bytes were processed. The 'count' parameter is usually checked first, and if it contains all the data we're waiting for, we're ready to process the message, and return only the size (in bytes) of the message we just processed. This means OnReceive usually processes 1 message at a time, it will get called back again when more data comes in, and in turn we will ensure we have enough data to process the following message. OnReceivePacket is an abstraction level on top, whereas the base Packet structure contains the data size of the packet, and so it only gets called with the exact amount of data ready (but it cannot work with a protocol that doesn't have this packet size at the head).

With TCP/IP, to host a service you instantiate a 'Service' class, in which you override OnAccept. If you want to accept the connection, you create an instance of your own Socket class of your choice, passing it the instance of the Service. With UDP the Service class is not used, because it is is a connectionless system.

To connect for UDP you use Socket::DatagramConnect, whereas for TCP/IP you would use Socket::Connect.

To send bytes, just use Socket::Send.

If you want to go secure, you will find the SSLSocket class under extras (requires OpenSSL). It's quite simple to port existing Ecere Socket code to the SSLSocket, you just need to make your socket class inherit from SSLSocket instead of Socket, and invoke EstablishConnection().

You will also find a small HTTP web server under net/httpserver.

Hope this helps!

-Jerome
samsam598
Posts: 212
Joined: Thu Apr 14, 2011 9:44 pm

Re: Easy Networing Programming tutorial in eC?

Post by samsam598 »

This really helps!!Appreicate.I'm studying...
samsam598
Posts: 212
Joined: Thu Apr 14, 2011 9:44 pm

Re: Easy Networing Programming tutorial in eC?

Post by samsam598 »

May I ask how to download a large file in eC?For example,download eC SDK and show the download progress in a ProgressBar.

Thanks for the help.
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: Easy Networing Programming tutorial in eC?

Post by jerome »

Hi Sam,

The File API understands the HTTP protocol. So you can do something like:

Code: Select all

File f = FileOpen("http://github.com/ecere/sdk/tarball/master", read);
if(f)
{
   f.CopyTo("ecere-sdk.tar.gz");
   delete f;
}
If you want to support progress feedback, you can copy CopyTo's implementation:

Code: Select all

File input = FileOpen("http://github.com/ecere/sdk/tarball/master", read);
if(input)
{
   File f = FileOpen("ecere-sdk.tar.gz", write);
   if(f)
   {
      byte buffer[65536];
      while(!input.Eof())
      {
         uint count = input.Read(buffer, 1, sizeof(buffer));
         if(count && !f.Write(buffer, 1, count))
         {
            // Write failed!
            break;
         }
         // Update progress bar here...
      }
      delete f;
   }
}
If you want to support another network protocol, of course you can always implement it using the Socket class.

Cheers!

Jerome
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: Easy Networing Programming tutorial in eC?

Post by jerome »

A few additional notes:

The github link above won't work, because it will redirect you to an HTTPS connection.
1. You'd need to handle the redirection (Something we should perhaps do automatically when you use FileOpen), with code like this:

Code: Select all

File SmartFileOpen(char * fileName)
{
   bool opened = false;
   HTTPFile f { };
   char relocation[MAX_LOCATION * 4];
   char location[MAX_LOCATION * 4];
   strcpy(location, fileName);
   while(!opened)
   {     
      if(!(opened = f.OpenURL(location, null, relocation)) && !strcmp(location, relocation))
         break;
 
      if(!opened)
         printf("Relocated to %s\n", relocation);
 
      if(location[strlen(location)-1] != '/')
         PathCat(location, "..");
      if(relocation[0] == '/' && relocation[1] == '/')
      {
         strcpy(location, "http:");
         strcat(location, relocation);
      }
      else
         PathCat(location, relocation);
   }
   if(!opened) 
      delete f;
   return f;
}
2. We'd need the HTTPFile class to use the SSLSocket class (from the extras), and whatever else is required to get HTTPS support working.

The git.ecere.com link ( http://ecere.com/cgi-bin/gitweb.cgi?p=s ... er;sf=tbz2 ) will work, but does not inform us of the Content-Length, so you wouldn't be able to give progress feedback as you don't know how big the file is. EDIT: Browsers just say the size downloaded so far. Implementing HTTPS support for downloading the tarball from GitHub would be nice :D

The Ecere 0.43 setup link ( http://www.ecere.com/setup-ecere-0.43-win32.exe ) does inform you of its Content-Length, so feedback will work! Here's sample code to update a progress bar:

Code: Select all

import "ecere"
 
define app = (GuiApplication)((__thisModule).application);
 
class Form1 : Window
{
   text = "Form1";
   background = activeBorder;
   borderStyle = sizable;
   hasMaximize = true;
   hasMinimize = true;
   hasClose = true;
   size = { 416, 176 };
 
   Button button1
   {
      this, isDefault = true, text = "Download!";
 
      bool NotifyClicked(Button button, int x, int y, Modifiers mods)
      {
         File input = FileOpen("http://www.ecere.com/setup-ecere-0.43-win32.exe", read);
         if(input)
         {
            File f = FileOpen("setup-ecere-0.43-win32.exe", write);
            if(f)
            {
               byte buffer[65536];
               bool result = true;
               progressBar1.range = input.GetSize() / sizeof(buffer);
               progressBar1.progress = 0;
               while(!input.Eof())
               {
                  uint count = input.Read(buffer, 1, sizeof(buffer));
                  if(count && !f.Write(buffer, 1, count))
                  {
                     // Write failed!
                     result = false;
                     break;
                  }
                  // Update progress bar here...
                  progressBar1.progress++;
                  app.ProcessInput(false);
                  app.UpdateDisplay();
               }
               if(result)
                  progressBar1.progress = progressBar1.range;
               delete f;
            }
         }
         return true;
      }
   };
   ProgressBar progressBar1 { this, text = "progressBar1", size = { 388, 24 }, inactive = true, anchor = { left = 8, right = 12, bottom = 12 } };
}
 
Form1 form1 {};
samsam598
Posts: 212
Joined: Thu Apr 14, 2011 9:44 pm

Re: Easy Networing Programming tutorial in eC?

Post by samsam598 »

Hi Jerome,

Thank you so much for the detailed explanation!
samsam598
Posts: 212
Joined: Thu Apr 14, 2011 9:44 pm

Re: Easy Networing Programming tutorial in eC?

Post by samsam598 »

oops,networking program is hard to me. :(

Could anybody please post a short example which just do a very simple socket work:send a string from the client to the server,the server read it,display it(in a EditBox ),and then send back to client.The client receives the string and display it,compare the one it send and the one it receives,they should be the same.Wide string like Chinese should be supported.

It would be grateful if anybody help.

Regards,
Sam
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: Easy Networing Programming tutorial in eC?

Post by jerome »

Hi Sam,

I'm sorry you're struggling with the networking. I've added a SocketSample to the samples/net/ folder, I hope it helps you. Don't hesitate to ask more questions if you need more help.

I've attached it to this post as well.

Please try it out, and maybe review the explanation I posted above as well together with the sample.

Best regards,

Jerome
Attachments
socketSample.ec
(5.92 KiB) Downloaded 3937 times
samsam598
Posts: 212
Joined: Thu Apr 14, 2011 9:44 pm

Re: Easy Networing Programming tutorial in eC?

Post by samsam598 »

Hi Jerome,

Appreciated!!

Regards,
Sam
Post Reply