java单人聊天

2023-12-13 07:42:20

?服务端

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();
?? ?}
}

文章来源:https://blog.csdn.net/qq_73086324/article/details/134953263
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。