2012-02-29 43 views
0

我正在與Java服務器進行通信。 在java中開發的一個應用程序,它在一些ip,port上運行。 例如192.168.1.1的端口9090 沒有Wi想用我的ASP.NET(C#)JAVA服務器和.Net客戶端編程

我有以下情形到服務器通信:

  1. 與服務器
  2. 一旦數據已經trasferred連接, 我必須通知服務器我的數據傳輸已完成。所以之後,服務器將處理數據並將恢復我(回覆)。
  3. 然後我將不得不閱讀這些數據。

當我使用NetworkStream類。

我有1個方法,我正在使用寫入發送數據。

但服務器不明白已收到或未收到完整的數據。 因此它不斷等待數據。

那麼如何做到這一點?

+1

如果你不給我們一些代碼來看看你是如何構建你的客戶端和服務器的,那麼不可能幫助你。 – Dervall 2012-02-29 12:54:49

回答

1

也許你可以考慮爲該通信使用Eneter Messaging Framework。
它是用於進程間通信的輕量級跨平臺框架。

Java服務代碼將是這個樣子:

// Declare your type of request message. 
public static class MyRequestMsg 
{ 
    public double Number1; 
    public double Number2; 
} 

// Declare your type of response message. 
public static class MyResponseMsg 
{ 
    public double Result; 
} 


public static void main(String[] args) throws Exception 
{ 
    // Create receiver that receives MyRequestMsg and 
    // responses MyResponseMsg 
    IDuplexTypedMessagesFactory aReceiverFactory = new DuplexTypedMessagesFactory(); 
    myReceiver = 
     aReceiverFactory.createDuplexTypedMessageReceiver(MyResponseMsg.class, MyRequestMsg.class); 

    // Subscribe to handle incoming messages. 
    myReceiver.messageReceived().subscribe(myOnMessageReceived); 

    // Create input channel listening to TCP. 
    IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory(); 
    IDuplexInputChannel anInputChannel = 
     aMessaging.createDuplexInputChannel("tcp://127.0.0.1:4502/"); 

    // Attach the input channel to the receiver and start the listening. 
    myReceiver.attachDuplexInputChannel(anInputChannel); 

    System.out.println("Java service is running. Press ENTER to stop."); 
    new BufferedReader(new InputStreamReader(System.in)).readLine(); 

    // Detach the duplex input channel and stop the listening. 
    // Note: it releases the thread listening to messages. 
    myReceiver.detachDuplexInputChannel(); 
} 

private static void onMessageReceived(Object sender, 
      TypedRequestReceivedEventArgs<MyRequestMsg> e) 
{ 
    // Get the request message. 
    MyRequest aRequest = e.getRequestMessage(); 

    ... process the request ... 

    // Response back the result. 
    MyResponseMsg aResponseMsg = new MyResponseMsg(); 
    ... set the result in the response message ... 

    try 
    { 
     // Send the response message. 
     myReceiver.sendResponseMessage(e.getResponseReceiverId(), aResponseMsg); 
    } 
    catch (Exception err) 
    { 
     EneterTrace.error("Sending the response message failed.", err); 
    } 
} 


// Handler used to subscribe for incoming messages. 
private static EventHandler<TypedRequestReceivedEventArgs<MyRequestMsg>> myOnMessageReceived 
     = new EventHandler<TypedRequestReceivedEventArgs<MyRequestMsg>>() 
{ 
    @Override 
    public void onEvent(Object sender, TypedRequestReceivedEventArgs<MyRequestMsg> e) 
    { 
     onMessageReceived(sender, e); 
    } 
}; 


和.NET客戶端將是這個樣子:

  public class MyRequestMsg 
    { 
     public double Number1 { get; set; } 
     public double Number2 { get; set; } 
    } 

    public class MyResponseMsg 
    { 
     public double Result { get; set; } 
    } 


    private IDuplexTypedMessageSender<MyResponseMsg, MyRequestMsg> myMessageSender; 


    private void OpenConnection() 
    { 
     // Create message sender. 
     // It sends string and as a response receives also string. 
     IDuplexTypedMessagesFactory aTypedMessagesFactory = new DuplexTypedMessagesFactory(); 
     myMessageSender = 
      aTypedMessagesFactory.CreateDuplexTypedMessageSender<MyResponseMsg, MyRequestMsg>(); 

     // Subscribe to receive response messages. 
     myMessageSender.ResponseReceived += OnResponseReceived; 

     // Create TCP messaging. 
     IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory(); 
     IDuplexOutputChannel anOutputChannel = 
      aMessaging.CreateDuplexOutputChannel("tcp://127.0.0.1:4502/"); 

     // Attach the output channel to the message sender and be able 
     // send messages and receive responses. 
     myMessageSender.AttachDuplexOutputChannel(anOutputChannel); 
    } 

    private void CloseConnection(object sender, FormClosedEventArgs e) 
    { 
     // Detach output channel and stop listening to response messages. 
     myMessageSender.DetachDuplexOutputChannel(); 
    } 

    private void SendMessage() 
    { 
     // Create message. 
     MyRequestMsg aRequestMessage = new MyRequestMsg(); 
     ... 

     // Send message. 
     myMessageSender.SendRequestMessage(aRequestMessage); 
    } 

    private void OnResponseReceived(object sender, 
           TypedResponseReceivedEventArgs<MyResponseMsg> e) 
    { 
     // Get the response message. 
     MyResponseMsg aResponse = e.ResponseMessage; 

     .... process the response from your Java client .... 
    } 


更多的技術信息,你可以找到here
Java的在線幫助爲here,.NET的在線幫助爲here