Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the code of chatbot realized by Java

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

In this article Xiaobian for you to introduce in detail "how to write the code for Java chat robot", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to write the code for Java chat robot" can help you solve your doubts.

Code for the server:

Package Day02;import java.io.*;import java.net.*; public class Socket1 {public static void main (String [] args) throws Exception {int port=8899; ServerSocket server=null; MChat MC=new MChat (); try {server=new ServerSocket (port); System.out.println ("Server starts..."); while (true) {Socket socket=server.accept () / / listens to the client connection and returns the instantiated Socket object System.out.println ("client information:" + socket) if the client connection is successful; / / read stream operation SocketThd thd=new SocketThd (socket); thd.start ();}} catch (Exception e) {/ / TODO: handle exception} finally {try {if (serverflows null) server.close () System.out.println ("the server has stopped! ");} catch (Exception e2) {/ / TODO: handle exception} static class SocketThd extends Thread {MChat MC=new MChat (); BufferedReader br=null; PrintWriter pw=null; public SocketThd (Socket socket) {this.socket=socket;} public void run () {/ / read stream operation try {br=new BufferedReader (new InputStreamReader (socket.getInputStream ()," UTF-8 ")) / / write stream operation pw=new PrintWriter (new BufferedWriter (new OutputStreamWriter (socket.getOutputStream (), "UTF-8")); / / receive the message String str= "" from the client; while ((str=br.readLine ())! = null) {System.out.println ("client:" + str); / / the server processes String rtn= "" / / chat robot processor rtn=MC.getCharResult (str); / / the client side pw.println (rtn); pw.flush ();}} catch (Exception e) {e.printStackTrace ();} finally {/ / TODO: handle finally clause try {if (brushed robot null) br.close (); if (pwelled empty null) pw.close () If (socketkeeper null) socket.close ();} catch (Exception e2) {/ / TODO: handle exception}

Code for the client:

Package Day02; import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.net.Socket; public class Client1 {public static void main (String [] args) throws Exception {String IP= "192.168.1.*"; int port=8899; Socket socket=new Socket (IP,port); / / establish a connection to the server / / read stream operation BufferedReader br=new BufferedReader (new InputStreamReader (socket.getInputStream (), "UTF-8")) BufferedReader brKey=new BufferedReader (new InputStreamReader (System.in, "UTF-8")); / / write stream operation PrintWriter pw=new PrintWriter (new BufferedWriter (new OutputStreamWriter (socket.getOutputStream (), "UTF-8")); while (true) {String send=brKey.readLine (); / / waiting for blocking command if (send.equals ("END")) {/ / accommodation condition break for keyboard input } / / send a message to the server pw.println (send); pw.flush (); / / receive a reply from the server String strRtn=br.readLine (); System.out.println ("server reply:" + strRtn);} / / close the connection br.close (); pw.close (); socket.close ();}

Conversation content engine:

Package Day02; import java.util.Hashtable; public class MChat {private Hashtable htMS=new Hashtable (); public String getCharResult (String sKey) {String str=htMS.get (sKey); if (str==null) return "; else return str;} public MChat () {htMS.put (" Hello "," how can I help you? ") ; htMS.put ("Nice to meet you", "Nice to meet you, too.") ; htMS.put ("have you eaten", "I'm so full today"); htMS.put ("tired", "relax by listening to music"); htMS.put ("traffic jams", "need to choose a route for you"); htMS.put ("good-bye", "I'll go first, call me if you need anything");}}

The implementation results are as follows:

After reading this, the article "how to write the code for Java chat robot" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself. If you want to know more about the article, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report