TCP协议下简单聊天室代码

1 客户端写出和读入分别开启两个线程,分开运行,来模拟实现现实生活人聊天时候,不停写,不停读的效果

2 服务端一直开启,并不断接受各个客户端的连接,每接收到一个连接,则服务端开启一个线程,来响应

   这个客户端a不停发来的数据,并将a发送的数据转发到其余接入的客户端b,c,d等通道内

3 案例综合用到:

       tcp通讯

       集合使用

       流读写

       线程

       面向对象模拟

4 实现步骤可以按照:

     a) 先客户端发送 服务端接受后在回复实现

     b) 发送数据从控制台接受 实现

    c) 客户端输入 输出分线程实现

    d)  服务端多线程实现

    

   

5 代码:

 服务端:

/**
 * 创建服务器
 * 写出数据:输出流
 * 读取数据:输入流
 * @author Administrator
 *
 */
public class Server {
	private List<MyChannel> all = new ArrayList<MyChannel>();
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		new Server().start();	
		
	}
	
	public void start() throws IOException{
		ServerSocket server =new ServerSocket(9999);
		while(true){ // 不停接受别的socket管道接入
			Socket client =server.accept();		
			MyChannel channel = new MyChannel(client);
			all.add(channel);//统一管理
			new Thread(channel).start(); //一条道路    每接入一条管道, 则开启接受到这条管道服务端的线程来不停监听客户端写入并将数据写出到别的客户端socket
		}
	}
	
	
	/**
	 * 一个客户端 一条道路
	 * 1、输入流
	 * 2、输出流
	 * 3、接收数据
	 * 4、发送数据
	 * @author Administrator
	 *
	 */
	private class MyChannel implements Runnable{
		private DataInputStream dis ;
		private DataOutputStream dos ;
		private boolean isRunning =true;
		public MyChannel(Socket client ) {
			try {
				dis = new DataInputStream(client.getInputStream());
				dos = new DataOutputStream(client.getOutputStream());
			} catch (IOException e) {
				//e.printStackTrace();
				CloseUtil.closeAll(dis,dos);
				isRunning =false;
			}
		}
		/**
		 * 读取数据
		 * @return
		 */
		private String receive(){
			String msg ="";
			try {
				msg=dis.readUTF();
			} catch (IOException e) {
				//e.printStackTrace();
				CloseUtil.closeAll(dis);
				isRunning =false;
				all.remove(this); //移除自身
			}
			return msg;
		}
		
		/**
		 * 发送数据
		 */
		private void send(String msg){
			if(null==msg ||msg.equals("")){
				return ;
			}
			try {
				dos.writeUTF(msg);
				dos.flush();
			} catch (IOException e) {
				//e.printStackTrace();
				CloseUtil.closeAll(dos);
				isRunning =false;
				all.remove(this); //移除自身
			}
		}
		
		/**
		 * 发送给其他客户端
		 */
		private void sendOthers(){
			String msg = this.receive();
			//遍历容器
			for(MyChannel other:all){
				if(other ==this){
					continue;
				}
				//发送其他客户端
				other.send(msg);
			}
		}
		
		
		@Override
		public void run() {
			while(isRunning){
				sendOthers();
			}
		}
	}
	

}



客户端:

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * 创建客户端: 发送数据+接收数据
 * 写出数据:输出流
 * 读取数据:输入流
 * 
    输入流 与输出流 在同一个线程内 应该 独立处理,彼此独立
    
    
    
 * 
 * 
 * 
 * @author Administrator
 *
 */
public class Client {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws UnknownHostException 
	 */
	public static void main(String[] args) throws UnknownHostException, IOException {
		Socket client = new Socket("localhost",9999);
		new Thread(new Send(client)).start(); //一条路径
		new Thread(new Receive(client)).start(); //一条路径
		
	}

}

客户端发送数据线程:

/**
 * 发送数据 线程
 * @author Administrator
 *
 */
public class Send implements Runnable{
	//控制台输入流
	private BufferedReader console;
	//管道输出流
	private DataOutputStream dos;
	//控制线程
	private boolean isRunning =true;
	public Send() {
		console =new BufferedReader(new InputStreamReader(System.in));
	}
	public Send(Socket client){
		this();
		try {
			dos =new DataOutputStream(client.getOutputStream());
		} catch (IOException e) {
			//e.printStackTrace();
			isRunning =false;
			CloseUtil.closeAll(dos,console);
			
		}
	}
	//1、从控制台接收数据
	private String getMsgFromConsole(){
		try {
			return console.readLine();
		} catch (IOException e) {
			//e.printStackTrace();
		}
		return "";
	}
	/**
	 * 1、从控制台接收数据
	 * 2、发送数据
	 */
	public void send(){
		String msg = getMsgFromConsole();
		try {
			if(null!=msg&& !msg.equals("")){
				dos.writeUTF(msg);
				dos.flush(); //强制刷新
			}
		} catch (IOException e) {
			//e.printStackTrace();
			isRunning =false;
			CloseUtil.closeAll(dos,console);
		}
	}
	
	
	@Override
	public void run() {
		//线程体
		while(isRunning){
			send();
		}
	}

}

客户端接受数据线程:

/**
 * 接收线程 
 * @author Administrator
 *
 */
public class Receive implements Runnable {
	//输入流
	private  DataInputStream dis ;
	//线程标识
	private boolean isRunning = true;
	public Receive() {
	}
	public Receive(Socket client){
		try {
			dis = new DataInputStream(client.getInputStream());
		} catch (IOException e) {
			e.printStackTrace();
			isRunning =false;
			CloseUtil.closeAll(dis);
		}
	}
	/**
	 * 接收数据
	 * @return
	 */
	public String  receive(){
		String msg ="";
		try {
			msg=dis.readUTF();
		} catch (IOException e) {
			e.printStackTrace();
			isRunning =false;
			CloseUtil.closeAll(dis);
		}
		return msg;
	}
	@Override
	public void run() {
		//线程体
		while(isRunning){
			System.out.println(receive());
		}
	}
}

工具类:

/**
 * 关闭流的方法
 * @author Administrator
 *
 */
public class CloseUtil {
	public static void closeAll(Closeable... io){
		for(Closeable temp:io){
			try {
				if (null != temp) {
					temp.close();
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}
}

图 图 :



 

猜你喜欢

转载自chengjianxiaoxue.iteye.com/blog/2182160
今日推荐