Monday, October 5, 2020

Implementasi Socket TCP Asynchrounus Menggunakan Java Swing







/*
---------------------------------------------------------
 Dodit Suprianto dodit.suprianto@polinema.ac.id
 Mata Kuliah Pemrograman Jaringan
 September 2020, Implementasi Socket TCP pada Java Swing
 http://doditsuprianto.blogspot.com
---------------------------------------------------------
 Thanks and Credit to Steven Ou (ochinchina)
 https://gist.github.com/ochinchina/72cc23220dc8a933fc46
---------------------------------------------------------
 */

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.util.concurrent.atomic.AtomicInteger;

import java.nio.channels.AsynchronousServerSocketChannel;

public class frmDemoSocketJava {
   
private JTabbedPane tabbedPane1;
   
private JPanel panel1;
   
private JTextField txtPortServer;
   
private JTextField txtIPServer;
   
private JButton btnListen;
   
private JTextField txtPesanDiterimaServer;
   
private JTextField txtPortClient;
   
private JTextField txtAlamatIPTujuan;
   
private JTextField txtPesanDikirimKeServer;
   
private JButton btnKirim;

   
public frmDemoSocketJava() {
       
btnListen.addActionListener(new ActionListener() {
           
@Override
           
public void actionPerformed(ActionEvent e) {
               
//JOptionPane.showMessageDialog(null, "Tombol LISTENER diklik");
               
try {
                   
new Thread(() -> {
                       
try {
                            EchoServer(
txtIPServer.getText(), Integer.parseInt(txtPortServer.getText()));
                        }
catch (IOException ioException) {
                            ioException.printStackTrace();
                        }
                    }).start();
                }
catch(Exception ex) {
                   
JOptionPane.showMessageDialog(null, ex.getMessage());
                }
            }
        });

       
btnKirim.addActionListener(new ActionListener() {
           
@Override
           
public void actionPerformed(ActionEvent e) {
               
//JOptionPane.showMessageDialog(null, "Tombol KIRIM diklik");
               
try {
                   
AtomicInteger messageWritten = new AtomicInteger(0);
                   
AtomicInteger messageRead = new AtomicInteger(0);

                    EchoClient(
txtAlamatIPTujuan.getText(), Integer.parseInt(txtPortClient.getText()), txtPesanDikirimKeServer.getText(), messageWritten, messageRead);
                }
catch (Exception ex) {
                   
JOptionPane.showMessageDialog(null, ex.getMessage());
                }
            }
        });
    }

   
public static void main(String[] args){
       
JFrame gui = new JFrame("Socket Java");
       
gui.setContentPane(new frmDemoSocketJava().panel1);
       
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
gui.pack();
       
gui.setVisible(true);
    }

   
//////////////////////////
    // Socket Client Helper //
    //////////////////////////
   
public void EchoClient( String host, int port, final String message, final AtomicInteger messageWritten, final AtomicInteger messageRead ) throws IOException {
       
//create a socket channel
       
AsynchronousSocketChannel sockChannel = AsynchronousSocketChannel.open();

       
//try to connect to the server side
       
sockChannel.connect( new InetSocketAddress(host, port), sockChannel, new CompletionHandler<Void, AsynchronousSocketChannel >() {
           
@Override
           
public void completed(Void result, AsynchronousSocketChannel channel ) {
               
//start to read message
               
startRead( channel,messageRead );

               
//write an message to server side
               
startWrite( channel, message, messageWritten );
            }

           
@Override
           
public void failed(Throwable exc, AsynchronousSocketChannel channel) {
               
System.out.println( "fail to connect to server");
            }

        });
    }

   
private void startRead( final AsynchronousSocketChannel sockChannel, final AtomicInteger messageRead ) {
       
final ByteBuffer buf = ByteBuffer.allocate(2048);

        sockChannel.read(
buf, sockChannel, new CompletionHandler<Integer, AsynchronousSocketChannel>(){

           
@Override
           
public void completed(Integer result, AsynchronousSocketChannel channel) {
               
//message is read from server
               
messageRead.getAndIncrement();

               
//print the message
               
System.out.println( "Read message:" + new String( buf.array()) );
            }

           
@Override
           
public void failed(Throwable exc, AsynchronousSocketChannel channel) {
               
System.out.println( "fail to read message from server");
            }
        });
    }

   
private void startWrite( final AsynchronousSocketChannel sockChannel, final String message, final AtomicInteger messageWritten ) {
       
ByteBuffer buf = ByteBuffer.allocate(2048);
       
buf.put(message.getBytes());
       
buf.flip();
        messageWritten.getAndIncrement();
        sockChannel.write(
buf, sockChannel, new CompletionHandler<Integer, AsynchronousSocketChannel >() {
           
@Override
           
public void completed(Integer result, AsynchronousSocketChannel channel ) {
               
//after message written
                //NOTHING TO DO
           
}

           
@Override
           
public void failed(Throwable exc, AsynchronousSocketChannel channel) {
               
System.out.println( "Fail to write the message to server");
            }
        });
    }

   
///////////////////
    // Socket Server //
    //////////////////
   
public void EchoServer( String bindAddr, int bindPort ) throws IOException {
       
InetSocketAddress sockAddr = new InetSocketAddress(bindAddr, bindPort);

       
//create a socket channel and bind to local bind address
       
AsynchronousServerSocketChannel serverSock AsynchronousServerSocketChannel.open().bind(sockAddr);

       
//start to accept the connection from client
       
serverSock.accept(serverSock, new CompletionHandler<AsynchronousSocketChannel,AsynchronousServerSocketChannel >() {

           
@Override
           
public void completed(AsynchronousSocketChannel sockChannel, AsynchronousServerSocketChannel serverSock ) {
               
//a connection is accepted, start to accept next connection
               
serverSock.accept( serverSock, this );
               
//start to read message from the client
               
startRead( sockChannel );

            }

           
@Override
           
public void failed(Throwable exc, AsynchronousServerSocketChannel serverSock) {
               
System.out.println( "fail to accept a connection");
            }

        });
    }

   
private void startRead( AsynchronousSocketChannel sockChannel ) {
       
final ByteBuffer buf = ByteBuffer.allocate(2048);

       
//read message from client
       
sockChannel.read( buf, sockChannel, new CompletionHandler<Integer, AsynchronousSocketChannel >() {

           
/**
             * some message is read from client, this callback will be called
             */
           
@Override
           
public void completed(Integer result, AsynchronousSocketChannel channel  ) {
               
buf.flip();

               
// echo the message
               
startWrite( channel, buf );

               
//start to read next message again
               
startRead( channel );

               
// menampilkan string pesan ke textfield txtPesanDiterimaServer
               
txtPesanDiterimaServer.setText(new String( buf.array()));
            }

           
@Override
           
public void failed(Throwable exc, AsynchronousSocketChannel channel ) {
               
System.out.println( "fail to read message from client");
            }
        });
    }

   
private void startWrite( AsynchronousSocketChannel sockChannel, final ByteBuffer buf) {
        sockChannel.write(buf, sockChannel,
new CompletionHandler<Integer, AsynchronousSocketChannel >() {

           
@Override
           
public void completed(Integer result, AsynchronousSocketChannel channel) {
               
//finish to write message to client, nothing to do
           
}

           
@Override
           
public void failed(Throwable exc, AsynchronousSocketChannel channel) {
               
//fail to write message to client
               
System.out.println( "Fail to write message to client");
            }

        });
    }
}

 


Download source code github https://github.com/doditsuprianto/Implementasi-Socket-TCP-dengan-Java-Swing


 

Wednesday, September 23, 2020

Implementasi Socket TCP Asynchrounus Menggunakan Visual Studio - C#.NET

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;
using System.Linq;
using System.Text;
using System.Threading.Tasks; 

namespace DemoSocketClientServer
{
    class varGlobal
    {

        public static string alamatIPServer;       
        public static int port;
        public static string terimapesandariserver;
        public static string terimapesandiserver;
    }
}


SocketTCP.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms; 

namespace DemoSocketClientServer
{
    class StateObject
    {
        public Socket workSocket = null;
        public const int BufferSize = 256;
        public byte[] buffer = new byte[BufferSize];
        public StringBuilder sb = new StringBuilder();       
    }

    class SocketTCP
    {
        public static ManualResetEvent connectDone = new ManualResetEvent(false);
        public static ManualResetEvent sendDone = new ManualResetEvent(false);
        public static ManualResetEvent receiveDone = new ManualResetEvent(false);
        public static String reponse = String.Empty;

        //############################
        //       SOCKET CLIENT
        //############################

        public static void StartClient(string pesan)
        {
            IPAddress ip = IPAddress.Parse(varGlobal.alamatIPServer);
            IPEndPoint remoteEP = new IPEndPoint(ip, varGlobal.port);
            Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            
            var
result = client.BeginConnect(remoteEP, new AsyncCallback(SocketTCP.ConnectCallback), client);

            result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(1));
            Thread.Sleep(10);

            if (client.Connected)
            {
                Send(client, pesan);
                Thread.Sleep(500);
                sendDone.WaitOne();

                client.Shutdown(SocketShutdown.Both);
                client.Close();
            }
        }

        public static void ConnectCallback(IAsyncResult ar)
        {

            try
            {
                Socket client = (Socket)ar.AsyncState;
                client.EndConnect(ar);
                connectDone.Set();
            }

            catch
            {

            }
        }

        public static void Send(Socket client, String data)
        {
            byte[] byteData = Encoding.ASCII.GetBytes(data);

            client.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), client);
        }

        private static void SendCallback(IAsyncResult ar)
        {
            try
            {
                Socket client = (Socket)ar.AsyncState;
                int byteSent = client.EndSend(ar);
                sendDone.Set();
            }

            catch
            {

            }
        }

        public static void Receive(Socket client)
        {
            StateObject state = new StateObject();
            state.workSocket = client;
            client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
        }

        private static void ReceiveCallback(IAsyncResult ar)
        {
            StateObject state = (StateObject)ar.AsyncState;
            Socket client = state.workSocket;

            int bytesRead = client.EndReceive(ar);

            if (bytesRead > 0)
            {
                state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
                client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
            else
            {
                if (state.sb.Length > 1)
                {          
                    reponse = state.sb.ToString();
                    varGlobal.terimapesandariserver = reponse;
                }

                receiveDone.Set();
            }
        }

 

        //##################################

        //          SOCKET SERVER

        //##################################

        public static ManualResetEvent allDone = new ManualResetEvent(false);

        public static void StartListening()

        {

            IPAddress ipAddress = IPAddress.Parse(varGlobal.alamatIPServer);
            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, varGlobal.port);

            Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            try

            {

                listener.Bind(localEndPoint);
                listener.Listen(100); // jumlah client max <= 100 client

                while (true)

                {
                    allDone.Reset();
                    listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
                    allDone.WaitOne();

                }

            }

            catch (Exception e)

            {
                MessageBox.Show(e.ToString());
            }

        }

        public static void AcceptCallback(IAsyncResult ar)

        {

            allDone.Set();
            Socket listener = (Socket)ar.AsyncState;
            Socket handler = listener.EndAccept(ar);
            StateObject state = new StateObject();
            state.workSocket = handler;
            handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);

        }

        public static void ReadCallback(IAsyncResult ar)

        {

            String terimaPesan = String.Empty;

            StateObject state = (StateObject)ar.AsyncState;
            Socket handler = state.workSocket;
            int bytesRead = handler.EndReceive(ar);

            if (bytesRead > 0)

            {
                state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));

                terimaPesan = state.sb.ToString();

                if (terimaPesan != "")

                {

                    varGlobal.terimapesandiserver = terimaPesan;
                    Send(handler, terimaPesan);

                }

            }

        }       

    }

}

 



frmDemoSocketClientServer.cs

using System;
using System.Threading;
using System.Windows.Forms;

namespace DemoSocketClientServer

{

    public partial class frmDemoSocketClientServer : Form

    {

        public frmDemoSocketClientServer()

        {
            InitializeComponent();
        }

        private void btnListen_Click(object sender, EventArgs e)

        {

            Thread thr = new Thread(listenSocket);

            if (txtPortServer.Text != "" && txtIPServer.Text != "")

            {

                thr.Start();
                btnListen.Enabled = false;

            } else

            {

                MessageBox.Show("Lengkapi port dan alamat IP terlebih dahulu !!!");

            }

        }

        private void listenSocket()

        {

            varGlobal.port = Int16.Parse(txtPortServer.Text);
            varGlobal.alamatIPServer = txtIPServer.Text;
            SocketTCP.StartListening();

        }

        private void btnKirimDataKeServer_Click(object sender, EventArgs e)

        {           

            varGlobal.alamatIPServer = txtIPTujuan.Text;
            varGlobal.port = Int16.Parse(txtPortClient.Text);
            SocketTCP.StartClient(txtKirimData.Text);

        }

        private void timer1_Tick(object sender, EventArgs e)

        {

            txtTerimaData.Clear();
            txtTerimaData.Text = varGlobal.terimapesandiserver;

        }

    }

}


Untuk mengetahui IP Local komputer, gunakan perintah "ipconfig" melalui Command Prompt, seperti berikut ini (dalam kasus ini saya menggunakan jaringan WiFi pada jaringan lokal saya, dengan network 192.168.1.0/24):

Note:
Anda dapat menguji Socket TCP dalam satu komputer atau beda komputer namun masih dalam jaringan yang sama, dengan mengirimkan teks dari client ke server. Anda dapat pula mengganti nomor PORT dan alamat IP sesuai kondisi jaringan yang Anda miliki.

Demo Program juga dapat di-download di Github pada link berikut https://github.com/doditsuprianto/Implementasi-Socket-TCP-Menggunakan-Visual-Studio---C-.NET