JAVA服务器和.Net客户端编程

标签 java .net sockets network-programming tcpclient

我正在与 Java 服务器进行通信。 一个用java开发的应用程序,它运行在某个ip、端口上。 例如192.168.1.1 端口 9090 没有人想使用我的 ASp .NET (C#) 与该服务器通信

我有以下情况:

  1. 与服务器的连接
  2. 数据传输完毕后,我必须通知服务器我的数据传输已完成。之后服务器将处理数据并回复我(响应)。
  3. 然后我将不得不读取该数据。

当我使用 NetworkStream 类时。

我有 1 个正在使用的方法是 write 来发送数据。

但服务器不知道是否已收到完整的数据。 所以它不断地等待数据。

那么如何做到这一点呢?

最佳答案

也许您可以考虑使用 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 ....
    }

关于JAVA服务器和.Net客户端编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9497990/

相关文章:

java - HHH10001002 : Using Hibernate built-in connection pool (not for production use!)

c# - 我如何知道 WindowsFormsHost 何时在 WPF 中调整大小?

c# - ICSharpCode.SharpZipLib.Zip.FastZip 不压缩文件名中包含特殊字符的文件

java - 线程中的异常 "main"java.net.SocketException : Connection reset at java. io.BufferedReader.readLine(来源未知)

node.js - Mongod 未运行.. E 网络 [initandlisten] Listen() : bind() failed errno:98 Address already in use for socket: 0. 0.0.0:27017

java - 实时向 JFrame 添加多个 JPanel

java列表创建方法区别

Java在索引文件中存储多个图像、配置文件等?

c# - ReRegisterForFinalize SuppressFinalize 现实生活中的例子

android - AsyncTask 不立即返回结果