码云地址:https://gitee.com/x-alchemist/java_study/tree/master/Exercise4
使用套接写连接编写一个简单的聊天室程序,客户端主函数放在Client_Main.java文件中,服务器端主函数放在Server_Main.java文件中
要求:
1.客户端从控制台进行输入,并将客户端的输出内容和时间保存到“学号.txt”文件中
2.服务器端读取到客户端的程序后,给客户端以回应,并将客户端的输入内容与服务端的输出内容、时间保存到文本文件中
3.要求服务器端可以实现同时与多个客户端进行通信,与每一个客户端通信的内容,保存为一个“学号_ip.txt”的文件
4..提交文件结果包括:代码,通信后生成的txt文件
1 import java.net.*; 2 import java.io.*; 3 import java.util.*; 4 5 public class Client_Main { 6 7 private static String file; 8 9 /** 10 * @param args 11 */ 12 13 @SuppressWarnings("resource") 14 public static void main(String[] args) { 15 // TODO Auto-generated method stub 16 Scanner input = new Scanner(System.in); 17 Socket mysocket = null; 18 DataInputStream in = null; 19 DataOutputStream out = null; 20 21 try { 22 mysocket = new Socket(); 23 System.out.print("please input the ip of Server:"); 24 String ip = input.nextLine(); 25 System.out.print("please input the port of Server:"); 26 int port = input.nextInt(); 27 if (mysocket.isConnected()) { 28 } else { 29 InetAddress address = InetAddress.getByName(ip); 30 InetSocketAddress socketAddress = new InetSocketAddress(address, port); 31 mysocket.connect(socketAddress); 32 in = new DataInputStream(mysocket.getInputStream()); 33 out = new DataOutputStream(mysocket.getOutputStream()); 34 35 36 System.out.print(in.readUTF()); 37 System.out.print("please input your numberofyou:"); 38 39 for (int z = 0; z < 2; z++) { 40 String s = input.nextLine(); 41 File file = new File(s + ".txt"); 42 file.createNewFile(); 43 out.writeUTF(s); 44 } 45 46 System.out.println("please input your question"); 47 48 for (int i = 0; i < 4; i++) { 49 //System.out.print(in.readUTF()); 50 51 Date nowTime = new Date(); 52 //System.out.print("The answer of Servert:"+in.readUTF()); 53 String question = input.nextLine(); 54 out.writeUTF(question); 55 System.out.println(in.readUTF()); 56 //String answer = in.readUTF(); 57 File fwrite = new File(file); 58 Writer outfile = new FileWriter(fwrite); 59 BufferedWriter bufferwrite = new BufferedWriter(outfile); 60 bufferwrite.write(in.readUTF() + ":" + nowTime); 61 bufferwrite.newLine(); 62 bufferwrite.close(); 63 } 64 65 out.close(); 66 67 } 68 } catch (Exception e) { 69 System.out.print(""); 70 71 } 72 73 } 74 75 }
1 import java.net.*; 2 import java.io.*; 3 import java.util.*; 4 5 public class Server_Main { 6 7 /** 8 * @param args 9 */ 10 11 @SuppressWarnings("resource") 12 public static void main(String[] args) { 13 // TODO Auto-generated method stub 14 15 try { 16 // 1.创建服务器端socket,即ServerSocket,并绑定指定的端口,并在端口进行监听 17 ServerSocket serverSocket = new ServerSocket(8888); 18 Socket socket = null; 19 // 记录客户端的数量 20 int count = 0; 21 System.out.println("***服务器即将启动,等待客户端的连接***"); 22 // 循环监听等待客户端的连接 23 while (true) { 24 // 2.通过accept()方法对客户端进行监听,等待客户端的连接 25 socket = serverSocket.accept();// 返回的实例用来与客户端进行通信 26 serverThread serverThread = new serverThread(socket); 27 // 启动线程 28 serverThread.start(); 29 count++; 30 System.out.println("客户端数量: " + count); 31 InetAddress address = socket.getInetAddress(); 32 33 System.out.println("当前客户端的IP: " + address.getHostAddress()); 34 new bianliang().ip = address.getHostAddress(); 35 } 36 37 } catch (IOException e) { 38 e.printStackTrace(); 39 } 40 } 41 42 } 43 44 class bianliang { 45 public static String ip; 46 } 47 48 class xuehao { 49 public static String xuehao; 50 } 51 52 class serverThread extends Thread { 53 InetAddress ip = null; 54 Socket socket; 55 Scanner input = new Scanner(System.in); 56 ServerSocket serverForClient = null; 57 DataOutputStream out = null; 58 DataInputStream in = null; 59 private String xuehao; 60 private String dd; 61 62 serverThread(Socket t) { 63 socket = t; 64 try { 65 out = new DataOutputStream(socket.getOutputStream()); 66 in = new DataInputStream(socket.getInputStream()); 67 } catch (IOException e) { 68 System.out.println("aa"); 69 } 70 71 } 72 73 public void run() { 74 bianliang ss = new bianliang(); 75 String xuehao = null ; 76 try { 77 out.writeUTF("The Link is connected!!\n"); 78 } catch (IOException e) { 79 // TODO Auto-generated catch block 80 e.printStackTrace(); 81 } 82 83 84 for (int i = 0; i <2; i++) { 85 try { 86 xuehao = in.readUTF(); 87 File aa = new File(xuehao+ "_" + ss.ip + ".txt"); 88 aa.createNewFile(); 89 } catch (Exception e) { 90 System.out.print("Fail!!!"); 91 } 92 93 } 94 95 String name = xuehao+"_" + ss.ip + ".txt"; 96 for (int z = 0; z < 4; z++) { 97 Date nowTime = new Date(); 98 try { 99 String question = in.readUTF(); 100 101 System.out.println("The question of Client:"+question); 102 103 System.out.println("The answer of Server:"); 104 String qwer = input.nextLine(); 105 out.writeUTF(qwer); 106 107 File fwrite = new File(name); 108 try { 109 Writer out = new FileWriter(fwrite); 110 BufferedWriter bufferwrite = new BufferedWriter(out); 111 bufferwrite.write(question + ":" + nowTime); 112 String answer = input.nextLine(); 113 bufferwrite.write(answer); 114 bufferwrite.close(); 115 } catch (Exception e) { 116 } 117 118 119 } catch (IOException e) { 120 // TODO Auto-generated catch block 121 e.printStackTrace(); 122 } 123 } 124 } 125 }
PS:差一点,待改进。
原文地址:https://www.cnblogs.com/x-alchemist/p/10129043.html
时间: 2024-10-12 19:47:37