Aplikasi Form UDP |
UDP, singkatan dari User Datagram Protocol, adalah salah satu protokol lapisan transpor TCP/IP yang mendukung komunikasi yang tidak andal (unreliable), tanpa koneksi (connectionless) antara host-host dalam jaringan yang menggunakan TCP/IP. Protokol ini didefinisikan dalam RFC 768.
Daftar Komponen
Rincian Codding
1. Form
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 UDP_Form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void uDPServerToolStripMenuItem_Click(object sender, EventArgs e)
{
UDP_Server udp_server = new UDP_Server();
udp_server.Show();
}
private void uDPClientToolStripMenuItem_Click(object sender, EventArgs e)
{
UDP_Client udp_client = new UDP_Client();
udp_client.Show();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace UDP_Form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void uDPServerToolStripMenuItem_Click(object sender, EventArgs e)
{
UDP_Server udp_server = new UDP_Server();
udp_server.Show();
}
private void uDPClientToolStripMenuItem_Click(object sender, EventArgs e)
{
UDP_Client udp_client = new UDP_Client();
udp_client.Show();
}
}
}
2. Server
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;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
namespace UDP_Form
{
public partial class UDP_Server : Form
{
public UDP_Server()
{
InitializeComponent();
}
private void serverTheard() // membuat metode
{
UdpClient clUdp = new UdpClient(int.Parse(textBox1.Text));
//membuat sebuah objek udpclient baru
while (true)
{
IPEndPoint remoteIp = new IPEndPoint(IPAddress.Any, 0);
//membuat sebuah objek ipendpoint yg akan mengirim paket
byte[] receiveByte = clUdp.Receive(ref remoteIp);
string returnData = Encoding.ASCII.GetString(receiveByte);
//untuk memberikan delegasi kepada listbox
listBox1.Invoke(new MethodInvoker(delegate()
{
listBox1.Items.Add(remoteIp.Address.ToString() + " : " + returnData.ToString());
}));
}
}
private void Form1_Load(object sender, EventArgs e)
{
Thread svrThread = new Thread(new ThreadStart(serverTheard));
svrThread.Start();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
namespace UDP_Form
{
public partial class UDP_Server : Form
{
public UDP_Server()
{
InitializeComponent();
}
private void serverTheard() // membuat metode
{
UdpClient clUdp = new UdpClient(int.Parse(textBox1.Text));
//membuat sebuah objek udpclient baru
while (true)
{
IPEndPoint remoteIp = new IPEndPoint(IPAddress.Any, 0);
//membuat sebuah objek ipendpoint yg akan mengirim paket
byte[] receiveByte = clUdp.Receive(ref remoteIp);
string returnData = Encoding.ASCII.GetString(receiveByte);
//untuk memberikan delegasi kepada listbox
listBox1.Invoke(new MethodInvoker(delegate()
{
listBox1.Items.Add(remoteIp.Address.ToString() + " : " + returnData.ToString());
}));
}
}
private void Form1_Load(object sender, EventArgs e)
{
Thread svrThread = new Thread(new ThreadStart(serverTheard));
svrThread.Start();
}
}
}
3. Client
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;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
namespace UDP_Form
{
public partial class UDP_Client : Form
{
public UDP_Client()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
UdpClient ucl = new UdpClient();
try
{
ucl.Connect(textAlmt.Text, int.Parse(textPort.Text));
//menghubungkan ke server
byte[] datasend = Encoding.ASCII.GetBytes(textPesan.Text);
//merubah string menjadi byte
ucl.Send(datasend, datasend.Length);
//mengirim data ke server
ucl.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
namespace UDP_Form
{
public partial class UDP_Client : Form
{
public UDP_Client()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
UdpClient ucl = new UdpClient();
try
{
ucl.Connect(textAlmt.Text, int.Parse(textPort.Text));
//menghubungkan ke server
byte[] datasend = Encoding.ASCII.GetBytes(textPesan.Text);
//merubah string menjadi byte
ucl.Send(datasend, datasend.Length);
//mengirim data ke server
ucl.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
Hasil UDP Client Server |
INGAT!!
Jika Anda kurang puas atau ingin menambahkan ide
Anda dapat mengkostumnya di Form/View code nya
Untuk keterangan lebih lanjut lihat slide
Download Slide
Download Program
0 komentar:
Posting Komentar