Java Tutorial: How To Create Auto Messaging (BOT MESSAGE) With Socket Programming On NetBeans

in #utopian-io6 years ago (edited)

Repository

https://github.com/dmlloyd/openjdk

What Will I Learn?

In brief, write details of what the user is going to learn in a bullet list.

  • You will learn how to create program to auto reply chat (bot message).
  • You will learn how to connect between two program.
  • You will learn how to apply socket programming on bot message.

Requirements

  • you must know basic of java programming
  • you must know about socket programming
  • you must know how to create two program in once package on netbeans and connecting the program

Difficulty

Choose one of the following options:

  • Basic

Tutorial Contents

okay, on this occasion I will make an app BOT MESSAGE, this is an application that can reply to chat automatically, this application is already widely used by large companies such as Apple smartphone is SIRI feature that can answer the questions from users, google also apply this method to the * oke google* feature, as well as some other apps. we will create two programs that work for questions and answers.

  • okay we just go to the tutorial.
  • the first thing you have to do is create a new project on netbeans.

  • then create a new program, select new file then place on the project we have created before.

  • then give name to first program that is question and for second program give name answer.

  • okay, then we start with the code for the program question, type the code below, I'll explain it below later.
package BotMessage;

import java.io.DataInputStream;
import java.io.DataOutputStream;

import java.io.IOException;
import java.net.Socket;
import javax.swing.JOptionPane;

public class question {

public static void main(String[] args)throws IOException {
    int Port =Integer.parseInt(JOptionPane.showInputDialog("Input Your Port : "));
String IP = JOptionPane.showInputDialog("Input Your IP Server : ");
Socket sock=new Socket("localhost", 6066);
DataInputStream in= new DataInputStream(sock.getInputStream());
System.out.println(in.readUTF());
DataOutputStream out =new DataOutputStream(sock.getOutputStream());
out.writeUTF("please tell me location");
sock.close();
}}
  • okay I'll explain a little bit about the above program.
  • package BotMessage; This is the declaration of the package name we created earlier.
  • this is the class we use if we apply input method and output data stream.
import java.io.DataInputStream;
import java.io.DataOutputStream;
  • this is to handle the input and output process import java.io.IOException;
  • import java.net.Socket; this is for declaration of socket programming method.
  • import javax.swing.JOptionPane; this we use because we create the input box with JOptionPane.
  • public class question { this is the declaration of the class name.
  • public static void main(String[] args)throws IOExceptionthis is the main function that must exist in any java programming.
  • int Port =Integer.parseInt(JOptionPane.showInputDialog("Input Your Port : ")); this is the process of creating a new variable that is a port variable, with the fileld input of your port. This port variable serves to create a connection between the two programs.
  • String IP = JOptionPane.showInputDialog("Input Your IP Server : "); this is the process of making a variable with the name of the IP type data string that serves to input IP procces.
  • Socket sock=new Socket("localhost", 6066); this is the process of determining the port and IP, for the port is 6066 and for the IP is localhost .
  • this is the section for organizing questions, and later we will provide answers to these questions in the answer program.
DataOutputStream out =new DataOutputStream(sock.getOutputStream());
out.writeUTF("how are you...?");
  • sock.close(); this works to end all processes.

  • okay for the first program that the question program is completed, then we will create a second program that is the answer program.
  • create new class in package BotMessage and named the program with the answer.
  • type the program below for the answer program.
package BotMessage;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JOptionPane;

public class answer {
    public static void main(String[] args) throws IOException{
        int Port =Integer.parseInt(JOptionPane.showInputDialog("Input Your Port : "));
String IP = JOptionPane.showInputDialog("Input Your IP Server : ");
       ServerSocket serverSock=new ServerSocket(6066);
       Socket Sock=serverSock .accept();
       DataOutputStream out =new DataOutputStream(Sock.getOutputStream());
       out.writeUTF("i am fine, thank you");
       DataInputStream in= new DataInputStream(Sock.getInputStream());
        System.out.println(in.readUTF());
       Sock.close();
       
    }
}
  • okay, in the above program we have set the answer for the question of the above program, this is a method called data minning method, which where we have arranged everything that will happen at the time of program executed, but in this program I only enter one questions and one answer for experiment only.
  • okay, do not forget to set the same port with the first program so that both programs can be connected.
  • if both programs are done we will try to run the program.
  • right click on the program, then select run file.

  • then enter the port and IP.

.


  • if you have input port and IP correctly then will emerge question and answer which we have set.
    question:

answer:


  • Okay application BOT MESSAGE we have successfully created, this method is very widely used in current technology, you can try it at home, and develop again, if there is less understood you can ask the comment field.

THANK YOU

Curriculum

Include a list of related tutorials you have already shared on Utopian that make up a Course Curriculum, if applicable.

Proof of Work Done

https://gist.github.com/andilapulga/08c85909721f70c6e73b21780d9daf61

Sort:  

Thank you for your contribution.
While I liked the content of your contribution, I would still like to extend few advices for your upcoming contributions:

  • Put some comments on your code, plus the explanation you've already made.
  • The difficulty level of this tutorial is basic.

Please edit your tutorial and remove this:"Choose one of the following options:"

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you sir @portugalcoin ,i already fix it

Hey @lapulga
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.17
TRX 0.15
JST 0.028
BTC 62345.27
ETH 2427.57
USDT 1.00
SBD 2.49