Setup OSC Server And Client At Server With Perl
The following post demonstrate who you can setup a OSC server at your server (i am on bluehost) and also built a webpage as a OSC client for sending OSC messages to the server by using cgi interface.
We love Perl so let's start.
1. Install Net::OpenSoundControl Perl module at your server
- Download it from CPAN
- Put in a folder at your server
- Go there with the Terminal using ssh
- Follow the commands
perl Makefile.PL PREFIX=$HOME/perl LIB=$HOME/perl make make install
2.Create a folder (download) at your server with the content
- index.html
<html> <head> <title>Send OSC</title> </head> <body> <h1>Send OSC</h1> <p> <form action="sendRecieveOSC.cgi" method="post" > Send OSC message to 7777: <input type="text" size="30" name="message"> <input type="submit" name="submit" value="Submit!"> <input type=reset name=reset value="Reset"> </form> </body> </html>
- sendRecieveOSC.cgi
#!/usr/bin/perl
use lib '$HOME/perl';
use Net::OpenSoundControl::Client;
use Net::OpenSoundControl::Server;
use Data::Dumper qw(Dumper);
##==== BASIC STUFF ================================
require "subparseform.lib";
&Parse_Form;
$message = $formdata{'message'};
print "Content-type: text/html\n\n";
print "OSC Client: Sending out messages to localhost, port 7777\n";
print "You send the message: $message \n";
##===== CLIENT ==================================
my $client = Net::OpenSoundControl::Client->new(Host => "127.0.0.1", Port => 7777);
$client->send([$message]);
- OSC_server.pl
#!/usr/bin/perl
use strict;
use lib '$HOME/perl';
#use warnings;
use Net::OpenSoundControl::Server;
use Data::Dumper qw(Dumper);
sub dumpmsg {
print "[$_[0]] ", Dumper $_[1];
}
my $server =
Net::OpenSoundControl::Server->new(Port => 7777, Handler => \&dumpmsg)
or die "Could not start server: $@\n";
print "[OSC Server] Receiving messages on port 7777\n";
$server->readloop();
- subparseform.lib
sub Parse_Form {
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
if ($ENV{'QUERY_STRING'}) {
@getpairs =split(/&/, $ENV{'QUERY_STRING'});
push(@pairs,@getpairs);
}
} else {
print "Content-type: text/html\n\n";
print "<P>Use Post or Get";
}
foreach $pair (@pairs) {
($key, $value) = split (/=/, $pair);
$key =~ tr/+/ /;
$key =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~s/<!--(.|\n)*-->//g;
if ($formdata{$key}) {
$formdata{$key} .= ", $value";
} else {
$formdata{$key} = $value;
}
}
}
1;
3.Change the permissions of the .cgi file
chmod 755 sendRecieveOSC.cgi