開いているソケット接続で 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;
}
}