第四次过程性考核
第四次考核码云地址:https://gitee.com/lixin-123/fourth_process_assessment
课堂练习码云地址;https://gitee.com/lixin-123/class_exercises_34
使用套接写连接编写一个简单的聊天室程序,客户端主函数放在Client_Main.java文件中,服务器端主函数放在Server_Main.java文件中
要求:
1.客户端从控制台进行输入,并将客户端的输出内容和时间保存到“学号.txt”文件中
2.服务器端读取到客户端的程序后,给客户端以回应,并将客户端的输入内容与服务端的输出内容、时间保存到文本文件中
3.要求服务器端可以实现同时与多个客户端进行通信,与每一个客户端通信的内容,保存为一个“学号_ip.txt”的文件
4.4.提交文件结果包括:代码,通信后生成的txt文件
程序设计思路:根据题目要求可知,是要编写一个简单的聊天程序,服务器端读取到客户端的程序后,给客户端以回应
运用到的知识点;DatagramPacket类、DatagramSocket类、方法receive
代码如下:
客户端主函数:
import java.net.*; import java.util.*; public class Client_Main{ public static void main(String args[]){ Scanner scanner = new Scanner(System.in); Thread readData; ReceiveLetterForClient receiver = new ReceiveLetterForClient(); try{ readData = new Thread(receiver); readData.start(); byte []buffer= new byte[1]; InetAddress address =InetAddress.getByName("127.0.0.1"); DatagramPacket datapack= new DatagramPacket(buffer,buffer.length,address,666); DatagramSocket postman = new DatagramSocket(); System.out.print("输入发送给服务器的信息:"); while (scanner.hasNext()){ String mess = scanner.nextLine(); buffer = mess.getBytes(); if(mess.length()==0) System.exit(0); buffer= mess.getBytes(); datapack.setData(buffer); postman.send(datapack); System.out.print("继续输入发送给服务器的信息:"); } } catch(Exception e){ System.out.println(e); } } }
import java.net.*; public class ReceiveLetterForClient implements Runnable{ public void run(){ DatagramPacket pack=null; DatagramSocket postman=null; byte data[]=new byte[8192]; try{ pack = new DatagramPacket(data,data.length); postman=new DatagramSocket(888); } catch(Exception e){} while(true){ if(postman==null)break; else try{ postman.receive(pack); String message=new String(pack.getData(),0,pack.getLength()); System.out.println("收到:"+message); } catch(Exception e){} } } }
服务器端主函数
import java.net.*; import java.util.*; public class Server_Main{ public static void main(String args[]){ Scanner scanner = new Scanner(System.in); Thread readData; ReceiveLetterForServer receiver = new ReceiveLetterForServer(); try{ readData = new Thread(receiver); readData.start(); byte []buffer= new byte[1]; InetAddress address =InetAddress.getByName("127.0.0.1"); DatagramPacket datapack= new DatagramPacket(buffer,buffer.length,address,888); DatagramSocket postman = new DatagramSocket(); System.out.print("输入发送给客户端的信息:"); while (scanner.hasNext()){ String mess = scanner.nextLine(); buffer = mess.getBytes(); if(mess.length()==0) System.exit(0); buffer= mess.getBytes(); datapack.setData(buffer); postman.send(datapack); System.out.print("继续输入发送给客户端的信息:"); } } catch(Exception e){ System.out.println(e); } } }
import java.net.*; public class ReceiveLetterForServer implements Runnable{ public void run(){ DatagramPacket pack=null; DatagramSocket postman=null; byte data[]=new byte[8192]; try{ pack = new DatagramPacket(data,data.length); postman=new DatagramSocket(666); } catch(Exception e){} while(true){ if(postman==null)break; else try{ postman.receive(pack); String message=new String(pack.getData(),0,pack.getLength()); System.out.println("收到:"+message); } catch(Exception e){} } } }
运行结果
总结:这次考核还有一部分没有完成,虽然这次考核代码是参考书上的,但是自己也确实收获挺大的,临近期末JAVA课程也快结束了,发现有时有些知识点不知道怎么用,有时有些思路也编不出程序,思维不够灵活,但是尽管如此通过一学期的学习,学到的东西仍然挺多的
学习内容 | 代码行数 | 博客字数 |
第四次考核 | 113 | 300 |
课堂练习 | 167 |