Java服务器仅接收一条消息

时间:2018-08-25 09:48:25

标签: java sockets server socket.io serversocket

我已经搜索了互联网,并从面临相同问题的人们那里发现了一些问题,但是解决方案对我不起作用,现在我的问题是我制作了一个Java服务器,该服务器应该从具有多个类的客户端接收消息,但是它从每个班级只收到一条消息,然后从同一班级没有收到任何消息,当我从其他班级发送任何消息时也是如此

代码如下:

package Database;

import Clinic.QueriesController;
import Clinic.ReciptionController;
import javafx.application.Platform;
import java.io.*;
import java.net.Socket;
import java.net.ServerSocket;
import java.net.SocketException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Networking {

private ServerSocket ss;

private QueriesController qc;
private DBConnection db;

{
    try {
        qc = new QueriesController();
        db = new DBConnection();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

// getCon() is a method I declared in the class DBConnection to simply get connection to the data base
private Connection con = db.getCon();

public void networkConnection() {

    new Thread(() -> {

        try {
            ss = new ServerSocket(16000);
            Platform.runLater(() ->
                    System.out.println("Server started"));

            while (true){
                // the following method is declared below
                handleData();
            }

        } catch(SocketException e){
            System.out.println("Client disconnected");
        } catch(Exception e){
            e.printStackTrace();
        }

    }).start();

}

public void handleData() throws IOException{

        Socket s = ss.accept();

        BufferedReader inputFromClient = new BufferedReader(new InputStreamReader(s.getInputStream()));
        PrintWriter outputToClient = new PrintWriter(s.getOutputStream(), true);

        // messages received from the client contain the command and the information of the message separated by ";"
        // and the content of the message are separated by "," and every thing gets split in the following code
        String messageFromClient = inputFromClient.readLine(); // receive the message

        //check the command gained from client
        System.out.println("Received Code: " + messageFromClient);

        if (messageFromClient != null) {

            // first split to get the command
            String[] commandInfo = messageFromClient.split(";");

            String[] messageContent;
            PreparedStatement ps2;
            ResultSet rs;

            PreparedStatement ps;

//the switch statement takes the first part of the message received and each case splits the second part of the message based on the ","
            switch (commandInfo[0]) {

                case "insertMS":

                    messageContent = commandInfo[1].split(",");

                    try {
                        ps = con.prepareStatement("insert into `patiantInfo` values(null,?,?,?,?,?);");
                        ps.setString(1, messageContent[0]);
                        ps.setString(2, messageContent[1]);
                        ps.setString(3, messageContent[2]);
                        ps.setString(4, messageContent[3]);
                        ps.setString(5, messageContent[4]);

                        ps.executeUpdate();

                    } catch (SQLException e) {
                        e.printStackTrace();
                    }

                    break;

                case "deleteMS":

                    try {
                        ps = con.prepareStatement("delete from patiantInfo where id = ?");
                        ps.setString(1, commandInfo[1]);

                        qc.ps = con.prepareStatement("delete from visitInfo where patiantID = ?");
                        qc.ps.setString(1, commandInfo[1]);

                        ps.executeUpdate();
                        qc.ps.executeUpdate();

                    } catch (SQLException e) {
                        e.printStackTrace();
                    }

                    break;

                case "editMS":

                    try {

                        messageContent = commandInfo[1].split(",");

                        ps = con.prepareStatement("update patiantInfo set name = ?," +
                                " age = ?," +
                                " sex = ?," +
                                " address = ?," +
                                " connect = ?" +
                                " where id = ?");
                        ps.setString(1, messageContent[0]);
                        ps.setString(2, messageContent[1]);
                        ps.setString(3, messageContent[2]);
                        ps.setString(4, messageContent[3]);
                        ps.setString(5, messageContent[4]);
                        ps.setString(6, messageContent[5]);

                        ps.executeUpdate();

                    } catch (SQLException e) {
                        e.printStackTrace();
                    }

                    break;

                case "insertMSF1":

                    try {

                        messageContent = commandInfo[1].split(",");

                        qc.ps = qc.con.prepareStatement("insert into visitInfo values (null,?,?,?,?,null,null,null,null,null,null,null) ");
                        qc.ps.setString(1, messageContent[0]);
                        qc.ps.setString(2, messageContent[1]);
                        qc.ps.setString(3, messageContent[2]);
                        qc.ps.setString(4, messageContent[3]);
                        qc.ps.executeUpdate();

                        ps2 = con.prepareStatement("select num from visitCount");
                        rs = ps2.executeQuery();

                        int num;
                        while (rs.next()) {
                        num = rs.getInt("num");
                        num++;
                        ps = con.prepareStatement("update visitCount set num = ?");
                        ps.setString(1, String.valueOf(num));
                        ps.executeUpdate();
                    }

                } catch (SQLException e) {
                    e.printStackTrace();
                }

                break;

            case "updateMSF2":

                try {

                    messageContent = commandInfo[1].split(",");

                    qc.ps = qc.con.prepareStatement("update visitInfo set attend_date = ?," +
                            " attend_time = ?," +
                            " visit_type = ?," +
                            " attend = ?," +
                            " attend_type = ?" +
                            " where patiantID = ? and id = ? ");

                    ps2 = con.prepareStatement("select num from visitCount");
                    rs = ps2.executeQuery();

                    qc.ps.setString(1, messageContent[0]);
                    qc.ps.setString(2, messageContent[1]);
                    qc.ps.setString(3, messageContent[2]);
                    qc.ps.setString(4, messageContent[3]);
                    qc.ps.setString(5, messageContent[4]);
                    qc.ps.setString(6, messageContent[5]);
                    while (rs.next()) {
                        qc.ps.setString(7, rs.getString("num"));
                    }

                    qc.ps.executeUpdate();

                } catch (SQLException e) {
                    e.printStackTrace();
                }

                break;

            case "updateMSF3":

                try {

                    messageContent = commandInfo[1].split(",");

                    qc.ps = qc.con.prepareStatement("update visitInfo set payment_date = ?," +
                            " payment_value = ?" +
                            " where patiantID = ? and id = ? ");

                    ps2 = con.prepareStatement("select num from visitCount");
                    rs = ps2.executeQuery();

                    qc.ps.setString(1, messageContent[0]);
                    qc.ps.setString(2, messageContent[1]);
                    qc.ps.setString(3, messageContent[2]);
                    while (rs.next()) {
                        qc.ps.setString(4, rs.getString("num"));
                    }

                    qc.ps.executeUpdate();

                } catch (SQLException e) {
                    e.printStackTrace();
                }

                break;

            case "deleteES":

                try {

                    qc.ps = con.prepareStatement("delete from visitInfo where id = ? ");
                    qc.ps.setString(1, commandInfo[1]);

                    qc.ps.executeUpdate();

                } catch (SQLException e) {
                    e.printStackTrace();
                }

                break;

        }
    }
}
}

我确定它不会收到消息,因为它不会打印消息的内容,并且不会进入switch语句内

0 个答案:

没有答案