第四次过程性考核
本次作业代码地址:https://gitee.com/x-h-b/disiciguochengxingkaohe_xuxiaodong
本次作业
使用套接写连接编写一个简单的聊天室程序,客户端主函数放在Client_Main.java文件中,服务器端主函数放在Server_Main.java文件中
要求:
- 1.客户端从控制台进行输入,并将客户端的输出内容和时间保存到“学号.txt”文件中
- 2.服务器端读取到客户端的程序后,给客户端以回应,并将客户端的输入内容与服务端的输出内容、时间保存到文本文件中
- 3.要求服务器端可以实现同时与多个客户端进行通信,与每一个客户端通信的内容,保存为一个“学号_ip.txt”的文件
- 4.4.提交文件结果包括:代码,通信后生成的txt文件
解题思路:首先用DatagramSocket的另一个构造方法DatagramSocket(int port)创建一个对象,其中的参数必须和待接收数据包的端口号相同。然后对象mail_in使用方法receive(DatagramPacket pack)接收数据包。该方法有一个数据包参数pack,方法receive吧收到的数据包传递给改参数。因此必须准备一个数据包以便收取数据包。这时需使用DatagramPack类的另一个构造方法DatagramPack(byte data[],int length)创建一个数据包,用于接收数据包。
Server_Main
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); } } }
Client_Main
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); } } }
ReceiveLetterForServer
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){} } } }
ReceiveLetterForServer
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的学习还是应跟加努力啊!