第四次考核

1.码云地址:https://gitee.com/ztsxxny/codes

2.服务器端:

import java.util.*;

import java.io.*;

import java.net.*;

public class Server_Main

{

    public static void main(String args[])

    {

    Scanner scanner = new Scanner(System.in);

    ServerSocket server = null;

    ServerThread thread;

    Socket kehu = null;

    while (true){

    try {

    server = new ServerSocket(2010);

    }

    catch(IOException e1){

        System.out.println("正在监听");

    }

    try {

        System.out.println("等待呼叫");

        kehu = server.accept();

    }

    catch (Exception e)

    {

        System.out.println("正在等待客户"+e);

    }

    if (kehu !=null){

    new ServerThread(kehu).start(); }

   }

  }

}

class ServerThread extends Thread{

    Scanner scanner = new Scanner(System.in);

    Socket socket;

    DataOutputStream out=null;

    DataInputStream in=null;

    String s = null;

    ServerThread(Socket t)

    {

        socket = t;

        try

        {

            out = new DataOutputStream(socket.getOutputStream());

            in = new DataInputStream(socket.getInputStream());

            for(int i=0;i<10;i++)

        {

            String s=in.readUTF(); 

            System.out.println("客户来话:"+s);

            out.writeUTF(scanner.nextLine());

         

        }

        }

        catch (IOException e){}

    }

     

}

3.客户端:

import java.io.*;

import java.net.*;

import java.util.*;

public class Client_Main

{

    public static void main (String args[])

    {

        Socket mysocket;

        DataInputStream in=null;

        DataOutputStream out=null;

        try

        {

            mysocket = new Socket("127.0.0.1",2010);

            in = new DataInputStream(mysocket.getInputStream());

            out = new DataOutputStream(mysocket.getOutputStream());

            for (int i=0;i<10;i++)

            {

                Scanner scanner = new Scanner(System.in);    

                String source = scanner.nextLine();

                out.writeUTF(source);

                String s= in.readUTF();

                System.out.println("收到服务器的回答:"+s);

                Thread.sleep(500);

            }

        }

        catch(Exception e)

        {

            System.out.println("服务器已断开"+e);

        }

    }

}

4.数据库:

Connection con=null;

Statement sql;

ResultSet rs;

try{ Class.forName("com.mysql.kdbc.Driver");

}

catch(Exception e){}

String uri="jdbc:mysql://localhost:3306/client?useSSL=true";

String user="root";

String password="111111";

try{con = DriverManager.getConnection(uri,user,password);}

catch(SQLException e){}

String jilu="";

String sqlStr = ""+jilu;

try{

sql=con.createStatement();

rs=sql.executeQuery("SELECT bir FROM mess WHERE Number=1001");

while(rs.next()){

String number=rs.getString(1);

//String name=rs.getString(2);

//Date date=rs.getDate(3);

//float height=rs.getFloat(4);

System.out.print(number);

//System.out.print(name);

//System.out.print(date);

//System.out.print(height);

}

con.close();

 

}

catch(SQLException e){

System.out.println(e);

}

5.解题思路:参考书上的世界杯通信,经过改编,然而还是有问题。

6.遇到的问题:本次考核是将通信与数据库结合到一起,单个的例子书上都有,但是并没有解决过合在一起的,经过很多实验还是没有成功。

7总结:不能够灵活的运用所学到的知识,对知识的理解有欠缺,缺乏练习,今后会多加练习,尽量的做到对知识的理解。

学习内容 代码行数 博客字数
输入输出流  150行 ---
多线程机制 100行 ---
数据库 40 ---
总计 300 150

 

posted @ 2018-12-15 19:09  邹彤  阅读(159)  评论(0编辑  收藏  举报