Java服务器应用程序来模拟Mysql

时间:2014-11-01 21:30:29

标签: java mysql sockets jdbc wireshark

我有一个客户端Java应用程序,它通过JDBC连接到MySql服务器。我想用原型Java服务器应用程序替换MySql。此应用程序应通过套接字接收和发送字节回客户端,以便客户端“认为”它与Mysql通信。 我已完成Wireshark捕获并获得十六进制值。现在我想模拟,从服务器向客户端发送相同的服务器问候字节,以便客户端开始向服务器发送sql命令。 当Mysql响应Java客户端时,这是Wireshark的捕获。这是Mysql Greeting服务器响应:

0000   00 19 99 5d 6f 53 00 12 00 85 4c ce 08 00 45 00  ...]oS....L...E.
0010   00 60 3e 49 40 00 3f 06 e8 84 8d 1d 7d 35 8d 1d  .`>I@.?.....}5..
0020   7d 5a 0c ea ef dc 14 56 67 8e 17 b8 70 a8 50 18  }Z.....Vg...p.P.
0030   00 73 af 71 00 00 34 00 00 00 0a 35 2e 31 2e 36  .s.q..4....5.1.6
0040   31 00 74 00 00 00 4a 29 2e 4b 6d 66 38 22 00 ff  1.t...J).Kmf8"..
0050   f7 08 02 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0060   00 3f 56 72 5c 58 23 44 6d 76 2c 73 22 00        .?Vr\X#Dmv,s".

这是另一个Wireshark捕获,当我的Java服务器替换Mysql时。我的Java服务器应用程序模拟Mysql Server Greeting消息,但由于某种原因它发送到很多字节:

0000   00 19 99 5d 6f 53 00 12 00 85 4c ce 08 00 45 00  ...]oS....L...E.
0010   00 96 8d 8d 40 00 3f 06 99 0a 8d 1d 7d 35 8d 1d  ....@.?.....}5..
0020   7d 5a 0c ea fd 4b a8 2d 3f 6d 96 b3 42 b3 50 18  }Z...K.-?m..B.P.
0030   00 73 85 d7 00 00 00 19 99 5d 6f 53 00 12 00 85  .s.......]oS....
0040   4c ce 08 00 45 00 00 60 3e 49 40 00 3f 06 e8 84  L...E..`>I@.?...
0050   8d 1d 7d 35 8d 1d 7d 5a 0c ea ef dc 14 56 67 8e  ..}5..}Z.....Vg.
0060   17 b8 70 a8 50 18 00 73 af 71 00 00 34 00 00 00  ..p.P..s.q..4...
0070   0a 35 2e 31 2e 36 31 00 74 00 00 00 4a 29 2e 4b  .5.1.61.t...J).K
0080   6d 66 38 22 00 ff f7 08 02 00 00 00 00 00 00 00  mf8"............
0090   00 00 00 00 00 00 00 3f 56 72 5c 58 23 44 6d 76  .......?Vr\X#Dmv
00a0   2c 73 22 00                                      ,s".

如何通过套接字将从Java服务器上使用Wireshark捕获的相同字节发送到Java客户端? 这是我在服务器端使用的Java代码:

import java.io.*;


public class Server { 

    /**
     * 
     */
    public void run() {
    try {
        int serverPort = 3306;
        ServerSocket serverSocket = new ServerSocket(serverPort);
        serverSocket.setSoTimeout(10000); 
        while(true) {
            System.out.println("Waiting for client on port " + serverSocket.getLocalPort() + "..."); 

            Socket server = serverSocket.accept();

            System.out.println("Just connected to " + server.getRemoteSocketAddress()); 


            byte[] clientMessage = hexStringToByteArray("0019995d6f53001200854cce0800450000603e4940003f06e8848d1d7d358d1d7d5a0ceaefdc1456678e17b870a850180073af710000340000000a352e312e363100740000004a292e4b6d66382200fff7080200000000000000000000000000003f56725c5823446d762c732200");


            BufferedOutputStream bos = new BufferedOutputStream(server.getOutputStream());
            bos.write(clientMessage);   
            bos.flush();

            System.out.println("Data sent back");   

        }
    }
    catch(UnknownHostException ex) {
        ex.printStackTrace();
    }
    catch(IOException e){
        e.printStackTrace();
    }
  }

  private byte[] hexStringToByteArray(String string) {
        int len = string.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(string.charAt(i), 16) << 4)
                                 + Character.digit(string.charAt(i+1), 16));
        }
        return data;
    }

public static void main(String[] args) {
        Server srv = new Server();
        srv.run();
  }
}

0 个答案:

没有答案