Server / client String []数组通过Socket连接

时间:2012-06-13 16:54:11

标签: java database sockets

我正在尝试通过打开的套接字连接发送String [],但它不起作用。 我从数据库发送数组并在套接字连接后在客户端发送结果集!

服务器

public class ServerConnectionHandler implements Runnable {

private static String String;
private Socket clientSocket;

public ServerConnectionHandler(Socket clientSocket) {
    this.clientSocket = clientSocket;
}

public String[] toStringArray(ResultSet resultSet, String columnLabel) {
    LinkedList<String> resultList = new LinkedList<String>();

    try {
        while (resultSet.next()) {
            resultList.add(resultSet.getString(columnLabel));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return resultList.toArray(new String[0]);
}

@Override
public void run() {




    ResultSet val = null;
    System.out.println("Client " + clientSocket.getInetAddress() + ":" + clientSocket.getPort() + " has connected.");
    Mysql conn = new Mysql("****", "root", "","*****");
    val = conn.executeSelect(CONSTANT.QUERY_ALL);
    String[] mybytearray = toStringArray(val,"real_url");

    try {
        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        String inputline, outputline;
        outputline = "Connected to server...type in the console to interact!";
        out.println(outputline);


        while((inputline = in.readLine()) != null){
            outputline = "Server echoes: " + mybytearray;
            out.println(outputline);
        }
    }

    catch(Exception e) {
        System.out.println("Client " + clientSocket.getInetAddress() + ":" + clientSocket.getPort() + " has disconnected.");
    }

}

}

客户端

public class ClientMain {

private String host;
private int port;

public ClientMain(String host, int port) {
    this.host = host;
    this.port = port;
}

public void start() {
    try {
        Socket clientSocket = new Socket(host, port);
        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        String inputline, outputline;
            while((inputline = in.readLine()) != null){
                System.out.println(inputline);
                outputline = getUserInput();
                out.println(outputline);

            }
    } 

    catch (Exception e) {
        System.out.println("Server not running, shutting down...");
        System.exit(-1);
    } 
}

private static String getUserInput() {
    String s = "";
    InputStreamReader isr; 
    BufferedReader br;
    try {
        isr = new InputStreamReader(System.in);
        br = new BufferedReader(isr);
        s += br.readLine();

    }
    catch(IOException e){
        e.printStackTrace();
        System.exit(-1);
    }

    return s;
}

}

1 个答案:

答案 0 :(得分:0)

除了整体有缺陷的设计外,我很确定这条线路是非常错误的:

outputline = "Server echoes: " + mybytearray;

您将字符串“Server echoes:”与数组连接起来。你不能这样做。

相反,试试这个:

outputline = "Server echoes: ";
for (String s : mybytearray) {
    outputline += s;
}

但是为什么你要调用String mybytearray数组?

作为职业建议,请考虑在每个现代Java IDE中使用调试器工具。