java基于socket的简单聊天系统

/*=============服务端================*/

/**
* 服务器程序 在9999端口监听
* 可以通过控制台输入来回应客户端
* @author xiaoluo
* @qq 3087438119
*/

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import javax.swing.*;

public class MyServer1 extends JFrame implements ActionListener{
/**
*
*/
private static final long serialVersionUID = 1L;
JTextArea jta = null;
JTextField jtf = null;
JButton jb = null;
JPanel jpl= null;
JScrollPane jsp = null;
//把信息发给客户端的对象
PrintWriter pw =null;
public static void main(String [] args){
MyServer1 ms = new MyServer1();
}
public MyServer1(){
jta = new JTextArea();
jtf = new JTextField(20);
jb= new JButton("发送");
jb.addActionListener(this);
jpl = new JPanel();
jsp = new JScrollPane(jta);
jpl.add(jtf);
//jpl.add(jta);
jpl.add(jb);
this.add(jsp,"Center");
this.add(jpl,"South");
this.setTitle("和佳客服");
this.setSize(400,300);
this.setVisible(true);
//服务器监听
try {
ServerSocket ss= new ServerSocket(9988);
//等待客户端连接
Socket s = ss.accept();
//读取客户端发来的信息
InputStreamReader isr = new InputStreamReader(s.getInputStream());
BufferedReader brd = new BufferedReader(isr);
pw = new PrintWriter(s.getOutputStream(),true);
while(true){
//读取客户端信息
String info = brd.readLine();
//把客户端信息写到信息栏
jta.append("客户端:"+info+" ");
}
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void actionPerformed(ActionEvent e) {
//如果用户按下发送信息按钮
if(e.getSource()==jb){
//把服务器在框里写内容发送给客户端
String info = jtf.getText();
jta.append("服务端:"+info+" ");
pw.println(info);//发送
jtf.setText("");//清空输入框
}
}

}

/*===============客户端====================*/


/**
* 客户端
* @author xiaoluo
* @qq 3087438119
*/

import java.io.*;
import java.net.*;
import javax.swing.*;

public class MyClient1 extends JFrame implements ActionListener{

JTextArea jta = null;
JTextField jtf = null;
JButton jb = null;
JPanel jpl= null;
JScrollPane jsp = null;
//把信息发给客户端的对象
PrintWriter pw =null;


public static void main(String [] args){
MyClient1 mc = new MyClient1();
}

public MyClient1(){
jta = new JTextArea();
jtf = new JTextField(20);
jb= new JButton("发送");
jb.addActionListener(this);
jpl = new JPanel();
jsp = new JScrollPane(jta);
jpl.add(jtf);
//jpl.add(jta);
jpl.add(jb);
this.add(jsp,"Center");
this.add(jpl,"South");
this.setTitle("客户端");
this.setSize(400,300);
this.setVisible(true);

try {
Socket s = new Socket("127.0.0.1",9988);
InputStreamReader isr = new InputStreamReader(s.getInputStream());
BufferedReader br = new BufferedReader(isr);

pw = new PrintWriter(s.getOutputStream(),true);

while(true){
//不停地读取从服务器端发来的信息
String info = br.readLine();
jta.append("服务端:"+info+" ");



}

} catch (UnknownHostException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}


}

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//如果用户按下发送信息按钮
if(e.getSource()==jb){
//把服务器在框里写内容发送给客户端
String info = jtf.getText();
jta.append("客户端:"+info+" ");
pw.println(info);//发送
jtf.setText("");//清空输入框
}
}
}

原文地址:https://www.cnblogs.com/herd/p/5981504.html