第四次过程性考核

考核任务:

使用套接写连接编写一个简单的聊天室程序,客户端主函数放在Client_Main.java文件中,服务器端主函数放在Server_Main.java文件中 

要求: 

  • 1.客户端从控制台进行输入,并将自己的输出内容和时间保存到数据库的“client_学号”表中
  • 2.服务器端读取到客户端的程序后,从控制台进行输入给客户端以回应,并将客户端的输入内容与服务端的输出内容、时间保存到数据库的表中
  • 3.要求服务器端可以实现同时与多个客户端进行通信,与每一个客户端通信的内容,保存为一个"ip_学号"的表
  • 4.提交文件结果包括:代码,数据库导出为.sql文件

注:以下为部分代码(未完成全部要求)

客户端代码:

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.print("收到:"+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.print("收到:"+message);
                }
                catch(Exception e){}
            }
        }
    }

 

连接数据库:

import java.sql.*;
public class GetDBConnection {    
    public static Connection connectDB(String DBName,String id,String p){
        Connection con=null;
        String uri="jdbc:mysql://localhost:3306/"+DBName+"?useSSL=true&characterEncoding=utf-8";
        try{
            Class.forName("com.mysql.jdbc.Driver");
        }
        catch(Exception e){}
        try{
            con=DriverManager.getConnection(uri,id,p);
        }
        catch(SQLException e){}
        return con;
    }
}

 

运行结果:

 

 

我的码云地址:https://gitee.com/tzy123/projects

总结:这次考试的要求,我只按照书上的代码做出了一个简单的一个客户机对一个服务器的聊天程序,连要求1都没有完成,主要将客户端和服务器输入输出的内容和时间都上传到数据库这一部分我一点想法都没有。我觉得这阶段的课听得不认真,而且课上做的练习基本都是按书上的打的,很多都没动脑理解,有些老师让在练习的基础上做的扩展我也没做,导致这次的题我一点思路都没有。所以,我以后尽量会在周末的时候多花点时间学习这一部分的内容。

 

学习内容 代码行数 博客字数
第四次过程性考核 124 200
 
 
 
 
posted @ 2018-12-13 18:53  demi111  阅读(187)  评论(0编辑  收藏  举报