server和chat

chat端

public class chat {
	public static void main(String[] args) throws UnknownHostException, IOException {
		Socket client =new Socket("localhost",8888);
		BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
		System.out.println("请输入名字");
		String name=br.readLine();
		new Thread(new send(client,name)).start();
		new Thread(new receive(client)).start();
	}
	static class send implements Runnable{
		BufferedReader br ;
		DataOutputStream dos;
		Socket client;
		String name;
		boolean isrunning ;
		public send(Socket client, String name) {
			isrunning =true;
			this.client=client;
			this.name=name;
			br=new BufferedReader(new InputStreamReader(System.in));
			try {
				dos=new DataOutputStream(client.getOutputStream());
				Send(name);
			} catch (IOException e) {
			}
		}
		public void Send(String msg) {
			try {
				dos.writeUTF(msg);
				dos.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		public String console() {
			try {
				return br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return "";
		}
		public void run() {
			while(isrunning) {
			String msg =console();
			if(!msg.equals("")) {
				Send(msg);
			}
			}
		}
		public void release() {
			this.isrunning=false;
			Utils.close(dos,client);
		}
	}
	static class receive implements Runnable{
		private Socket client;
		private DataInputStream dis;
		private boolean isrunning;
		public receive(Socket client) {
			isrunning=true;
			this.client=client;
			try {
				dis =new DataInputStream(client.getInputStream());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		public String receives() {
			String msg ="";
			try {
				msg=dis.readUTF();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return msg;
		}
		public void run() {
			while(isrunning) {
			String msg =receives();
			if(!msg.equals("")) {
				System.out.println(msg);
			}
			}
		}
		public void release() {
			isrunning =false;
			Utils.close(dis,client);
		}
	}
}

server端

public class server {
	private static CopyOnWriteArrayList<Cannel> all =new CopyOnWriteArrayList<>();
	public static void main(String[] args) throws IOException {
		ServerSocket sever =new ServerSocket(8888);
		while(true) {
		Socket client =sever.accept();
		System.out.println("连接一个客户端");
		Cannel c =new Cannel(client);
		all.add(c);
		new Thread(c).start();
	}
	}
	static class Cannel implements Runnable{
		DataInputStream dis;
		DataOutputStream dos;
		Socket client;
		boolean isrunning;
		String name;
		public Cannel(Socket client) {
			isrunning =true;
			this.client =client;
			try {
				dis =new DataInputStream(client.getInputStream());
				dos =new DataOutputStream(client.getOutputStream());
				this.name =receive();
			} catch (IOException e) {
			}
		}
		public void send(String msg) {
			try {
				dos.writeUTF(msg);
				dos.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		public void sendothers(String msg,boolean isSys) {
			boolean issi =msg.startsWith("@");
			if(issi) {
				int dex =msg.indexOf(":");
				String targentname =msg.substring(1,dex);
				msg =msg.substring(dex+1);
				for(Cannel other:all) {
					if(other.name.equals(targentname)) {
						other.send(this.name+"悄悄对你说"+msg);
						break;
					}
				}
			}
			if(isSys) {
				for(Cannel other:all) {
					send(this.name+"对所有人说"+msg);
				}
			}else {
			for(Cannel other:all) {
				other.send(msg);
			}
			}
		}
		public String receive(){
			String msg ="";
			try {
				msg =dis.readUTF();
			} catch (IOException e) {
			}
			return msg;
		}
		public void run() {
			while(isrunning) {
			String msg =receive();
			if(!msg.equals("")) {
				sendothers(msg,false);
			}
		}
		}
		public void release() {
			this.isrunning=false;
			Utils.close(dos,dis,client);
		}
	}
}

端口经常会被占用

工具类

public static void close(Closeable... targets) {
		for(Closeable target: targets) {
			try {
				if(target!=null) {
					target.close();
				}
			}catch(Exception e) {
				
			}
		}
	}

chat2

public class client2 {
	public static void main(String[] args) throws UnknownHostException, IOException  {
		Socket client =new Socket("localhost",8888);
		//从控制台获得消息
		BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
		System.out.println("请输入名字");
		String name =br.readLine();
		new Thread(new send(client,name)).start();
		new Thread(new receive(client)).start();
	}
	static class send implements Runnable{
		BufferedReader br ;
		DataOutputStream dos;
		Socket client;
		String name;
		boolean isRunning ;
		public send(Socket client, String name) {
			this.client=client;
			this.name=name;
			isRunning =true;
			br=new BufferedReader(new InputStreamReader(System.in));
			try {
				dos=new DataOutputStream(client.getOutputStream());
				Send(name);
			} catch (IOException e) {
			}
		}
		public void run() {
			boolean isRunning =true;
			while(isRunning) {
			String msg =receivee();
			if(!msg.equals("")) {
				Send(msg);
			}
			}
		}
		public void Send(String msg) {
				try {
					dos.writeUTF(msg);
					dos.flush();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		public String receivee() {
			try {
				return br.readLine();
			} catch (IOException e) {
			}
			return "";
		}
		public void release() {
			this.isRunning=false;
			Utils.close(dos,client);
		}
	}
	static class receive implements Runnable{
		private Socket client;
		private DataInputStream dis;
		private boolean isRunning;
		public receive(Socket client) {
			this.isRunning =true;
			this.client=client;
			try {
				dis =new DataInputStream(client.getInputStream());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		public void run() {
			while(isRunning) {
				String msg =receives();
				if(!msg.equals("")) {
					System.out.println(msg);
				}
			}
		}
		public String receives() {
			String msg="";
			try {
				msg=dis.readUTF();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return msg;
		}
		public void release() {
			this.isRunning=false;
			Utils.close(dis,client);
		}
	}
}

server2

public class server2 {
	private static CopyOnWriteArrayList<Cannel> all=new CopyOnWriteArrayList<>();
	public static void main(String[] args) throws Exception {
		ServerSocket sever =new ServerSocket(8888);
		while(true) {
		Socket client =sever.accept();
		System.out.println("建立了一个客户端");
		//接收消息
		Cannel c=new Cannel(client);
		all.add(c);
		new Thread(c).start();
		}
	}
	static class Cannel implements Runnable{
		private DataInputStream dis;
		private DataOutputStream dos;
		private Socket client;
		boolean isRunning ;
		String name;
		public Cannel(Socket client) {
			this.client=client;
			isRunning=true;
			try {
				dis=new DataInputStream(client.getInputStream());
				dos=new DataOutputStream(client.getOutputStream());
				this.name =receive();
			} catch (IOException e) {
				release();
			}
		}
		public String receive() {
				String msg="";
				try {
					msg=dis.readUTF();
				} catch (IOException e) {
					release();
				}
				return msg;
		}
		public void send(String msg) {
			try {
				dos.writeUTF(msg);
				dos.flush();
			} catch (IOException e) {
				release();
			}
		}
		public void sendothers(String msg,boolean isSys) {
			boolean issi =msg.startsWith("@");
			if(issi) {
				int dex =msg.indexOf(":");
				String targentname =msg.substring(1,dex);
				msg =msg.substring(dex+1);
				for(Cannel others:all) {
					if(others.name.equals(targentname)) {
						others.send(this.name+"私聊"+msg);
						break;
					}
				}
			}
			for(Cannel others:all) {
				if(others==this) {
					continue;
				}
				if(isSys) {
					others.send(this.name+"对所有人说"+msg);
				}else {
					others.send(msg);
				}
			}
		}
		public void release() {
			this.isRunning=false;
			Utils.close(dis,dos,client);
		}
		public void run() {
			boolean isRunning =true;
			while(isRunning) {
				String msg =receive();
				if(!msg.equals("")) {
					sendothers(msg, false);
				}
				
			}
	}
	}
}

发布了8 篇原创文章 · 获赞 0 · 访问量 20

猜你喜欢

转载自blog.csdn.net/richpersion/article/details/105125461