2011-11-16 26 views
1

我想在同一個服務器套接字(Java應用程序)上讀寫(從服務器到客戶端隨機)。我的客戶端到服務器寫入和讀取循環中工作正常。在響應正確寫入的服務器上。全雙工服務器套接字實現,單獨的讀寫線程?

但是,如果我想在服務器隨機寫一些命令。我沒有解決方案,首先我的問題是:

  1. 是否有可能在服務器端將命令寫入客戶端ramdonly在同一套接字上?
  2. 如果可能,任何建議或指針如何做到這一點?
  3. 請給我一些指針,在那裏我可以閱讀有關這種情況的材料?

在此先感謝。

public class ContentServerSocket extends ServerSocket { 
    private final static int PORT = 4444; 

    protected static boolean XYZGONE = false; 
    public static Content content; 

    public ContentServerSocket(xyzService service) throws IOException { 
     super(PORT); 

     while (true) { 

      Log.d(TAG, "Waiting for new request from client(content) ...."); 
      new HandleRequest(accept(), service).start(); 
     } 
    } 

    public static void xyzRunAway() { 
     Log.d(TAG," Content Serv er 1 "); 
     XYZGONE = true; 
    } 

} 

class HandleRequest extends Thread { 
    private final static String TAG = "ContentServerSocket:Thread for a request:"; 
    private Socket client; 
    private xyzService service; 

    private static Context context; 

    HandleRequest(Socket client, SuggestionService service) { 
     this.client = client; 
     this.service = service; 
     context = xyzService.serviceContext(); 
    } 

    public void run() { 
     while (true) { 
      try { 

       Log.d(TAG, " Step 1: client: Received request MSG for Check... "); 
       PrintWriter out = new PrintWriter(client.getOutputStream(), 
         true); 

       BufferedReader in = new BufferedReader(new InputStreamReader(
         client.getInputStream(), "utf-8")); 
       String request = ""; 
       String tmpLine = null; 


       Log.d(TAG, " Step Xyz waiting data from the client ... "); 


       while ((tmpLine = in.readLine()) != null) { 

        if (tmpLine.length() > 0) { 
         request += tmpLine; 
         //if (tmpLine.toLowerCase().contains("</contentInfo>")) { 
         if (tmpLine.contains("</contentInfo>")) { 
          Log.d(TAG, " Server : broke because of </contentInfo>"); 
          break; 
         } 
        } else { 
         Log.d(TAG, " Step NULL : "); 
         request = ""; 
        } 

       } 



       Log.d("Robin", " Step 2: Actual request received from the client : : " + request); 
       if (request.length() == 0) { 
        Log.d("Robin", 
          " client got 0 length request, thread stop!"); 
        throw new Exception(); 

       } 
       //XMLParser xmlParser = new XMLParser(new ByteArrayInputStream(
       //  request.getBytes("UTF-8"))); 

       Log.d(TAG, " Step 3 : "); 
       RequestParser readxmlrequest = new RequestParser(request); 
       String requestType = readxmlrequest.parsingXmlRequestFromContent(); 
       Log.d(TAG, " Step 4 requestType : " + requestType); 


       //TODO : need to get the result and pas to the out.println.. 

       //String result = processXML(xmlParser); 

       String result = responseToContentRequest(readxmlrequest,requestType);//null; //TODO need to complete. 
       Log.d(TAG, " Step 5 result : "+result); 
       (((((((((())))))))))"; 
       if (result != null && result.length() > 0) { 

        //oos.writeObject(result); 
        Log.d("Robin", " Writing response to socket ... "); 
        out.println(result + "\n"); 
        out.flush(); 
        Log.d("Robin", " Writing flush completed "); 
       } 

       if(ContentServerSocket.XYZGONE) { 
        Log.d(TAG," XYZGONE >>>>>>>> "); 
        ContentServerSocket.XYZGONE = false; 
        String tmp = "<ssr> OK Done .......</ssr>"; 
        out.println(tmp + "\n"); 
        Log.d("Content Server Socket ", "xyz:" + tmp); 
        out.flush(); 
       } 

      } catch (IOException ioException) { 
       Log.d("Robin", " IOException on socket listen: " + ioException); 
      } 
      catch (Exception e) { 
       Log.d("Robin", " outer exception: " + e.toString()); 
       break; 
      } 

      finally { 
       if (client == null || client.isClosed() 
         || !client.isConnected()) { 
        Log.d(" Robin ", " client is null"); 
        break; 
       } 
      } 
      //break; 

      } 
     Log.d("Robin", " thread stop... "); 
    } 
+0

你能展示一些代碼來演示你現有的工作嗎? – Gray

+0

嗨,格雷,我擺了我的代碼部分,通過編輯我的問題。請檢查並給我您的反饋。 – RobinSingh

回答

0

是的,可以將數據從服務器上或客戶端上的多個線程寫入現有套接字。但是,您必須確保請求不會重疊,並且接收方實際上知道由誰寫的內容。

如果使用基於行的協議,則可以將每條消息定義爲單行。在這種情況下,您應該以某種方式同步多個線程,只有一個線程在任何給定時刻寫入該線的一部分。

您的代碼有點太大,無法理解您的問題在哪裏,對不起。

也許本教程有幫助嗎?有相當多的在那裏:

http://www.javaworld.com/javaworld/jw-12-1996/jw-12-sockets.html

2

所以,我固定它。我只需要維護兩個不同的線程。 1)閱讀。 2)寫。

在上面的代碼中,我剛剛開始寫一個多線程。

將代碼插入上面代碼的Run函數中。

============================================== ======

Runnable r1 = new Runnable() { 
    public void run() { 
      try { 
      while (true) { 
       System.out.println("Hello, world!"); 
       if(ContentServerSocket.XYZGONE) { 
        Log.d(TAG," XYZGONEY >>>>>>>> "); 
        ContentServerSocket.XYZGONE = false; 
        String tmp = "<ssr> OK Done .......</ssr>"; 
        out.println(tmp + "\n"); 
        Log.d("Content Server Socket ", "XYZGONE :" + tmp); 
        out.flush(); 
       } 
       Thread.sleep(1000L); 
      } 
     } catch (InterruptedException iex) {} 
    } 
}; 

Thread thr1 = new Thread(r1); 

==================================

然後在讀取的wile循環中啓動線程。 以下面的代碼與檢查。

====================================

if(!thr1.isAlive())thr1.start(); 

謝謝大家,誰迴應我的問題..

+0

「if(!thr1.isAlive())thr1.start();」的好處是什麼? –

相關問題