2011-11-16 22 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();”的好处是什么? –

相关问题