Showing posts with label erlang. Show all posts
Showing posts with label erlang. Show all posts

Erlang SSL server example

Today I decided to prepare and publish simple example of SSLsockets usages. As in my previous articles about TCP & UDP sockets I decided to write simple echo server that just returns the pacage it gets. Okay, I do not whant to waist your time and suggest just to go to the article.

Erlang: UDP socket usage example with gen_udp

UDP is a widely used protocol and Erlang is a great platform for different network application different kinds. Today I decided to publish small UDP echo server example. I know that UDP socket usage is very simple in Erlang, but I think somebody will find this code usefull.

Erlang: How to integrate C with Erlang program example or Erlang NIFs (Native Implemented Functions) usage

Erlang NIF (Native Code Functions) example. Basically article shows how to connect the library written in C and compiled with GNU gcc to the erlang code. That is very important when speed optimisation is required for some Erlang code piece. Example is very basic and just shows types conversion between two languages.


Read complete article: Erlang: How to integrate C with Erlang program example or Erlang NIFs (Native Implemented Functions) usage

Erlang geocoding wrapper for Google Geocoding service (Google Maps v3)

I have just published an article in my blog about accessing Google geocoding service from Erlang and published wrapper module for Google geocoding cervice on GitHub. It is based on Google Maps V3 cervice, as Google Maps v2 is going to be depricated soon.

Erlang string to IP coversion and IP to string conversion

Today I had to convert string to IP for one of my Erlang modyles. Preaty simple operation, but I decided to describe this in my blog.

You can read the article here: String to IP tuple and tuple to IP address

Sorry, I have moved my blog to my own blogging platform, so you can rad all the articles there.

Erlang float to integer

Today I decided to write very small article. I think it may be interesting for programmers who came to Erlang from Java, C++ or C. From the point of view of Erlang developer everything is very simple. You just need to round or truncate the float and it will become an integer. Here is an example:

Eshell V5.8.4  (abort with ^G)
1> is_float(round(1.5)).
false
2> is_integer(round(1.5)).
true
3> is_float(trunc(1.5)).  
false
4> is_integer(trunc(1.5)).
true
5> round(1.5).            
2
6> trunc(1.5).            
1

Erlang TCP client & server sockets with gen_tcp


Article have been moved to my new blog: Erlang TCP server & TCP client sockets with gen_tcp

Today I am going to write a brief article about Erlang gen_tcp usage. Probably the reader already has experience with sockets programming. But just to remind server architecture. First of all listening socket should be created, than server is going into accept state waiting for new connection. Once new connection is accepted server creates new thread to process incoming packets. While server waits for another connection.

Erlang echo server code:

Erlang captcha code for websites

Today I want to publish simple Erlang captcha library. It uses very simple algorithm - code & captcha image is generated and file name contains code itself. So, if file exists, than code entered correctly. Once code is entered, file is deleted. One can reload (in terms of web) or generate new code. In this case old file will be removed and new generated.

Sorrym but this article have been moved to my new blog on erlycoder.com

Read complete article: Erlang captcha code for websites

How to send email with attachment from Erlang

Today I am going to publish small script for sending email from Erlang. It is quite simple. The idea was taken from some other script (not mine, but I do not know who is an author. Sorry for that). Actually many linux users or at least admins will find it familar.
Main difference from original code, that I have found in the Internet, is that this script allows attachments. It does not cover all possible cases, but you can simply modify it for your needs.

Sorry, but this article was permanentaly moved to my new blog erlycoder.com

Read complete article: How to send email with attachment from Erlang

Erlang convert IP to integer & integer to IP.

Jut needed to convert ip to integer for Erlang project and have not found code via Google. So decided to post it here for anybody who will find it helpful.

First function converting IP tuple to integer:

ip_to_int({A,B,C,D}) -> (A*16777216)+(B*65536)+(C*256)+(D).

Code to convert integer to IP tuple:

int_to_ip(Ip)->	{Ip bsr 24, (Ip band 16711680) bsr 16, (Ip band 65280) bsr 8, Ip band 255}.

And brief dumb example:

Eshell V5.8.4  (abort with ^G)
1> Ip = {1,2,3,4}.
{1,2,3,4}
2> db_server:int_to_ip(db_server:ip_to_int(Ip)).
{1,2,3,4}
3> 

Hope this code helped you, if you have reached this page :)

What is Erlang open_port and os:cmd?

Sometimes it happens that you need to execute external command. Same happens when you are programming Erlang. Usually it is enough to use os:cmd. It executes the command and returns the result. Like the following

LsOut = os:cmd("ls"), % on unix platform

But sometimes you need to start the process and communicate with it via standard input and output. For example most of linux users know GnuChess program. And may be somebody even tried to start it from the shell without GUI. So, the following simple example will show how to start GnuChess and communicate with the process from Erlang.

-module(pipe).
-author('Sergiy Dzysyak ').

-compile(export_all).

start()->
	spawn(?MODULE, read, []).
	
read() ->
  Port = open_port({spawn,"/usr/bin/gnuchess -xe"},[binary,{line, 255}]),
  do_read(Port).

do_read(Port) ->
  receive
    {Port,{data,Data}} ->
    	io:format("Data: ~p~n",[Data]);
    {Port,eof} ->
      read();
    {go, Go} ->
    	Port ! {self(), {command, Go}};
    Any ->
      io:format("No match fifo_client:do_read/1, ~p~n",[Any])
  end,
  do_read(Port). 

Example is very basic and to stat it you should do the following:

[serg@localhost erl.chess]$ erl
Erlang R14B03 (erts-5.8.4) [source] [64-bit] [rq:1] [async-threads:0] [kernel-poll:false]

Eshell V5.8.4  (abort with ^G)
1> c(pipe).
{ok,pipe}
2> Pid = pipe:start().
<0.38.0>
3> Pid ! {go, <<"e2e4\n">>}.
{go,<<"e2e4\n">>}
Data: {eol,<<"Chess">>}
Data: {eol,<<"Adjusting HashSize to 1024 slots">>}
Data: {eol,<<"1. e2e4">>}
Data: {eol,<<"1. ... e7e5">>}
Data: {eol,<<"My move is: e7e5">>}
4> 

In a similar way communication via linux named pipes can be organized with external programs or even more complicated goals may be achieved. I have not tried to load drivers & communicate with them via port_open, but if I will get a minute, I will try and describe it here in my blog...

Example of Erlang crossdomain policy server for Adobe Flash

Latest version of article & code can be found on my new blog: Example of Erlang crossdomain policy server for Adobe Flash

Some time ago I had to use sockets from Adobe Flash/Flex, so needed a simple one implemented in Erlang. I do not suggest to use it exactly like it listed below, but you may find some idea for your project there. I hope that you know what I am talking about, if you are reading this article, so do not want to waist your time...