Networking Objects

Help with the Ecere networking library: Socket, Service, SSL, Distributed Objects, HTTP, XML protocols, etc.
Post Reply
sacrebleu
Posts: 27
Joined: Sun Jan 17, 2010 12:37 pm

Networking Objects

Post by sacrebleu »

How to use TCP/IP, Sockets, Servers and UDP? :mrgreen:
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: Networking Objects

Post by jerome »

There is a Ecere networking quickstart guide on the wiki.

There are some networking samples in the sdk/samples/net folder.

For UDP sockets, take a look at UDPSample.

For TCP/IP, go up one directory and look at the threads/threadsAndListBoxes sample.
It teaches you threads, listboxes and sockets all at once. It might be a bit much, but what a nice bundle :)

Here is the relevant Socket code:

Code: Select all

// This class defines our data packets layout:
class AddPacket : Packet
{
   Seconds delay;
   char string[1];
   // Data follows...
}
 
class MySocket : Socket
{
   // This is invoked every time we receive a packet:
   void OnReceivePacket(Packet packet)
   {
      AddPacket addPacket = (AddPacket)packet;
      AddThread{}.AddItem(addPacket.string, addPacket.delay);
   }
 
   // This is the method we will call to do something useful... It sends a packet to the server.
   void AddItem(char * string, Seconds delay)
   {
      int len = strlen(string);
      uint size = sizeof(class AddPacket) + len;
      AddPacket packet = (AddPacket)new byte[size];
 
      packet.size = size;
      packet.delay = delay;
      CopyBytes(packet.string, string, len+1);     
      SendPacket(packet);
 
      delete packet;
   }
}
 
// Our service simply instantiates a MySocket object upon accepting a connection
class MyServer : Service
{
   void OnAccept()
   {
      MySocket { this };
   }
}
 
// We're listening on port 1234
MyServer server { port = 1234 };
 
class MyApplication : GuiApplication
{
   bool Init()
   {
      myApp = this;
      // Start the server here
      server.Start();
      return true;
   }
}
 
MyApplication myApp;
The Form1 class then has 2 MySocket objects:

Code: Select all

   MySocket socket {};
   MySocket socket2 {};
And it invokes Connect on them (on localhost) on creation:

Code: Select all

      socket.Connect("localhost", 1234);
      socket2.Connect("localhost", 1234);
You will find other useful examples under samples/net, notably:

eCom - Implements a very simplistic (and useful) chat and file sharing system on a single connected socket
eIRC - The beginnings of an IRC client
smtp - Send mail!
XMLSample - How to setup XML based protocol on a TCP/IP socket
httpserver - a basic web server

networkfile - implements a very specific protocol built in Ecere for file streaming complete with seeking and caching.
DCOMSample - shows how to use the eC distributed object system, so that you don't actually need to write any networking code at all.

You will also find Socket code in the games/Chess and games/Othello, and more distributed objects code in games/ruff and games/scrabble.
fedor_bel
Posts: 21
Joined: Sun Mar 14, 2010 4:46 pm

Re: Networking Objects

Post by fedor_bel »

Dear Jerome,

I am trying to use the Socket and the Service class. They work great!
But still I have got a question: how to make the service to stop listening without shutting down all already connected sockets.
I have a listening service, once a connection is established, I create a socket, which is connected - all is great. then I want the service to stop listening, because I need only one socket connected, not more and I dont want an open port in a listening state. The already connected socket should remain connected, i will use it, I just want to shutdown the now unneeded listening socket. How to do it? Please, forgive me my stupidity, I tried to figure it out through the Samples, but did nof find such scenario there. btw, the processAlone flag is just sweet ;) - I was so glad when I have discovered it and it did what i wanted :-)

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

Re: Networking Objects

Post by jerome »

Dear Fedor,

The Service class does not have that functionality right now.
It never came up as a use case until you asked for it just now :)

I assume it is perfectly reasonable for you to want to close the listening socket without terminating the established connections...

The API was originally written with the idea of a 'Service' that keeps running serving multiple clients, and thus as long as the service is running it keeps listening. If you no longer want to accept connections, then you simply don't create a Socket object in the Service's OnAccept anymore, so even though the socket is still listening, it will simply accept and terminate the connection right away (with a closesocket), which (in theory) shouldn't make it much of a security threat. The biggest problem left would be if you want to listen on that same port in another application or instance of that application...

Now if you really want to close that socket, you will need to dive into the Ecere code :)

First, I'd suggest to put a feature request on mantis.
By the way since you seem to be making good use of Ecere, you're most welcome to join the project as a contributor :) I'm not sure if I've set you up with the CodeGuard repository yet? If not you can email or private message me about it :)

I've attached two files with little tweaks to try to implement the functionality that you want... It adds a 'disconnectClientsOnStop' bool property (which defaults to 'true') to the Service class. There are still rough edges, for example starting the services again will add the Service a second time in the services link list. We should add another bool status flag in Service to tell whether the service is started or not, rather than just check 's' for non 0.

Cheers,

Jerome
Attachments
network.ec
(7.08 KiB) Downloaded 1849 times
Service.ec
(4.24 KiB) Downloaded 1871 times
fedor_bel
Posts: 21
Joined: Sun Mar 14, 2010 4:46 pm

Re: Networking Objects

Post by fedor_bel »

Dear Jerome,
Thank you for the files. I am wondering now how to use them? I have always been interested in doing some in-depth groung level programming, especially in such a nice project, as this!!!
I have access to Mantis with Access Level reporter.
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: Networking Objects

Post by jerome »

Hi Fedor,

You did build the SDK for the 0.44pre1 source tarball, right?

These files go into ecere/src/net/

You can load up the ecere.epj project file in the IDE to build the Ecere runtime library.

I recommend you install some files and folders comparison tool for a start :)
A must for teamwork :)

It would be best to get together on IRC sometimes, although that might be a bit difficult with your timezone...

Cheers,

Jerome
fedor_bel
Posts: 21
Joined: Sun Mar 14, 2010 4:46 pm

Re: Networking Objects

Post by fedor_bel »

Hi, Jerome,
Sure, the ecere project built very well and I happen to have some comparison tools. I dont have much time, because I am busy at my main job, but have much interest, it is a pleasure to take part in such a nice project. Maybe we could do the communication in some separate forum thread, that way we could keep the history and dont worry of time synchronisation that much.
Cheers,
Fedor
Post Reply