第四次过程性考核
码云地址:https://gitee.com/lsylsyhwy/fourth_process_assessment/tree/master
第一步 使用套接字编写多线程通信任务:
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表格
/* Navicat MySQL Data Transfer
Source Server : ccy Source Server Version : 50520 Source Host : localhost:3306 Source Database : students
Target Server Type : MYSQL Target Server Version : 50520 File Encoding : 65001
Date: 2018-12-05 11:23:24 */
SET FOREIGN_KEY_CHECKS=0;
-- ---------------------------- -- Table structure for client_学号 -- ---------------------------- DROP TABLE IF EXISTS `client_学号`; CREATE TABLE `client_学号` ( `内容` varchar(255) DEFAULT NULL, `时间` varchar(255) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ---------------------------- -- Records of client_学号 -- ----------------------------
-- ---------------------------- -- Table structure for ip_学号 -- ---------------------------- DROP TABLE IF EXISTS `ip_学号`; CREATE TABLE `ip_学号` ( `IP` varchar(255) DEFAULT NULL, `内容` varchar(255) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ---------------------------- -- Records of ip_学号 -- ----------------------------
-- ---------------------------- -- Table structure for mess -- ---------------------------- DROP TABLE IF EXISTS `mess`; CREATE TABLE `mess` ( `number` char(50) NOT NULL DEFAULT '', `name` varchar(100) DEFAULT NULL, `birthday` date DEFAULT NULL, `height` float DEFAULT NULL, PRIMARY KEY (`number`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ---------------------------- -- Records of mess -- ---------------------------- INSERT INTO `mess` VALUES ('R1001', '张三', '2000-12-12', '1.78'); INSERT INTO `mess` VALUES ('R1002', '李四', '1999-10-09', '1.68'); INSERT INTO `mess` VALUES ('R1003', '赵小五', '1997-03-09', '1.65');
三:实现数据库:
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){}
}
}
}
}
JAVA学习总结:本学期通过郑蕊老师的谆谆教导学到了很多关于java的关键性内容,搞懂了很多复杂的难懂的编程任务,通过几次不多的过程性考核和作业更加充实了自己对Java这门学科的认识,但是在学习Java的这一段时间里也发现了自己还有许多方面的不足需要继续学习改进,最后我也会在课余之时多加学习和研究。
| 学习内容 | 代码行数 | 博客字数 |
| 第四次过程性考核 | 192 | 200 |

浙公网安备 33010602011771号