当前位置: 首页 > news >正文

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

服务端的窗口

 

客户端窗口

 

 

http://www.lryc.cn/news/254654.html

相关文章:

  • nodejs环境安装
  • R语言进行正态分布检验
  • 什么是SPA(Single Page Application)?它的优点和缺点是什么?
  • 由于找不到xinput1_3.dll,无法继续执行代码的多种解决方法指南,xinput1_3.dll文件修复
  • Vue---Echarts
  • uni-app实现返回刷新上一页
  • centos服务器安装docker和Rabbitmq
  • 【Redis】Redis高级特性和应用(慢查询、Pipeline、事务、Lua)
  • 【pytorch】深度学习入门一:pytorch的安装与配置(Windows版)
  • 安装postgresql驱动及python使用pyodbc指定postgresql驱动调用postgresql
  • 【OpenCV】计算机视觉图像处理基础知识
  • Course1-Week3-分类问题
  • Dockerfile 指令的最佳实践
  • Drools 入门:折扣案例
  • 微信小程序中生命周期钩子函数
  • “无忧文件安全!上海迅软DSE文件加密软件助您轻松管控分公司数据!
  • 详解线段树
  • C语言——指针的运算
  • Apache Hive(部署+SQL+FineBI构建展示)
  • python入门级简易教程
  • 模拟一个集合 里面是设备号和每日的日期
  • antdesign前端一直加载不出来
  • 排序算法介绍(一)插入排序
  • 2023新优化应用:RIME-CNN-LSTM-Attention超前24步多变量回归预测算法
  • RNN:文本生成
  • Rust UI开发(五):iced中如何进行页面布局(pick_list的使用)?(串口调试助手)
  • Linux学习笔记2
  • 数据结构算法-插入排序算法
  • 安装Kuboard管理K8S集群
  • 网络安全行业大模型调研总结