第四次过程性考核
2018-12-12 10:10 POOOOO 阅读(394) 评论(4) 编辑 收藏 举报第四次过程性考核
码云:https://gitee.com/Mchain/projects
使用套接写连接编写一个简单的聊天室程序,客户端主函数放在Client_Main.java文件中,服务器端主函数放在Server_Main.java文件中
要求:
- 1.客户端从控制台进行输入,并将自己的输出内容和时间保存到数据库的“client_学号”表中
- 2.服务器端读取到客户端的程序后,从控制台进行输入给客户端以回应,并将客户端的输入内容与服务端的输出内容、时间保存到数据库的表中
- 3.要求服务器端可以实现同时与多个客户端进行通信,与每一个客户端通信的内容,保存为一个"ip_学号"的表
- 4.提交文件结果包括:代码,数据库导出为.sql文件
程序设计思路:最开始的思路是将TCP单线程通信进行更改,参照了书上403页的圆半径的例子。最后越改越乱没成功。然后参照了网上的例子,首先我们得用端口号进行通信,因为IP地址一样。然后网上用了不同的用户名进行区分对话。我尝试了一下,一直有错误,询问了同学,以书上张三的通信例子为参考,进行了修改。但是最后的成果仍然有错误,所以讲错误的代码部分进行了删减,不会的询问了同学。代码先这样提交上来。
一,客户端从控制台进行输入,并将自己的输出内容和时间保存到数据库的“client_学号”表中
Client_Main.java
import java.net.*; import java.util.*; public class Client_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,666); DatagramSocket postman=new DatagramSocket(); System.out.print("请输入给服务器发送到消息:"); while(scanner.hasNext()){ String mess=scanner.nextLine(); buffer =mess.getBytes(); /*String jilu="(mess,null)"; String sqlStr="insert into mess values"+jilu;*/ 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); } /*try{ sql=con.createStatement(); int ok=sql.executeUpdate(sqlStr); rs=sql.executeQuery("select * from client_学号"); while(rs.next()){ String mess=rs.getString(1); String time=rs.getString(2); System.out.print(mess); System.out.print(time); } con.close(); } catch(SQLException e){ System.out.println(e); }*/ } }
ReceiveLetterForClient.java
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(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.printf("%25s\n","收到:"+message); } catch(Exception e){} } } } }
Server_Main.java
import java.net.*; import java.util.*; import java.sql.*; public class Server_Main { public static void main(String[] args) { /*Connection con=null; Statement sql; ResultSet rs; con =GetDBConnection.connectDB("students","root","111111"); if (con==null)return;*/ 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,888); DatagramSocket postman=new DatagramSocket(); System.out.print("请输入给客户端发送到消息:"); while(scanner.hasNext()){ String mess=scanner.nextLine(); buffer =mess.getBytes(); /*String jilu="(mess,null)"; String sqlStr="insert into mess values"+jilu;*/ 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); } /*try{ sql=con.createStatement(); int ok=sql.executeUpdate(sqlStr); rs=sql.executeQuery("select * from client_学号"); while(rs.next()){ String mess=rs.getString(1); String time=rs.getString(2); System.out.print(mess); System.out.print(time); } con.close(); } catch(SQLException e){ System.out.println(e); }*/ } }
ReceiveLetterForServer.java
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(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.printf("%25s\n","收到:"+message); } catch(Exception e){} } } } }
二:将所需数据提出,建立SQL表格
因为不能提交文件夹,所以这里没有提交上文件。
三:实现数据库
GetDBConnection.java
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(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.printf("%25s\n","收到:"+message); } catch(Exception e){} } } } }
总结:对程序设计的框架有基本概念和思想,在代码的实现上,还是出现了很多错误,甚至有些错误依旧没有更改过来,问题还是最原始的,代码量和代码的理解上不充足。
学习内容 | 代码行数 | 博客字数 |
第四次考核 | 166 | 230 |