In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces how to use network communication technology in java to achieve chat Mini Program related knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that you will have something to gain after reading this article on how to use network communication technology to achieve chat Mini Program in java, let's take a look.
The first is the server code:
Package ChatTwoPackage; import java.io.*;import java.net.*; public class ChatTwoServer {public ChatTwoServer (int port,String name) throws IOException {ServerSocket server=new ServerSocket (port); / / create a seversocket object that provides tcp connection services. Specify the port port and wait for the tcp connection. System.out.print ("waiting for connection, do not operate!") ; Socket client=server.accept (); / / create a socket object that waits for a connection from the receiving client. New ChatTwoClient (name,client); / / implement the graphical interface. Server.close ();} public static void main (String [] args) throws IOException {new ChatTwoServer (2001, "SQ");}}
Then there is the client-side code:
Package ChatTwoPackage; import java.awt.event.*;import javax.swing.*;import java.net.*;import java.io.*; public class ChatTwoClient extends JFrame implements ActionListener {private String name; private JTextArea text_re; private JTextField text_se; private PrintWriter cout; private JButton buttons []; public ChatTwoClient (String name,Socket socket) throws IOException {super ("I:" + name+InetAddress.getLocalHost (). GetHostAddress () + ":" + socket.getLocalPort ()); this.setBounds (320240400240); this.text_re=new JTextArea () This.text_re.setEditable (false); this.getContentPane (). Add (new JScrollPane (this.text_re)); JToolBar toolBar=new JToolBar (); this.getContentPane (). Add (toolBar, "South"); toolBar.add (this.text_se=new JTextField (30)); buttons=new JButton [2]; buttons [0] = new JButton ("send"); buttons [1] = new JButton ("offline"); toolBar.add (buttons [0]); toolBar.add (buttons [1]); buttons [0] .addActionListener (this) Buttons [1] .addActionListener (this); / / add event listener to the button to delegate the current object to handle this.setVisible (true); this.name=name; this.cout=new PrintWriter (socket.getOutputStream (), true); / / get socket output stream this.cout.println (name); BufferedReader br=new BufferedReader (new InputStreamReader (socket.getInputStream () / / convert the byte input stream of socket to character stream, default GBK character set, and then create buffered character input stream String line= "connection" + br.readLine () + "success"; while (linewise input nullary input stream) {text_re.append (line+ "\ r\ n"); line=br.readLine () } / / read the content sent by the other party and display it until the content is empty or offline br.close (); this.cout.close (); socket.close (); buttons [0] .setEnabled (false); buttons [1] .setEnabled (false);} public ChatTwoClient (String name,String host,int port) throws IOException {this (name,new Socket (host,port)) / / call another constructor} public void actionPerformed (ActionEvent ev) {if (ev.getActionCommand (). Equals ("send")) {this.cout.println (name+ ":" + text_se.getText ()); text_re.append ("I:" + text_se.getText () + "\ n"); text_se.setText (") } / / after the send button is pressed, send the content and update the contents of your chat box if (ev.getActionCommand (). Equals ("offline")) {text_re.append ("you are offline\ n"); this.cout.println (name+ "offline\ n" + "bye\ n"); buttons [0] .setEnabled (false); buttons [1] .setEnabled (false) } / / when the offline button is pressed, send bye as the offline flag} public static void main (String [] args) throws IOException {new ChatTwoClient ("mxl", "127.0.0.1", 2001); / / ip address and port}}
Running effect:
Description:
1. One of the two computers is used as a server, and the computer as a server needs two codes. First, run the server code and wait for the client machine to connect. After the client runs the client code, it prompts that the connection is successful. You can send a message.
two。 Before running the code, you need to change the ip address to the current ip address of your computer (computers that surf the Internet by means of Modem, ISDN, ADSL, cable broadband, residential broadband, etc., each time the IP address assigned to the Internet is different, which is called dynamic IP address). If you want to use a computer as the client and server, write the ip address as: 127.0.0.1 (127.0.0.1 is the loopback address, the local machine, which is generally used for testing). Run the server code first, and then run the client code.
This is the end of the article on "how to use network communication technology to achieve chat Mini Program in java". Thank you for reading! I believe that everyone has a certain understanding of "how to use network communication technology to achieve chat Mini Program in java". If you want to learn more knowledge, you are welcome to 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.