Assalamu Alaikum WR.WB
berjumpa lagi
sama saya Septian dalam pemrograman c# ;-). baiklah pada kesempatan kali ini saya
ingin menjelaskan Parsing data sederhana.
Protocol
komunikasi di internet sangat bergantung pada format teks. Ketika anda
melakukan browsing ke google.com, maka yang
terjadi adalah pertukaran string teks terus-menerus antara
router-router sampai dengan server google.
Sebagai contoh
adalah protokol TCP yang memiliki format sebagai berikut:
Misalnya awal
dari protokol ditandai dengan karakter “abcde”, akhir dari satu paket
protokol ditandai dengan flag “edcba”. Kemudian 2 byte berikutnya
menunjukkan source port, diikuti 2 byte destination address. Lalu HLEN
sebanyak 5 byte yang menunjukkan banyaknya data. Setelah itu data sebanyak
HLEN. Terdapat trailer 4 byte, sebelum ditutup CRC 4 byte. Buat
program untuk memisah-misahkan satu paket protokol. Misalnya data yang
diterima dari internet sebagai berikut:
Program anda harus bisa mengambil satu
paket protokol yang ditandai dengan flag awal dan akhir protokol. Kemudian
memisah-misahkan source port, destination port, data dan trailer.
Berikut adalah
desain Form1 :
Dan ini adalah desain Form2 :
Tampilan awal saat
masuk ke Form 3 :
Tampilan saat
penginputan protokol, dan parsing datanya di Form 3 :
berikut
adalah coding untuk form 1 :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace parsing_data
{
public partial class Form1 : Form
{
string a, b, c;
public Form1()
{
InitializeComponent();
}
private void
timer1_Tick(object sender, EventArgs e)
{
if (pb1.Value < 100)
{
pb1.Value += 1;
}
else if (pb1.Value ==
100)
{
timer1.Stop();
Form2 Form1= new
Form2();
Form1.Show();
this.Hide();
}
}
private void
timer2_Tick(object sender, EventArgs e)
{
a
= label1.Text;
b
= a.Substring(a.Length - 1, 1);
c
= a.Substring(0, a.Length - 1);
label1.Text = b + c;
}
}
}
Dan berikut
adalah coding untuk form2 :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace parsing_data
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void
button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void
button2_Click(object sender, EventArgs e)
{
Form3 Form2 = new Form3();
Form2.Show();
this.Hide();
}
}
}
Dan berikut
adalah coding utama di form3 :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace parsing_data
{
public partial class Form3 : Form
{
int awal, akhir;
string i, j, k;
string contoh;
public Form3()
{
InitializeComponent();
}
private void
button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
for (int p = this.Width; p < 767; p++)
{
this.Width = p;
}
}
private void
Form3_Load(object sender, EventArgs e)
{
this.Width = 158;
}
private void
button2_Click(object sender, EventArgs e)
{
contoh = labelcontoh.Text;
awal = contoh.IndexOf("abcde");
akhir = contoh.LastIndexOf("edcba");
textPotocol.Text = contoh.Substring(awal, akhir - 3);
}
private void
button3_Click(object sender, EventArgs e)
{
int sourceport = awal + 5;
int destinationaddress = awal + 7;
int HLEN = awal + 9;
int Data = awal + 14;
int Trailer = awal + 29;
int CRC = awal + 33;
textsourceport.Text = contoh.Substring(sourceport, 2);
textdestinationaddress.Text = contoh.Substring(destinationaddress, 2);
textData.Text = contoh.Substring(Data, 15);
textHLEN.Text = contoh.Substring(HLEN, 5);
textTrailer.Text = contoh.Substring(Trailer, 4);
textCRC.Text = contoh.Substring(CRC, 4);
}
private void
button5_Click(object sender, EventArgs e)
{
textsourceport.Text = "";
textdestinationaddress.Text = "";
textData.Text = "";
textHLEN.Text = "";
textTrailer.Text = "";
textCRC.Text = "";
textPotocol.Text = "";
}
private void timer2_Tick(object
sender, EventArgs e)
{
i
= label10.Text;
j
= i.Substring(i.Length - 1, 1);
k
= i.Substring(0, i.Length - 1);
label10.Text = j + k;
}
private void
Form3_FormClosed(object sender, FormClosedEventArgs e)
{
Form2 Form3 = new Form2();
Form3.Show();
}
}
}
Agar lebih jelas langsung lihat video ini :





No comments:
Post a Comment