我在Java中使用这个简单的服务器客户端程序时遇到了问题。我的目标是实现一个从客户端获取String数组的服务器。服务器必须按字母顺序对这些单词进行排序,然后将其发送回客户端。最后,命令字将打印在控制台中。
问题是我启动服务器,并且启动客户端后控制台上没有任何内容。错误在哪里?
public class Server {
public static void main(String[] args) {
Socket s = null;
ServerSocket server = null;
BufferedWriter out = null;
BufferedReader in = null;
System.out.println("Server startet.......");
try {
server = new ServerSocket(6666);
s = server.accept();
out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
String text = in.readLine();
String array[] = text.split(";");
Arrays.sort(array);
String answer = "";
for (int i = 0; i < array.length; i++) {
answer = "" + array[i] + ";";
}
out.write(answer);
out.flush();
} catch (IOException e) {
e.getMessage();
} finally {
try {
out.close();
} catch (IOException e) {
e.getMessage();
}
try {
server.close();
} catch (IOException e) {
e.getMessage();
}
try {
in.close();
} catch (IOException e) {
e.getMessage();
}
}
}
}
public class Client {
public static void main(String[] args) {
String array[] = { "Hallo", "Bir", "Deme", "ABC" };
question(array);
}
public static void question(String array[]) {
Socket s = null;
BufferedWriter out = null;
BufferedReader in = null;
try {
s = new Socket("localhost", 6666);
out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
String text = "";
for (int i = 0; i < array.length; i++) {
text += array[i] + ";";
}
out.write(text);
out.flush();
String answer = in.readLine();
System.out.println(answer);
} catch (IOException e) {
e.getMessage();
} finally {
try {
in.close();
} catch (IOException e) {
e.getMessage();
}
try {
out.close();
} catch (IOException e) {
e.getMessage();
}
}
}
}