RoomServer.java
RoomUser.java
Send.java
Receive.java
Utils.java
RoomServer.java
package Server; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.concurrent.CopyOnWriteArrayList; public class RoomServer { private static CopyOnWriteArrayList<Channel> all = new CopyOnWriteArrayList<>(); public static void main(String[] args) throws IOException { System.out.println("---服务器开始工作---\n"); ServerSocket server = new ServerSocket(8888); while (true) { Socket client = server.accept(); System.out.println("\n一个客户端建立了连接"); Channel c = new Channel(client); all.add(c); new Thread(c).start(); } } static class Channel implements Runnable { private DataInputStream dis; private DataOutputStream dos; private BufferedReader is; private PrintWriter os ; private Socket client; private boolean isRunning; private String name; private int loginflag; public Channel(Socket client) { this.client = client; loginflag=0; try { dis = new DataInputStream(client.getInputStream()); dos = new DataOutputStream(client.getOutputStream()); is = new BufferedReader(new InputStreamReader(client.getInputStream())); os = new PrintWriter(client.getOutputStream()); isRunning = true; // 登录 while(true) { File a = new File("account.txt"); BufferedReader login =new BufferedReader(new FileReader(a)); String line1 =receive(); this.send("服务器: 已输入账号"); String line2 =receive(); this.send("服务器: 已输入密码"); String name = "user"; String password = "password"; int flag = 0; while((name = login.readLine())!=null && (password = login.readLine())!=null) { if(line1.equals(name) && line2.equals(password)) { this.send("trueMember"); System.out.println("用户"+name+"已登陆"); flag=1; this.name=name; break; } } if(flag==0) { this.send("wrong!"); login.close(); } else { loginflag=1; login.close(); break; } } sendOthers(this.name + "加入群聊", true); } catch (IOException e) { System.out.println("---登录时出现问题---"+e); release(); } } private String receive() { String msg = ""; // 避免空指针 try { //msg = is.readLine(); msg = dis.readUTF(); //msg=dis.readFully(b); //msg = dis.readLine(); } catch (IOException e) { if(isRunning==true) System.out.println("---接受消息出现问题---"+e); release(); isRunning=false; } return msg; } private void send(String msg) { try { //os.println(msg); //os.flush(); dos.writeUTF(msg); dos.flush(); } catch (IOException e) { if(isRunning==true) System.out.println("---发送消息出现问题---"); release(); isRunning=false; } } private void sendOthers(String msg, boolean isSys) { boolean isPrivate = msg.startsWith("@"); if (isPrivate) { // 私聊 int index = msg.indexOf(":"); // 第一次冒号出现的位置 if(index>=0) { // 获取目标和数据 String targetName = msg.substring(1, index); msg = msg.substring(index+1); //System.out.println(this.name+" "+targetName+""+msg); for (Channel other: all) { if (other.name.equals(targetName)) { // 目标 System.out.println(this.name+"对"+targetName+"的私信:"+msg); other.send(this.name + "对您说悄悄话: " + msg); // 群聊消息 } } } } else { if (!isSys) System.out.println(this.name+":"+msg); for (Channel other : all) { if (other == this) { continue; } if (!isSys) { other.send(this.name + ": " + msg); // 群聊消息 } else { other.send("系统消息:" + msg); // 系统消息 } } } } private void fileupload() { try { String filename = receive(); long fileLength = dis.readLong(); File file = new File(filename); FileOutputStream fos = new FileOutputStream(file); byte[] bytes = new byte[1024]; int length = 0; long progress = 0; int percent=0; int per_last=-1; System.out.println("======== 开始接收"+this.name+"发送给服务器的文件:"+filename+" ========"); while((length = dis.read(bytes, 0, bytes.length)) != -1) { fos.write(bytes, 0, length); fos.flush(); progress += length; percent=(int)(100*progress/fileLength); if(percent!=per_last) System.out.print("| " + percent + "% |"); if(percent%10==0 && percent!=0 && percent!=per_last) System.out.print("\n"); per_last=percent; if(percent>=100) break; } System.out.println("======== "+this.name+"上传了文件:"+filename+" ========"); } catch(IOException e) { System.out.println("========文件上传时异常========"+e); } return; } private void filedownload() { try { String filename = receive(); // File directory = new File("transport_file"); // if(!directory.exists()) { // directory.mkdir(); // } // File file = new File(directory.getAbsolutePath()+File.separatorChar+filename); // //System.out.println("路径:"+directory.getAbsolutePath() + File.separatorChar + filename); File file = new File(filename); long fileLength=file.length(); if(file.exists()) { send("filedownload"); send(filename); dos.writeLong(fileLength); dos.flush(); FileInputStream fis = new FileInputStream(filename); byte[] bytes = new byte[1024]; int length = 0; long progress = 0; int percent=0; int per_last=-1; System.out.println("======== 开始向"+this.name+"发送服务器文件:"+filename+" ========"); while((length = fis.read(bytes, 0, bytes.length)) != -1) { dos.write(bytes, 0, length); dos.flush(); progress += length; percent=(int)(100*progress/fileLength); if(percent!=per_last) System.out.print("| " + percent + "% |"); if(percent%10==0 && percent!=0 && percent!=per_last) System.out.print("\n"); per_last=percent; } System.out.println("======== "+this.name+"下载了文件:"+filename+" ========"); } } catch(IOException e) { System.out.println("========文件下载时异常========"+e); } return; } private void filestream() { try { String target = receive(); String filename = receive(); long fileLength = dis.readLong(); for (Channel other : all) { if (other.name.equals(target)) { other.send("filestream"); other.send(this.name); other.send(filename); other.dos.writeLong(fileLength); other.dos.flush(); byte[] bytes = new byte[1024]; int length = 0; long progress = 0; int percent=0; int per_last=-1; System.out.println("======== 开始转发" +this.name+"向"+target+"发送的文件:"+filename+" ========"); while((length = dis.read(bytes, 0, bytes.length)) != -1) { other.dos.write(bytes, 0, length); other.dos.flush(); progress += length; percent=(int)(100*progress/fileLength); if(percent!=per_last) System.out.print("| " + percent + "% |"); if(percent%10==0 && percent!=0 && percent!=per_last) System.out.print("\n"); per_last=percent; if(percent>=100) break; } System.out.println("======== " +this.name+"向"+target+"发送了文件:"+filename+" ========"); } } } catch(IOException e) { System.out.println("======== 文件转发时异常 ========"+e); } return; } @Override public void run() { while (isRunning==true && loginflag==1) { String msg = receive(); if (msg.equals("exit")) { this.send(msg); release(); } else if (msg.equals("fileupload")) { fileupload(); } else if (msg.equals("filedownload")) { filedownload(); } else if (msg.equals("filestream")) { filestream(); } else if (!msg.equals("") ) { sendOthers(msg, false); } } } private void release() { isRunning = false; if(this.name!=null) System.out.println(this.name+"断开了连接"); Utils.close(dos,dis,client); all.remove(this); if(this.name!=null) sendOthers("======== "+this.name + "离开聊天室 ========", true); } } }
RoomUser.java
package Client; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; public class RoomUser { public static void main(String[] args) throws IOException { System.out.println("======== 客户端开始工作 ========"); BufferedReader sin = new BufferedReader(new InputStreamReader(System.in)); Socket client = new Socket("47.112.128.10", 8888); //Socket client = new Socket("127.0.0.1", 8888); DataInputStream dis=new DataInputStream(client.getInputStream()); DataOutputStream dos=new DataOutputStream(client.getOutputStream()); String name=""; while(true) { System.out.println("输入账号: "); String readline1 = sin.readLine(); name = readline1; dos.writeUTF(readline1); dos.flush(); System.out.println(dis.readUTF()); System.out.println("输入密码:"); String readline2 = sin.readLine(); dos.writeUTF(readline2); dos.flush(); System.out.println(dis.readUTF()); if(dis.readUTF().equals("trueMember")) { System.out.println("\n欢迎来到聊天室!\n"); System.out.println("如果你要上传文件,请输入fileupload 如果你要下载文件,请输入filedownload"); System.out.println("私发文件,输入filestream\n"); break; } else System.out.println("\n你的账号或者密码出错了\n"); } new Thread(new Send(client, name)).start(); new Thread(new Receive(client)).start(); } }
Send.java
package Client; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.Socket; public class Send implements Runnable { private BufferedReader console; private DataOutputStream dos; private Socket client; private boolean isRunning; private String name; public Send(Socket client, String name) { this.client = client; console = new BufferedReader(new InputStreamReader(System.in)); this.isRunning = true; this.name = name; try { dos = new DataOutputStream(client.getOutputStream()); } catch (IOException e) { System.out.println("Send类构造时异常"); release(); } } private String getStrFromConsole() { try { return console.readLine(); } catch (IOException e) { e.printStackTrace(); return ""; } } private void send(String msg) { try { dos.writeUTF(msg); dos.flush(); } catch (IOException e) { System.out.println("---客户发送端发送消息异常---"); release(); } } private void filestream() { try { String msg=""; FileInputStream fis = null; send("filestream"); System.out.println("请输入 @目标用户名:文件路径 "); msg = getStrFromConsole(); int index = msg.indexOf(":"); String targetName = msg.substring(1, index); String root = msg.substring(index+1); File file = new File(root); String filename = file.getName(); if(file.exists()) { send(targetName); send(filename); long Length=file.length(); dos.writeLong(Length); dos.flush(); fis = new FileInputStream(root); System.out.println("======== 正在向"+targetName+"发送文件:"+filename+" ========"); byte[] bytes = new byte[1024]; int length = 0; long progress = 0; int percent=0; int per_last=-1; while((length = fis.read(bytes, 0, bytes.length)) != -1) { dos.write(bytes, 0, length); dos.flush(); progress += length; percent=(int)(100*progress/Length); if(percent!=per_last) System.out.print("| " + percent + "% |"); if(percent%10==0 && percent!=0 && percent!=per_last) System.out.print("\n"); per_last=percent; } //send(""); System.out.println("======== 文件发送成功 ========"); } } catch(IOException e) { System.out.println("======== 文件发送时异常 ========"+e); } return; } private void filedownload() { String msg=""; send("filedownload"); System.out.println("请输入想要下载的服务器上的文件名 "); msg = getStrFromConsole(); String filename = msg; send(filename); return; } private void fileupload() { try { String msg=""; FileInputStream fis = null; send("fileupload"); System.out.println("请输入文件路径"); String root = getStrFromConsole(); File file = new File(root); String filename=file.getName(); if(file.exists()) { send(filename); long Length=file.length(); dos.writeLong(Length); dos.flush(); fis = new FileInputStream(root); System.out.println("======== 正在上传文件:"+filename+" ========"); byte[] bytes = new byte[1024]; int length = 0; long progress = 0; int percent=0; int per_last=-1; while((length = fis.read(bytes, 0, bytes.length)) != -1) { dos.write(bytes, 0, length); dos.flush(); progress += length; percent=(int)(100*progress/Length); if(percent!=per_last) System.out.print("| " + percent + "% |"); if(percent%10==0 && percent!=0 && percent!=per_last) System.out.print("\n"); per_last=percent; } System.out.println("======== 文件上传成功 ========"); } } catch(IOException e) { System.out.println("======== 文件上传时异常 ========"+e); } return; } @Override public void run() { while (isRunning) { String msg = getStrFromConsole(); if(msg.equals("exit")) { send(msg); release(); } else if (msg.equals("fileupload")) { fileupload(); } else if (msg.equals("filedownload")) { filedownload(); } else if (msg.equals("filestream")) { filestream(); } else if (!msg.equals("")) { send(msg); } } } private void release() { this.isRunning = false; Utils.close(dos,client); } }
Receive.java
package Client; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.Socket; public class Receive implements Runnable { private DataInputStream dis; private Socket client; private boolean isRunning; public Receive(Socket client) { this.client = client; try { dis = new DataInputStream(client.getInputStream()); isRunning = true; } catch (IOException e) { System.out.println("---客户接收端构造时异常---"); release(); } } private String receive() { String msg = ""; try { msg = dis.readUTF(); } catch (IOException e) { System.out.println("---客户接收端接收消息异常---"+e); release(); } return msg; } private void release() { isRunning = false; Utils.close(dis, client); } private void filedownload() { String filename=receive(); System.out.println("======== 正在下载文件:"+filename+" ========"); try { long fileLength = dis.readLong(); File directory = new File("download_file"); if(!directory.exists()) { directory.mkdir(); } File file = new File(directory.getAbsolutePath() + File.separatorChar + filename); FileOutputStream fos = new FileOutputStream(file); byte[] bytes = new byte[1024]; int length = 0; long progress = 0; int percent=0; int per_last=-1; while((length = dis.read(bytes, 0, bytes.length)) != -1) { fos.write(bytes, 0, length); fos.flush(); progress += length; percent=(int)(100*progress/fileLength); if(percent!=per_last) System.out.print("| " + percent + "% |"); if(percent%10==0 && percent!=0 && percent!=per_last) System.out.print("\n"); per_last=percent; if(percent>=100) break; } System.out.println("======== 文件下载成功 ========"); } catch(IOException e) { System.out.println("======== 文件下载时异常 ========"+e); } return; } private void filestream() { try { String source =receive(); String filename=receive(); System.out.println("======== 正在接收来自"+source+"的文件:"+filename+" ========"); long fileLength = dis.readLong(); File directory = new File("download_file"); if(!directory.exists()) { directory.mkdir(); } File file = new File(directory.getAbsolutePath() + File.separatorChar + filename); FileOutputStream fos = new FileOutputStream(file); byte[] bytes = new byte[1024]; int length = 0; long progress = 0; int percent=0; int per_last=-1; while((length = dis.read(bytes, 0, bytes.length)) != -1) { fos.write(bytes, 0, length); fos.flush(); progress += length; percent=(int)(100*progress/fileLength); if(percent!=per_last) System.out.print("| " + percent + "% |"); if(percent%10==0 && percent!=0 && percent!=per_last) System.out.print("\n"); per_last=percent; if(percent>=100) break; } System.out.println("======== 文件接收成功 ========"); } catch(IOException e) { System.out.println("======== 文件接收时异常 ========"+e); } return; } @Override public void run() { while (isRunning) { String msg = receive(); if(msg.equals("exit")) { release(); } else if(msg.equals("filedownload")) { filedownload(); } else if(msg.equals("filestream")) { filestream(); } else if (!msg.equals("")) { System.out.println(msg); } } } }
Utils.java
package Server; import java.io.Closeable; public class Utils { public static void close(Closeable... targets) { for (Closeable target: targets) { try { if (null != target) { target.close(); } }catch (Exception e) { e.printStackTrace(); } } } }