第四次过程性考核

考核内容:

*160121

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

要求: 

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

 

服务器端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.io.*;
import java.net.*;
public class Server_Main {
  public static void main (String args []) {
   String [] answer = {"我收到你的程序了","我要把你的输入内容我的输出内容还有时间保存到数据库中"};
   ServerSocket serverForClient_Main = null;
   Socket socketOnServer = null;
   DataOutputStream out = null;
   DataInputStream in = null;
   try {  serverForClient_Main = new ServerSocket(2010);
      }
   catch(IOException el) {
       System.out.println(el);
    }
   try{   System.out.println("等待客户呼叫");
       socketOnServer = serverForClient_Main.accept();
       out = new DataOutputStream(socketOnServer.getOutputStream());
       in = new DataInputStream(socketOnServer.getInputStream());
       for(int i=0;i<answer.length;i++){
          String s = in.readUTF();
          System.out.println("服务器收到客户的提问:"+s);
          out.writeUTF(answer[i]);
          Thread.sleep(500);
        }
      }
      catch(Exception e) {
          System.out.println("客户已断开"+e);
        }
      }
    }

客户端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.io.*;
import java.net.*;
import java.sql.*;
public class Client_Main{
 public static void main (String args[ ]) {
  String [] mess={"请输入内容和时间,我会保存到'client_学号'表中","好的请保存"};
  Socket mysocket;
  DataInputStream in=null;
  DataOutputStream out=null;
  Connection con =null;
  Statement sta = null;
  ResultSet rs;
  String SQL;
  try {
      class.forName("org.apache.derby.jdbc.EmbeddedDriver");
       }
   catch(Exception e) { }
  try {
       String url = "jdbc:derby:client_学号;create=true";
       con=DriverManager.getConnection(uri);
       sta.execute(SQL);
      }
   SQL="insert into ip_学号 values"+
  try{  mysocket = new Socket("127.0.0.1",2010);
       in = new DataInputStream(mysocket.getInputStream());
       out = new DataOutputStream(mysocket.getOutputStream());
       for(int i=0;i<mess.length;i++) {
           out.writeUTF(mess[i]);
           String s =in.readUTF();
           System.out.println("客户收到服务器的回答:"+s);
           Thread.sleep(500);
        }
      }
      catch(Exception e) {
         System.out.println("服务器已断开"+e);
      }
     }
    }
  

  考试提交的结果是上面这个,参考的是书上那个客户与服务器互答,但是能力有限服务器端还有问题,参考了别的同学的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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);
        }*/
    }
 
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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);
        }*/
    }
 
}

  实现数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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考核到此结束了,也没有给个完整的结局,主要是自己没认真学,学的不精,导致后面搞不太懂。以后会努力复习一下。

posted @ 2018-12-13 15:35  葫芦娃的老九  阅读(123)  评论(0编辑  收藏  举报