多线程问题

 
package com.chat.demo;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;

public class Server {
 
 static final String SOCKT_IP = "127.0.0.1";
 static final int SOCKT_PORT = 8888;
 static int clientId = 0;
 
 public static void main(String[] args) {
  //用来存储连接客户端的socket
  ArrayList<Socket> sockets = new ArrayList<Socket>();

  try {
   ServerSocket ss = new ServerSocket(8888);
   while (true) {
    Socket s = ss.accept();
    sockets.add(s);
    new ReadThread(s, sockets).start();
    System.out.println("客户端的个数:" + sockets.size());

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

}

class ReadThread extends Thread {

 private ArrayList<Socket> sockets;
 private Socket socket;
 boolean flag = true;

 public ReadThread(Socket socket, ArrayList<Socket> sockets) {
  this.socket = socket;
  this.sockets = sockets;
 }

 public void run() {
  try {
   DataInputStream dis = new DataInputStream(
     socket.getInputStream());
   boolean isExit = false;
   while (flag) {
    String msg = dis.readUTF();
    
    if(msg.contains("!q")){
     msg = msg.replace("!q", "离开聊天室");
     isExit = true;
    }
    System.out.println(msg);
    if (msg != null && !msg.equals("")) {
     for (Iterator iterator = sockets.iterator(); iterator
       .hasNext();) {
      Socket s = (Socket) iterator.next();
      DataOutputStream dos = null;
      if(s != socket){
       dos = new DataOutputStream(s.getOutputStream());
       dos.writeUTF(msg);
       dos.flush();
      }
     }
    }
    if(isExit){
     isExit = false;
     flag = false;
     sockets.remove(socket);
     socket.close();
     
    }
   }
  } catch (IOException e) {
  }
 }

}

 

************************************************************************************************************

 

package com.chat.demo;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Client {
 
 private int id;              //编号
 private String name;         //客户端名称
 private Date loginTime;       //发送时间
 private Socket sokcet;       //与服务器连接的socket
 private ClientRead cr;
 private ClientWrite cw;
 
 
 public Client(String name){
  this.name = name;
  this.id = Server.clientId++;
  this.loginTime = new Date();
  try {
   this.sokcet = new Socket(Server.SOCKT_IP,Server.SOCKT_PORT);
  } catch (UnknownHostException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  cr = new ClientRead(sokcet,this);
  cw = new ClientWrite(sokcet,name,this);
 }
 
 public void start(){
  cr.start();
  cw.start();
 }
 
 public void close(){
  System.exit(0);
 }
 
 
 
 
 public static void main(String[] args) {
//  new Client("张三").start();
//  new Client("李四").start();
  new Client("王五").start();
 }

}


class ClientRead extends Thread{
 
 private Socket s;
 private Client c;
 
 public ClientRead(Socket s , Client c){
  this.s = s;
  this.c = c;
 }
 
 public void run(){
  try {
   while(true){
    DataInputStream dis = new DataInputStream(
      s.getInputStream());
    String msg = dis.readUTF();
    System.out.println(msg);
    
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 
}

class ClientWrite extends Thread{
 
 private Socket s;
 private String name;
 boolean isLogin;  //登录状态 默认为false   
 private Client c;
 public ClientWrite(Socket s,String name,Client c){
  this.s = s;
  this.name = name;
  this.isLogin = false;
  this.c = c;
 }
 
 public void run(){
  Scanner scan = new Scanner(System.in);
  try {
  DataOutputStream dis = new DataOutputStream(
    s.getOutputStream());
   while(true){
    SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
    String sendTime = format.format(new Date());
    String msg = null;
    if(isLogin){
     msg = name+"说:"+scan.next()+"\t"+sendTime;
    }else{
     msg = "欢迎"+name+"\t"+sendTime; 
     isLogin = true;
    }
    
    dis.writeUTF(msg);
    dis.flush();
    if(msg.contains("!q")){
     c.close();
    }
   }
  } catch (IOException e) {
  }
 }
 
}


 


 

阅读更多
个人分类: java
上一篇java中的反射问题
下一篇数据报的服务器客户机连接
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭