Dengan socket programming kita dapat melakukan komunikasi antara berbagai bahasa pemprograman, antara berbagai macam platform, dan antara dua buah komputer, antara berbagai tingkatan user. Dalam komunikasi ini ada yang bertindak sebagai server dan ada yang bertindak sebagai client. Pada bagian pertama ini kita akan membuat socket server dengan bahasa pemrogramman Perl di Linux.
Contoh source codenya adalah :
#!/usr/bin/perl -w
require ‘/home/setup/daemon/acak.pl’;
require ‘/home/setup/daemon/pop.id’;
require “/home/setup/daemon/member_util.pl”;
my ($password_file)=”/var/qmail/users/poppasswd”;
my ($assign_file)=”/var/qmail/users/assign”;
my ($assign_file_temp)=”/var/qmail/users/assign_temp”;
use DBI;
use IO::Socket;
$server_sock = new IO::Socket::INET (LocalPort => 1234, Listen => $SOMAXCONN,Proto => ‘tcp’, Reuse => 1);
unless ($server_sock)
{
die “Could not create server socket; $!”;
}
while ($sock_to_client = $server_sock->accept())
{
$sSocketData = <$sock_to_client>;
print $sSocketData;
@SockArgv = split(/:/,$sSocketData);
$request = $SockArgv[0];
if ($request eq “ae”)
{
$email=$SockArgv[1];
$password=$SockArgv[2];
$hasil = add_email($email, $password);
print “$hasil\n”;
print $sock_to_client “$hasil\n”;
}
elsif ($request eq “cp”)
{
$email=$SockArgv[1];
$old_password=$SockArgv[2];
$new_password=$SockArgv[3];
$hasil = change_password($email,$old_password,$new_password);
print “$hasil\n”;
print $sock_to_client “$hasil\n”;
}
elsif ($request eq “rm”)
{
$perintah=”/home/setup/cron/smtpd/restart_smtpd > hasil”;
system($perintah);
$perintah =”cat hasil”;
$hasil = qx/$perintah/;
$hasil =~ s/\n/<br>/g;
print “$hasil\n”;
print $sock_to_client “$hasil\n”;
}
elsif ($request eq “qr)
{
$perintah=”qmail-qstat > hasil; qmail-qread >> hasil;qmail-qstat >> hasil;echo ‘remote dan done’>> hasil;qmail-qread | grep remote | wc -l>> hasil;qmail-qread | grep done | wc -l >> hasil; ceksmtp >> hasil”;
system($perintah);
$perintah =”cat hasil”;
$hasil = qx/$perintah/;
$hasil =~ s/\n/<br>/g;
#print “$hasil\n”;
print $sock_to_client “$hasil\n”;
}
elsif ($request eq “rm”)
{
$myid=$SockArgv[1];
$totbaris=$SockArgv[2];
$perintah=”cat `find /var/qmail/queue -name $myid | grep mess` | head -$totbaris > hasil”;
system($perintah);
$perintah =”cat hasil”;
$hasil = qx/$perintah/;
$hasil =~ s/\n/<br>/g;
#print “$hasil\n”;
print $sock_to_client “$hasil\n”;
}
elsif ($request eq “dm”)
{
$myid=$SockArgv[1];
$perintah=”rm -f `find /var/qmail/queue -name $myid `”;
system($perintah);
print $sock_to_client “selesai delete\n”;
}
elsif ($request eq “nae”)
{
$email=$SockArgv[1];
$perintah=”/home/setup/daemon/refresh_user_password.pl”;
system($perintah);
$perintah=”/home/setup/daemon/update_user”;
system($perintah);
print $sock_to_client “selesai non active $email\n”;
}
$sock_to_client->close;
}
close($server_sock);
- Pada bagian pertama adalah include utility-utility yang kita butuhkan:
#!/usr/bin/perl -w
require ‘/home/setup/daemon/acak.pl’;
require ‘/home/setup/daemon/pop.id’;
require “/home/setup/daemon/member_util.pl”;my ($password_file)=”/var/qmail/users/poppasswd”;
my ($assign_file)=”/var/qmail/users/assign”;
my ($assign_file_temp)=”/var/qmail/users/assign_temp”;
use DBI;
use IO::Socket; - Pada bagian berikutnya adalah membuat socket server:
$server_sock = new IO::Socket::INET (LocalPort => 2345, Listen => $SOMAXCONN,Proto => ‘tcp’, Reuse => 1);
unless ($server_sock)
{
die “Could not create server socket; $!”;
} - Kemudian dilanjutkan dengan mendengarkan dan menjalankan komunikasi kemudian mendengarkan kembali:
while ($sock_to_client = $server_sock->accept())
{
$sSocketData = <$sock_to_client>;
print $sSocketData;
@SockArgv = split(/:/,$sSocketData);
$request = $SockArgv[0];
if ($request eq “ae”)
{
$email=$SockArgv[1];
$password=$SockArgv[2];
$hasil = add_email($email, $password);
print “$hasil\n”;
print $sock_to_client “$hasil\n”;
}
elsif ($request eq “cp”)
{
$email=$SockArgv[1];
$old_password=$SockArgv[2];
$new_password=$SockArgv[3];
$hasil = change_password($email,$old_password,$new_password);
print “$hasil\n”;
print $sock_to_client “$hasil\n”;
}
elsif ($request eq “rm”)
{
$perintah=”/home/setup/cron/smtpd/restart_smtpd > hasil”;
system($perintah);
$perintah =”cat hasil”;
$hasil = qx/$perintah/;
$hasil =~ s/\n/<br>/g;
print “$hasil\n”;
print $sock_to_client “$hasil\n”;
}
elsif ($request eq “qr)
{
$perintah=”qmail-qstat > hasil; qmail-qread >> hasil;qmail-qstat >> hasil;echo ‘remote dan done’>> hasil;qmail-qread | grep remote | wc -l>> hasil;qmail-qread | grep done | wc -l >> hasil; ceksmtp >> hasil”;
system($perintah);
$perintah =”cat hasil”;
$hasil = qx/$perintah/;
$hasil =~ s/\n/<br>/g;
#print “$hasil\n”;
print $sock_to_client “$hasil\n”;
}
elsif ($request eq “rm”)
{
$myid=$SockArgv[1];
$totbaris=$SockArgv[2];
$perintah=”cat `find /var/qmail/queue -name $myid | grep mess` | head -$totbaris > hasil”;
system($perintah);
$perintah =”cat hasil”;
$hasil = qx/$perintah/;
$hasil =~ s/\n/<br>/g;
#print “$hasil\n”;
print $sock_to_client “$hasil\n”;
}
elsif ($request eq “dm”)
{
$myid=$SockArgv[1];
$perintah=”rm -f `find /var/qmail/queue -name $myid `”;
system($perintah);
print $sock_to_client “selesai delete\n”;
}
elsif ($request eq “nae”)
{
$email=$SockArgv[1];
$perintah=”/home/setup/daemon/refresh_user_password.pl”;
system($perintah);
$perintah=”/home/setup/daemon/update_user”;
system($perintah);
print $sock_to_client “selesai non active $email\n”;
}
$sock_to_client->close;
} - Terakhir menutup socket
close($server_sock);
Kunjungi www.proweb.co.id untuk menambah wawasan anda.