本文主要是介绍java单人聊天,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
服务端
package 单人聊天;import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;public class AServer extends JFrame implements ActionListener,Runnable {private int Port = 9999;private ServerSocket SS;private Socket socket;private JTextArea area = new JTextArea("聊天内容:"+'\n');private JTextField field = new JTextField("");public AServer(){this.setTitle("服务器");this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.add(area,BorderLayout.CENTER);this.add(field,BorderLayout.NORTH);field.addActionListener(this);this.setSize(190, 200);this.setVisible(true);try{ SS = new ServerSocket(Port);socket = SS.accept();new Thread(this).start();}catch(Exception ex){ } }public void run(){try{while(true){InputStream is = socket.getInputStream();BufferedReader br = new BufferedReader(new InputStreamReader(is));String str = br.readLine(); area.append(str + '\n'); } }catch(Exception ex){ }}public void actionPerformed(ActionEvent e){try{OutputStream os = socket.getOutputStream();PrintStream ps = new PrintStream(os);ps.println("服务器说:" + field.getText());field.setText("");}catch(Exception ex){ }} public static void main(String[] args){new AServer();}
}
客户端
package 单人聊天;import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;public class Aclient extends JFrame implements ActionListener, Runnable {private Socket socket1;private int Port1 = 9999;private InetAddress ip1;private JTextArea area1 = new JTextArea("聊天内容:\n");private JTextField field1 = new JTextField("");public Aclient(){this.setTitle("客户端");this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.add(field1,BorderLayout.NORTH);field1.addActionListener(this);this.add(area1, BorderLayout.CENTER); this.setSize(190, 200);this.setVisible(true);try{ip1 = InetAddress.getByName("Localhost");socket1 = new Socket(ip1,Port1);OutputStream os = socket1.getOutputStream();PrintStream ps = new PrintStream(os);ps.println("客户端连接");new Thread(this).start(); }catch (Exception ex){ }}public void run1(){try{while(true){InputStream is = socket1.getInputStream();BufferedReader bf = new BufferedReader(new InputStreamReader(is));String str = bf.readLine();area1.append(str + '\n');}}catch (Exception ex){ }}public void actionPerformed1(ActionEvent e){try{OutputStream os = socket1.getOutputStream();PrintStream ps = new PrintStream(os);ps.println("客户端说:" + field1.getText());field1.setText("");}catch (Exception ex){}}public static void main1(String[] args) {new Aclient();}private Socket socket;private int Port = 9999;private InetAddress ip;private JTextArea area = new JTextArea("聊天内容:\n");private JTextField field = new JTextField("");public void Aclient1(){this.setTitle("客户端");this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.add(field1,BorderLayout.NORTH);field1.addActionListener(this);this.add(area1, BorderLayout.CENTER); this.setSize(190, 200);this.setVisible(true);try{ip1 = InetAddress.getByName("Localhost");socket1 = new Socket(ip1,Port1);OutputStream os = socket1.getOutputStream();PrintStream ps = new PrintStream(os);ps.println("客户端连接");new Thread(this).start(); }catch (Exception ex){ }}public void run(){try{while(true){InputStream is = socket1.getInputStream();BufferedReader bf = new BufferedReader(new InputStreamReader(is));String str = bf.readLine();area1.append(str + '\n');}}catch (Exception ex){ }}public void actionPerformed(ActionEvent e){try{OutputStream os = socket1.getOutputStream();PrintStream ps = new PrintStream(os);ps.println("客户端说:" + field1.getText());field1.setText("");}catch (Exception ex){}}public static void main(String[] args) {new Aclient();}
}
服务端的窗口
客户端窗口
这篇关于java单人聊天的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!