Skip to content
Goliath1 edited this page Dec 15, 2014 · 21 revisions

Net

A library that provides creating net sockets. ##Introduction One of the way to connect two applications is to use Berkeley sockets (or another net sockets based on Berkeley ones). One can create socket-server and socket-client and the library helps to do it in a simple way.

Structure of the library

The library consists of several classes and interfaces: SocketServer, SocketClient, SocketServer/SocketServerBase etc. For those who are not going to develop the library the most important classes are SocketServer and SocketClient representing an interface of the interaction itself. As one may guess by naming, SocketServer class has methods and fields for server socket and SocketClient has methods and fields for client one.

Socket steps of interaction

There are seven steps of socket interaction and these steps are: creating, binding, listening, connecting/accepting, reading/writing, preparing to close, closing. Let's consider them for sockets to be a client and a server.

Client steps

The first action you have do with every socket is to create it. As mentioned before, the class representing client socket is SocketClient so to create client you should use constructor,

var client = new SocketClient("127.0.0.1", 60001, -1, true);

The arguments are:

  • first - host IP-address, string;
  • second - port, integer;
  • third - reconnect delay, ms, integer;
  • fourth - flag "isWithLength" indicating if the length of datagramm is added to it, boolean.

The first argument contains a server's IP, default value is "127.0.0.1". The second argument is a number of server's port to connect to. The third argument is a time, in milliseconds, the client has to wait for before it is to connect again; it's needed if connection is broken or the process of connecting is failed. -1 means that there is no reconnect at all, default value is -1. The fourth argument is a flag that indicates if the length of a datagramm is sent with the datagramm itself. If true, the datagramm consists of two parts: a data and a four-byte integer being equal to length of data. The default value is true.

It is strongly recommended to use sockets when isWithLength is true only.

Note also, that program will surely crash if client's isWithLength is not equal to server's one.

If all the default values seems to be what you need, you may type just

var client = new SocketClient(60001);

Next step one should make is binding socket to local end point. Although it is neccessary to bind server to pair IP/port, there is no need to do client the same way. Anyhow, the Connect method does it automatically so one doesn't need to make one's head messed.

The third step is listening and it relates to server only.

The fourth step of interaction is connecting. The client socket connects to server by using server's host/port - as you remember, we typed these in constructor's arguments. Moreover, the Connect method is in constructor too so you need to do nothing but what has already been done. It is guarenteed, that there will be no data sent or received before connecting ends and client is completely configured.

By calling the constructor you "simultaneously" create the client and connect it to the server.

The fifth and the main step is sending and receiving a data. To send data one should form it by using a BytesOutput class and send by send method. The code is

var data:BytesOutput = new BytesOutput();
data.writeString("Hello, World!");
client.send(data);

It's pretty simple.

To receive data one should use callback with BytesInput -> Void type in client's onData signal. For example, server sent a number with double-precision floating-point format. You may receive it next way:

client.onData.add(receiveCallback);

/*Some code.*/

static function receiveCallback(b_in:BytesInput):Void
{
    trace(b_in.readDouble());
}

Of course, it is not neccesary callback to be static - it is just because used in static main function. You may declare callback inside main like this:

function receiveCallback(b_in:BytesInput):Void
{
    trace(b_in.readDouble());
}
client.onData.add(receiveCallback);

But the best practice is using anonymous function:

client.onData.add(function(b_in:BytesInput)
{
    trace(b_in.readDouble());
});

When all the sending/receiving is done, the socket has to be correctly destroyed. There is a Shutdown function to do it the right way, it prepares the socket to destroy. In conclusion, there is a Close function to destroy the socket. Both these functions are in SocketClient's method destroy so all you need to do is to call it:

client.destroy();

It is guaranteed, that sending/receiving process executing when destroy is called ends correctly.

To destroy the client just call the destroy method.

Server steps

The class representing a server is SocketServer. Creating a server is even simpler than doing a client. The only argument one should set is port:

var server:SocketServer = new SocketServer(60001);

The argument is a number of port the clients to connect to. The considering why there is just one argument lies below.

Notice, that port in server's and client's constructors must be the same.

As noted before, the second step is binding a socket to local end point. Although it was mentioned that binding is neccessary for server, the host IP needed to bind to is not a constructor's argument. The reason why is that IP is always 127.0.0.1. Furthermore, thre is no need in reconnect so there is no reconnect delay; an isWithLength field can be set by direct access, its sence is just the same as client's one.

The third step is listening. It is implemented by socket's Listen method, the default count of clients listened simultaneously is one thousand; this value can not be changed. The fourth step is accepting the clients. To accept it is used asynchronous methods BeginAccept and EndAccept. All these methods - Bind, Listen, BeginAccept - is in the constructor so by calling it you do all the work needed to prepare the interaction. The EndAccept method is called in accept callback.

To prepare a server to interact one doesn't need to do anything but calling the constructor.

Clone this wiki locally