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

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.kler.cn/a/159979.html

相关文章:

  • vue请求数据报错,设置支持跨域请求,以及2种请求方法axios或者async与await
  • 微服务(二)
  • 树形dp总结
  • 微服务架构面试内容整理-API 网关-Gateway
  • 在 Service Worker 中caches.put() 和 caches.add()/caches.addAll() 方法他们之间的区别
  • libcurl.net入门使用
  • 模式识别与机器学习(七):集成学习
  • Python高级数据结构——并查集(Disjoint Set)
  • Multidimensional Scaling(MDS多维缩放)算法及其应用
  • docker安装mysql8
  • Python 模块的使用方法
  • 万宾科技监测设备,可燃气体监测仪特点一览
  • PostgreSQL有意思的现象:支持不带列的表
  • Java 数据结构篇-用链表、数组实现队列(数组实现:循环队列)
  • 【动手学深度学习】(六)权重衰退
  • 【Unity入门】声音组件AudioSource简介及实现声音的近大远小
  • 生成对抗网络——研讨会
  • SQL练习
  • QT-在ui界面中给QWidget增加Layout布局的两种方法
  • Tensorflow.js 入门学习指南
  • java内部类详解
  • matlab实践(十):贝塞尔曲线
  • Linux搭建FTP并安装xrdp,实现Windows系统下利用FileZilla传输文件和远程桌面连接
  • Seo优化是什么,怎么进行seo优化
  • 服务器数据恢复—服务器重装系统导致逻辑卷发生改变的数据恢复案例
  • uni-app详解、开发步骤、案例代码