import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
import java.util.ArrayList;
public class MyServer {
private ServerSocket ss ; //服务器
private Socket socket; //socket通信
private BufferedReader in; //输入
private PrintWriter out; //输出
private ArrayList<Socket> socketList = new ArrayList<Socket>();
// MyServer(){
// try{
// ss = new ServerSocket(9999); //创建服务器监听9999端口
// while(true){ //创建死循环
// System.out.println("等待通信");
// socket = ss.accept(); //创建通信
// System.out.println("有信息进入");
// in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// String sss=in.readLine();
// System.out.println("进入信息为:"+sss);
// out = new PrintWriter(socket.getOutputStream(),true);
//
out.print("返回信息");
// out.flush();
// System.out.println("返回信息成功");
//
// //关闭通信及输入输出流
// out.close();
in.close();
// socket.close();
// }
ss.close();
// }catch(Exception e){
// e.printStackTrace();
// }
// }
public MyServer(){
try{
ss = new ServerSocket(9999);
while(true){ //创建死循环
System.out.println("等待通信...");
socket = ss.accept(); //创建通信
this.socketList.add(this.socket);
new CreateThread(socket,this.socketList);
}
}catch(Exception ex){
ex.printStackTrace();
}
}
public static void main(String [] args){
new MyServer();
}
}
class CreateThread extends Thread{
private Socket socket;
private BufferedReader in;
private PrintWriter out;
private ArrayList<Socket> socketList;
public CreateThread(Socket socket,ArrayList<Socket> socketList){
try{
this.socket = socket;
this.socketList = socketList;
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(this.socket.getOutputStream(),true);
this.start();
}catch(Exception e){
e.printStackTrace();
}
}
@Override
public void run() {
try{
String IPPort=socket.getInetAddress().toString()+":"+socket.getPort();
System.out.println(this.getName()+":"+IPPort+"连接成功!");
System.out.println("当前IP:"+this.socket.getInetAddress().toString()+",现在有"+this.socketList.size()+"线程");
while(true){
String line = in.readLine();
if(line.equals("bye")){
System.out.println(this.getName()+"退出了。");
this.socketList.remove(this.socket);
System.out.println("当前IP:"+this.socket.getInetAddress().toString()+"线程名:"+this.getName()+"退出了,现在有"+this.socketList.size()+"线程");
return ;
}else{
System.out.println(this.getName()+":"+line);
}
}
}catch(Exception e){
// e.printStackTrace();
System.out.println(this.getName()+"退出了。");
this.socketList.remove(this.socket);
System.out.println("当前IP:"+this.socket.getInetAddress().toString()+"线程名:"+this.getName()+"退出了,现在有"+this.socketList.size()+"线程");
}
}
}
public class MyClient2 extends JFrame {
private Socket client;
public MyClient2(){
try {
client = new Socket("192.168.1.203",9999);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
this.setTitle("多线程聊天室");
this.setBounds(100, 100, 400, 400);
this.setLayout(new GridLayout(2,1));
JButton jb1 = new JButton("发送");
jb1.setSize(150, 60);
JTextArea jta1 = new JTextArea();
// jta1.setSize(100, 100);
jta1.setRows(20);
jta1.setColumns(150);
this.add(jta1);
this.add(jb1);
this.show();
this.addWindowListener(new ExitLintener(client));
jb1.addActionListener(new SaySo(jta1,client));
}
public static void main(String [] args){
new MyClient2();
}
}
class SaySo implements ActionListener{
private JTextArea say ;
private PrintWriter out;
private Socket client;
public SaySo(JTextArea say,Socket client){
this.say = say;
this.client = client;
}
public void actionPerformed(ActionEvent e) {
try{
out = new PrintWriter(client.getOutputStream());
out.println(say.getText());
out.flush();
if(say.equals("bye")){
System.exit(0);
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
class ExitLintener extends WindowAdapter{
private Socket client;
private PrintWriter out;
ExitLintener(Socket client){
this.client = client;
}
public void windowClosing(WindowEvent e) {
System.exit(0);
}
public void windowClosed(WindowEvent arg0) {
try{
out = new PrintWriter(client.getOutputStream());
out.println("bye");
out.flush();
out.close();
client.close();
}catch(Exception ex){
ex.printStackTrace();
}
}