展开全部
先运行服务端 再运行客户端 就能e5a48de588b662616964757a686964616f31333365653266对话了
服务端
package Liaotian;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class fwjiemian extends Frame implements ActionListener{
JTextArea lt;
TextField sr;
Button fs;
Panel xia;
ServerSocket ss;
Socket s;
BufferedReader br;
PrintWriter pw;
BufferedReader in;
public fwjiemian(){
lt=new JTextArea();
lt.setLineWrap(true);
sr=new TextField();
fs=new Button("发送");
xia=new Panel(new GridLayout(1,2));
xia.add(sr);
xia.add(fs);
this.add(lt);
this.add(xia,BorderLayout.SOUTH);
this.setResizable(false);
this.setSize(400, 500);
this.setLocation(350, 100);
this.setTitle("服务器");
this.setVisible(true);
fs.addActionListener(this);
//创建客户端+接受消息
try {
lt.setFont(new java.awt.Font("宋体",0,15));
ss=new ServerSocket(8888);
lt.append("服务器已启动,等待客户端接入..."+"\n"+"\n");
s=ss.accept();
lt.append("客户端已接入..."+"\n"+"\n");
while(true){
//获取时间
SimpleDateFormat sm=new SimpleDateFormat("HH:mm:ss");
System.out.println(sm.format(new Date()));
//接受消息
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
String info=br.readLine();
lt.append("☆ 客户端:"+info+" "+sm.format(new Date())+"\n"+"\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new fwjiemian();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==fs){
try {
//获取时间
SimpleDateFormat sm=new SimpleDateFormat("HH:mm:ss");
System.out.println(sm.format(new Date()));
//发送消息
String infotokehu=sr.getText();
pw=new PrintWriter(s.getOutputStream(),true);
pw.println(infotokehu);
lt.setFont(new java.awt.Font("宋体",0,15));
lt.append("★ 服务器:"+infotokehu+" "+sm.format(new Date())+"\n"+"\n");
sr.setText("");
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}客户端
package Liaotian;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class khjiemian extends Frame implements ActionListener{
JTextArea lt;
TextField sr;
Button fs;
Panel xia;
Socket s;
PrintWriter pw;
BufferedReader br;
BufferedReader in;
public khjiemian(){
lt=new JTextArea();
lt.setLineWrap(true);
sr=new TextField();
fs=new Button("发送");
xia=new Panel(new GridLayout(1,2));
xia.add(sr);
xia.add(fs);
this.add(lt);
this.add(xia,BorderLayout.SOUTH);
this.setResizable(false);
this.setSize(400, 500);
this.setLocation(800, 100);
this.setTitle("服务器");
this.setVisible(true);
fs.addActionListener(this);
//创建客户端+接受消息
try {
lt.setFont(new java.awt.Font("宋体",0,15));
s=new Socket("127.0.0.1",8888);
lt.append("客户端已启动,等待接入服务器..."+"\n"+"\n");
lt.append("已接入服务器..."+"\n"+"\n");
while(true){
//获取时间
SimpleDateFormat sm=new SimpleDateFormat("HH:mm:ss");
System.out.println(sm.format(new Date()));
//接受消息
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
String info=br.readLine();
lt.append("★ 服务器:"+info+" "+sm.format(new Date())+"\n"+"\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new khjiemian();
}
public void actionPerformed(ActionEvent w) {
if(w.getSource()==fs){
try {
//获取时间
SimpleDateFormat sm=new SimpleDateFormat("HH:mm:ss");
System.out.println(sm.format(new Date()));
//发消息
String infotofuwu=sr.getText();
pw=new PrintWriter(s.getOutputStream(),true);
pw.println(infotofuwu);
lt.setFont(new java.awt.Font("宋体",0,15));
lt.append("☆ 客户端:"+infotofuwu+" "+sm.format(new Date())+"\n"+"\n");
sr.setText("");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}