2015-11-25 84 views
1

對不起,我現在編碼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); 
    } 
} 

現在我的問題。我如何使我的字符串(數據)在輸入新數據時以循環方式發送到服務器。

如果一個正常的循環,我的字符串永久發送到服務器。如果我改變我的字符串,這並不重要。 我會讓它,如果我改變我的字符串,新的字符串發送到服務器。

對不起我的英語不好。我希望你能理解。

回答

0

如何用一個新的線程發送數據,這些線程在循環中發送數據。當你輸入一些新的數據插入舊的線程,並開始一個新的線程,等等?

0

如果我繼續: - 您需要一個控制檯應用程序(或窗口/ awt/swing ...)或主客戶端應用程序,它需要一個字符串,並且有時會更改此字符串。 - 此字符串必須由你的函數發送「發送」,連續,與最後一個字符串

我建議你: - (1秒,2秒,x秒)

1修正環

2 - 使用共享變量(在關鍵部分或同步),您的主客戶端應用程序將其寫入,並在需要時進行更改,並且您的「發送」函數每隔x秒讀取一次併發送它。

您的客戶端可能看起來像:

// SHARED VARIABLE 
static String warning=""; 
final static Object warning_sync=new Object(); 

// Alert function 
class Thread_alert extends Thread 
    { 
    // YOUR CODE 
    public void send(String send) { 
     DataOutputStream out; 
     Socket client; 
     int port=80; 

      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 Thread_alert() 
     { 
     super(); 
     } 

    public void run() 
     { 
     while (true) 
      { 
      // WHAT YOU HAVE TO DO 

      synchronized(warning_sync) 
       { 
       System.err.println("WARN: "+warning); 
       send(warning); 
       } 

      // Sleep 5 seconds 
      try { 
       Thread.sleep(5000); 
       } 
       catch (InterruptedException e) 
        { 
        e.printStackTrace(); 
        } 
      } 
      // while (true) 
     } 
     // public void run() 
} 
// class Thread_alert 



public void console_client() 
{ 
// START THE THREAD 
Thread_alert lethread=new Thread_alert(); 
lethread.start(); 

// INPUT LOOP 
Scanner s = new Scanner(System.in); 
String line; 

while ((line=s.nextLine())!=null) 
    { 
    System.out.println("STRING:'"+line+"'"); 

    // Fix the warning 
    synchronized(warning_sync) 
     { 
     warning=line; 
     } 

    // bonus 
    // IF STOP: STOP 
    if (warning.equals("STOP")) 
     { 
     lethread.stop(); 
     break; 
     } 
    } 
    // while ((line=s.nextLine())!=null) 

// safe 
s.close(); 
} 
0

我認爲你需要,收於while循環的末尾添加一個BufferedReader。

while (true) { 
    client = sock.accept(); 
    inFromClient = new BufferedReader(new InputStreamReader(client.getInputStream())); 
    cIn = inFromClient.readLine(); 
    System.out.println("" + cln); 
    inFromClient.close() //add this 
} 
相關問題