我们首先要完成服务端,不然出错,运行也要先运行服务端,如果不先连接服务端,就不监听,那客户端不知道连接谁
服务端
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;public class fwq extends JFrame implements ActionListener,KeyListener {//1.窗体组件//文本域private JTextArea wby;//滚动条private JScrollPane gdt;//输入面板private JPanel mb;//文本框private JTextField wbk;//按钮private JButton an ;//输出流private BufferedWriter BW=null;public FWD() {//将组件初始化wby=new JTextArea();//设置文本域不可编辑wby.setEditable(false);//要将文本域放到滚动条中,实现滚动效果gdt=new JScrollPane(wby);//面板mb=new JPanel();wbk=new JTextField(10);an=new JButton("发送");//文本框和按钮要放到面板中mb.add(wbk);mb.add(an);//将滚动条和面板放到窗体中this.add(gdt,BorderLayout.CENTER);//将文本内容也就是滚动条放到窗体的中间this.add(mb,BorderLayout.SOUTH);//将输入面板放到窗体的底(南)部//窗体属性setTitle("服务端");//标题setSize(400,400);//窗体大小setLocation(250,200);//位置setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭方式setVisible(true);//是否显示//发送事件an.addActionListener(this);wbk.addKeyListener(this);try {//创建套接字ServerSocket s=new ServerSocket(8998);Socket sk = s.accept();//获取输入流(读取文本框的数据)//使用BufferedReader的readLine()方法,为了一行一行读取BufferedReader r=new BufferedReader(new InputStreamReader(sk.getInputStream()));//获取输出流(输出数据)也是一行一行的//BufferedWriter的newLine()方法BW=new BufferedWriter(new OutputStreamWriter(sk.getOutputStream()));//循环获取数据,并拼接到文本域中String line=null;while((line=r.readLine())!=null) {//将读取的数据拼接到文本域中wby.append(line+"\n");}//关闭s.close();sk.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void sx() {//获取文本框的数据String text=wbk.getText();//拼接要发送的内容text="服务端:"+text;//服务端面板显示wby.append(text+"\n");try {//发送BW.write(text);//换行BW.newLine();//刷新BW.flush();//让文本框为空wbk.setText("");}catch(IOException e1) {e1.printStackTrace();}}public static void main(String[] args) {new FWD();}@Override//按钮事件public void actionPerformed(ActionEvent e) {sx();}//回车事件
@Overridepublic void keyPressed(KeyEvent e) {if(e.getKeyCode()==KeyEvent.VK_ENTER) {sx();}}@Overridepublic void keyTyped(KeyEvent e) {// TODO Auto-generated method stub}@Overridepublic void keyReleased(KeyEvent e) {// TODO Auto-generated method stub}}
客户端
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;public class khd extends JFrame implements ActionListener,KeyListener{//1.窗体组件//文本域private JTextArea wby;//滚动条private JScrollPane gdt;//输入面板private JPanel mb;//文本框private JTextField wbk;//按钮private JButton an ;private BufferedWriter BW=null;public KHD() {//将组件初始化wby=new JTextArea();//设置文本域不可编辑wby.setEditable(false);//要将文本域放到滚动条中,实现滚动效果gdt=new JScrollPane(wby);//面板mb=new JPanel();wbk=new JTextField(10);an=new JButton("按钮");//文本框和按钮要放到面板中mb.add(wbk);mb.add(an);//将滚动条和面板放到窗体中this.add(gdt,BorderLayout.CENTER);//将文本内容也就是滚动条放到窗体的中间this.add(mb,BorderLayout.SOUTH);//将输入面板放到窗体的底(南)部//窗体属性setTitle("客户端");//标题setSize(400,400);//窗体大小setLocation(800,200);//位置setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭方式setVisible(true);//是否显示//绑定一个发送的监听事件an.addActionListener(this);wbk.addKeyListener(this);try {//创建客户端的套接字(尝试连接)Socket sk=new Socket("127.0.0.1",8998);//获取Socket的输入流BufferedReader r=new BufferedReader(new InputStreamReader(sk.getInputStream()));//获取Socket的输出流BW=new BufferedWriter(new OutputStreamWriter(sk.getOutputStream()));//循环获取数据,并拼接到文本域中String line=null;while((line=r.readLine())!=null) {//循环读取wby.append(line+"\n");}//关闭Socket通道sk.close();}catch(IOException e) {e.printStackTrace();};}public void sx() {//获取文本框的数据String text=wbk.getText();//拼接要发送的内容text="客户端:"+text;//服务端面板显示wby.append(text+"\n");try {//发送BW.write(text);//换行BW.newLine();//刷新BW.flush();//让文本框为空wbk.setText("");}catch(IOException e1) {e1.printStackTrace();}}public static void main(String[]args) {new KHD();}@Overridepublic void actionPerformed(ActionEvent e) {sx();}@Overridepublic void keyPressed(KeyEvent e) {if(e.getKeyCode()==KeyEvent.VK_ENTER) {sx();}}@Overridepublic void keyTyped(KeyEvent e) {// TODO Auto-generated method stub}@Overridepublic void keyReleased(KeyEvent e) {// TODO Auto-generated method stub}}
运行结果: