简单的多线程

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();
  }
 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值