第四次过程性考核
码云仓库链接:https://gitee.com/xywymxs/16012005_zhou_ning_test4
考核要求:
使用套接写连接编写一个简单的聊天室程序,客户端主函数放在Client_Main.java文件中,服务器端主函数放在Server_Main.java文件中
要求:
1.客户端从控制台进行输入,并将客户端的输出内容和时间保存到“学号.txt”文件中
2.服务器端读取到客户端的程序后,给客户端以回应,并将客户端的输入内容与服务端的输出内容、时间保存到文本文件中
3.要求服务器端可以实现同时与多个客户端进行通信,与每一个客户端通信的内容,保存为一个“学号_ip.txt”的文件
4.提交文件结果包括:代码,通信后生成的txt文件
程序设计思路:通过套接字和多线程进行客户端和服务器端一对一或者一对多的简单通信。
涉及知识点:输入输出流;客户端套接字;服务器端套接字;多线程等
代码:
Client_Main.java
1 import java.net.*; 2 import java.io.*; 3 import java.util.*; 4 public class Client_Main { 5 public static void main(String[] args) { 6 Scanner input = new Scanner(System.in); 7 Socket mysocket = null; 8 DataInputStream in = null; 9 DataOutputStream out = null; 10 try { 11 mysocket = new Socket(); 12 System.out.print("please input the ip of Server:"); 13 String ip = input.nextLine(); 14 System.out.print("please input the port of Server"); 15 int port = input.nextInt(); 16 if (mysocket.isConnected()) { 17 } else { 18 InetAddress address = InetAddress.getByName(ip); 19 InetSocketAddress socketAddress = new InetSocketAddress(address, port); 20 mysocket.connect(socketAddress); 21 in = new DataInputStream(mysocket.getInputStream()); 22 out = new DataOutputStream(mysocket.getOutputStream()); 23 System.out.print("please input your number:"); 24 String s = input.nextLine(); 25 File file = new File(s + ".txt"); 26 if(s!=null) { 27 out.writeUTF(s); 28 } 29 System.out.println("please input your question"); 30 for (int i = 0; i < 3; i++) { 31 Date nowTime = new Date(); 32 String question = input.nextLine(); 33 out.writeUTF(question); 34 System.out.println(in.readUTF()); 35 Writer outfile = new FileWriter(file); 36 BufferedWriter bufferwrite = new BufferedWriter(outfile); 37 bufferwrite.write(in.readUTF() +":"+ nowTime); 38 bufferwrite.newLine(); 39 } 40 } 41 } catch (Exception e) { 42 System.out.print(""); 43 } 44 } 45 }
Server_Main.java
1 import java.net.*; 2 import java.io.*; 3 import java.util.*; 4 public class Server_Main { 5 public static void main(String[] args) { 6 try { 7 //创建服务器端socket,并绑定指定的端口 8 ServerSocket serverSocket=new ServerSocket(8888); 9 Socket socket = null; 10 int count = 0; 11 System.out.println("服务器正在启动,请客户端申请连接"); 12 while(true){ 13 socket=serverSocket.accept(); 14 serverThread serverThread = new serverThread(socket); 15 serverThread.start(); 16 count++; 17 System.out.println("客户端数量: "+count); 18 InetAddress address = socket.getInetAddress(); 19 bianliang a = new bianliang(); 20 a.ip = address.getHostAddress(); 21 System.out.println("当前客户端的IP: "+address.getHostAddress()); 22 } 23 } catch (IOException e) { 24 e.printStackTrace(); 25 } 26 } 27 } 28 class bianliang { 29 String ip ; 30 } 31 class serverThread extends Thread { 32 InetAddress ip = null; 33 Socket socket; 34 Scanner input = new Scanner(System.in); 35 ServerSocket serverForClient = null; 36 DataOutputStream out = null; 37 DataInputStream in = null; 38 serverThread(Socket t) { 39 socket = t; 40 try { 41 out = new DataOutputStream(socket.getOutputStream()); 42 in = new DataInputStream(socket.getInputStream()); 43 } catch (IOException e) { 44 System.out.println("aa"); 45 } 46 } 47 public void run() { 48 49 try { 50 String xuehao = in.readUTF(); 51 System.out.print(xuehao); 52 File aa = new File(xuehao+".txt"); 53 } catch (IOException e1) { 54 e1.printStackTrace(); 55 } 56 String xuehao = null; 57 bianliang ss = new bianliang(); 58 String name = xuehao + "_" + ss.ip + ".txt"; 59 for (int z = 0; z < 3; z++) { 60 Date nowTime = new Date(); 61 try { 62 @SuppressWarnings("unused") 63 String question = in.readUTF(); 64 File fwrite = new File(name); 65 try { 66 Writer out = new FileWriter(fwrite); 67 BufferedWriter bufferwrite = new BufferedWriter(out); 68 bufferwrite.write(in.readUTF()+":"+nowTime); 69 String answer = input.nextLine(); 70 bufferwrite.write(answer); 71 bufferwrite.close(); 72 } 73 catch(Exception e) { 74 } 75 } catch (IOException e) { 76 e.printStackTrace(); 77 } 78 } 79 } 80 }
程序运行结果:由于没有按要求完美运行,便没有粘贴运行结果图片。
错误总结:由于课后练得少并且对知识点理解的不透彻,导致编译的时候经常出错误,在解决问题的时候还缺少技巧,导致在考核中只编写了客户端而且错误还层出不穷,所以只好在课余时间完善程序。还有就是在课上听着感觉回了,结果自己一动手编,就不知道从何下手,所以不仅要听会,还要多总结错误的原因,最后总结一套自己做题方法,这样才不会有无从下手的事情产生。
学习内容 | 代码行数 | 博客字数 |
输入输出流 | 150行 | --- |
多线程机制 | 100行 | --- |
Java网络编程 | 200行 | --- |
阶段性测试四 | 125行 | 500字 |