第四次过程性考核
码云地址:https://gitee.com/wslgx/assessment_four/tree/master
*160120
使用套接写连接编写一个简单的聊天室程序,客户端主函数放在Client_Main.java文件中,服务器端主函数放在Server_Main.java文件中
要求:
- 1.客户端从控制台进行输入,并将客户端的输出内容和时间保存到“学号.txt”文件中
- 2.服务器端读取到客户端的程序后,给客户端以回应,并将客户端的输入内容与服务端的输出内容、时间保存到文本文件中
- 3.要求服务器端可以实现同时与多个客户端进行通信,与每一个客户端通信的内容,保存为一个“学号_ip.txt”的文件
- 4.4.提交文件结果包括:代码,通信后生成的txt文件
程序设计思路:编写一个简单的聊天程序,再添加日志文件。
运用到的知识点:多线程,文件的输入与存储,网络编程。
客户端:
import java.io.*; import java.net.*; import java.util.*; public class Client_Main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner = new Scanner(System.in); Socket mysocket; Thread readData; Read read = null; DataInputStream in = null; DataOutputStream out = null; try{ mysocket = new Socket(); readData = new Thread(read); System.out.print("输入服务器的IP:"); String IP =scanner.nextLine(); System.out.print("输入端口号:"); int port =scanner.nextInt(); if(mysocket.isConnected()){} else{ InetAddress address=InetAddress.getByName(IP); InetSocketAddress socketAddress = new InetSocketAddress(address,port); mysocket.connect(socketAddress); in = new DataInputStream(mysocket.getInputStream()); out = new DataOutputStream(mysocket.getOutputStream()); read.setDataInputStream(in); readData.start(); } } catch(Exception e) { System.out.println("服务器已断开"+e); } while(scanner.hasNext()){ try{ out.writeUTF(scanner.next()); } catch(Exception e){} } } } class Read implements Runnable{ Scanner scanner = new Scanner(System.in); DataInputStream in; Date nowtime = new Date(); void setDataInputStream(DataInputStream in){ this.in=in; } public void run(){ while(true){ String s; try { s = in.readUTF(); System.out.println("客户收到服务器的回答:"+s+nowtime); Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
服务器端:
import java.net.*; import java.io.*; public class Server_Main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ServerSocket serverForClient = null; Socket socketOnServer = null; ServerThread thread; while(true){ try{ serverForClient = new ServerSocket(2010); } catch(IOException el){ System.out.println("正在监听"); } try{ System.out.println("等待客户呼叫"); socketOnServer = serverForClient.accept(); System.out.println("客户的地址:"+serverForClient.getInetAddress()); } catch(IOException e){ System.out.println("正在等待客户"); } if(serverForClient!=null){ new ServerThread(socketOnServer).start(); } } } } class ServerThread extends Thread{ Socket socket; DataOutputStream out =null; DataInputStream in = null; ServerThread(Socket t){ socket =t; try{ out = new DataOutputStream(socket.getOutputStream()); in = new DataInputStream(socket.getInputStream()); } catch(IOException e){} } public void run(){ while(true){ try{ String s = in.readUTF(); System.out.println("服务器收到客户的提问"+s); out.writeUTF("回应"); Thread.sleep(500); } catch(Exception e){ System.out.println("客户已断开"+e); } } } }
运行结果:未完成,无结果。
总结:知识学的太死,缺乏灵活的运用与理解,基础也不够扎实,需要更加努力。