對不起,我現在編碼12小時,現在我有一個「brainlag」。Java簡單客戶端服務器應用程序
我做了一個小小的Client Server程序。
客戶:
public void send(String send) {
DataOutputStream out;
Socket client;
try {
client = new Socket("192.168.0.138", port);
out = new DataOutputStream(client.getOutputStream());
out.writeChars(send + '\n');
Thread.sleep(100L);
} catch (IOException ex) {
System.err.println("Can't connect to Server!");
} catch (InterruptedException ex) {
System.err.println("Cant sleep!");
}
}
服務器:
public static void main(String[] args) throws IOException {
int port = 5000;
String cIn;
System.out.println("Running on Port 5000");
ServerSocket sock = new ServerSocket(port);
Socket client;
BufferedReader inFromClient;
while (true) {
client = sock.accept();
inFromClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
cIn = inFromClient.readLine();
System.out.println("" + cln);
}
}
現在我的問題。我如何使我的字符串(數據)在輸入新數據時以循環方式發送到服務器。
如果一個正常的循環,我的字符串永久發送到服務器。如果我改變我的字符串,這並不重要。 我會讓它,如果我改變我的字符串,新的字符串發送到服務器。
對不起我的英語不好。我希望你能理解。