/* |
Download source code github https://github.com/doditsuprianto/Implementasi-Socket-TCP-dengan-Java-Swing
/* |
Download source code github https://github.com/doditsuprianto/Implementasi-Socket-TCP-dengan-Java-Swing
Berikut ini contoh implementasi komunikasi perangkat menggunakan Socket pada protocol TCP. Socket yang dibangun bersifat asynchrounus.
BAik langsung saja, buka Visual Studio NET, Pilih menu File > New > Project
Beri nama project “DemoSocketClientServer” dan letakkan file project sesuai keinginan. Kemudian ikuti langkah-langkahnya sesuai video di bawah ini.
Berikut ini kode program yang perlu dibuat:
varGlobal.cs |
using System; using System.Collections.Generic; namespace DemoSocketClientServer public static string alamatIPServer; |
SocketTCP.cs |
using System; namespace DemoSocketClientServer class SocketTCP //############################ public static void
StartClient(string pesan)
result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(1)); if (client.Connected)
client.Shutdown(SocketShutdown.Both); public static void
ConnectCallback(IAsyncResult ar) try catch } public static void
Send(Socket client, String data)
client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback),
client); private static void
SendCallback(IAsyncResult ar) catch } public static void
Receive(Socket client) private static void
ReceiveCallback(IAsyncResult ar) int bytesRead = client.EndReceive(ar); if (bytesRead > 0)
receiveDone.Set();
//################################## // SOCKET SERVER //################################## public static ManualResetEvent allDone = new ManualResetEvent(false); public static void
StartListening() { IPAddress
ipAddress = IPAddress.Parse(varGlobal.alamatIPServer); Socket
listener = new
Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); try {
listener.Bind(localEndPoint); while (true) { } } catch (Exception e) { } public static void
AcceptCallback(IAsyncResult ar) { allDone.Set(); } public static void
ReadCallback(IAsyncResult ar) { String terimaPesan = String.Empty; StateObject
state = (StateObject)ar.AsyncState; if (bytesRead > 0) { terimaPesan = state.sb.ToString(); if (terimaPesan != "") {
varGlobal.terimapesandiserver = terimaPesan; } } } } } |
frmDemoSocketClientServer.cs |
using System; namespace DemoSocketClientServer { public partial class frmDemoSocketClientServer : Form { public frmDemoSocketClientServer() { private void btnListen_Click(object sender, EventArgs e) { Thread thr = new Thread(listenSocket); if (txtPortServer.Text != "" &&
txtIPServer.Text != "") {
thr.Start(); } else {
MessageBox.Show("Lengkapi port dan alamat
IP terlebih dahulu !!!"); } } private void listenSocket() { varGlobal.port
= Int16.Parse(txtPortServer.Text); } private void btnKirimDataKeServer_Click(object sender, EventArgs e) {
varGlobal.alamatIPServer = txtIPTujuan.Text; } private void timer1_Tick(object sender, EventArgs e) {
txtTerimaData.Clear(); } } } |
© Dodit Suprianto 2016 . Powered by Blogger . Created by Weblyb