[英]Java loop to check for inputstream reader
我有一個服務器和client.java,它們可以通過輸入和輸出流同時發送和接受消息,但是一旦建立連接,就發送了一條消息,然后服務器關閉了自身,到目前為止,我已經將緩沖的讀取器放入等等。 在finally塊中,該塊僅在e == true時才激活(e表示退出,因此我可以在需要時停止它),但是在發送/接收消息后它仍然停止構建第一個問題我如何才能阻止它關閉自身? 第二個問題,如何將輸入流閱讀器放入循環中,以不斷測試來自client.java的輸入?
package client;
import java.io.*;
import java.net.*;
import java.io.BufferedReader;
// Variables declaration - do not modify
/**
*
* @author ****
*/
public class Client extends javax.swing.JFrame {
;
public Client() {
initComponents();
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextField1.setText("");
input = jTextField1.getText();
send(input);
}
int port = 1234;
String hostname = "localhost";
String input,output;
public void send(String text) {
try {
Socket skt = new Socket(hostname, port); /*Connects to server*/
BufferedReader in = new BufferedReader(new
InputStreamReader(skt.getInputStream())); /*Reads from server*/
System.out.println("Server:" + in.readLine());
PrintWriter out = new PrintWriter(skt.getOutputStream(), true);
out.println(text); /*Writes to server*/
out.close(); /*Closes all*/
in.close();
skt.close();
}
catch(Exception e) {
System.out.print("Error Connecting to Server\n");
}
}
public void startUP() {
}
public static void main(String args[]) {
Client c = new Client();
c.startUP();
c.send("Server is online");
new Client().setVisible(true);
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
package server;
import java.io.*;
import java.net.*;
class Server {
/*
To send string to client use "out.print(data)"
To use info sent from client use "in.readLine()"
*/
int port = 1234;
String input,output;
boolean e = false;
String question = "how are you";
String answer = "i am fine";
public void send(String text) {
ServerSocket srvr = null;
Socket skt = null;
PrintWriter out = null;
BufferedReader in = null;
try {
srvr = new ServerSocket(port);
skt = srvr.accept(); /*Waiting for Connection from client*/
System.out.println("Connection = true");
out = new PrintWriter(skt.getOutputStream(), true);
out.println(text); /*Write/Send to Client*/
in = new BufferedReader(new
InputStreamReader(skt.getInputStream())); /*Read from Client*/
System.out.println("Client:" + in.readLine());
input = in.readLine();
} catch( Exception e) {
System.out.println("Error Connecting\n");
} finally {
if(e == true){
try {
out.close();
in.close();
skt.close(); /*Closes all*/
srvr.close();
} catch (IOException e) {
}
}
}
}
public void testFor() {
if(input.equals(question)){ send(answer); }
}
public static void main(String args[]) {
Server s = new Server();
s.send("Client is online"); //sends a message to client
// s.testFor();
}
}
好的開始,要停止一個while循環,在某些時候您必須說該語句是false還是true(取決於您首先使用的是什么)。 所以,如果你說while(true)
。 這將永遠是正確的,因為沒有什么可比的,因此它將創建無限循環並永遠持續下去。 但是,如果您說我們這樣做
`while(x=true)'
{
x=false;
}
這將確保您停止while循環,並且不會再次輸入它。 怎么樣? 好吧,我們在x為真的條件下進入while循環。 進入該循環后,我們便宣布x為假。 因此,在下一次迭代中,x將為false,結果我們將不會進入while循環。 金田直截了當不是嗎?
至於問題本身,我建議閱讀這篇文章。 這是非常清楚且布局合理的,它將向您展示如何編寫基於套接字的良好程序:)
第一個問題是從方法中調用bufferreader,printwriter等。然后在單獨的方法中使用.close,該方法可以隨時激活。
對於第二個問題,我發現可以使用while循環:
while(true) {
//code...
}
謝謝沒有人的幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.