套接字:将字符串从 Java 发送到 C#?
我需要将字符串消息从 Java 程序实时发送到 C# 程序。 互联网上有很多例子,但你找不到任何适合我的目的的东西,即(可能)Java 客户端(套接字代码)和 C# 服务器(套接字代码)。 谢谢。
I need to send string messages from Java program to C# program in real time.
There are many examples in the Internet but U can't find anything good for my purpose that is (probably) Java client (sockets code) and c# server (sockets code).
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,我已经在我的一个项目中做到了这一点,所以这里是:
免责声明:一些代码(实际上只有一点点)是基于nakov聊天服务器的。
另请注意,我对以 UTF-8 发送和接收的所有消息进行解码和编码。
Java 代码:
Class: Server
}
Class Message Dispatcher:
Class: ClientInfo
Class ClientListner:
Class:ClientSender
好的是java代码,现在是c#代码
c#代码:
使用的变量:
函数:Intelize连接:
函数:ReciveMessages
函数: proceesMessage:
函数sendMessage:
这就是java服务器和c#客户端之间进行通信所需的全部内容。如果您有任何疑问,请随时在此发布。
Ok i already did this in one of my projects so here it is:
disclaimer: some of the code (only a little bit actually) is based on nakov chat server.
also note that i decode and encode all the messages sent and recived in UTF-8.
Java Code:
Class: Server
}
Class Message Dispatcher:
Class: ClientInfo
Class ClientListner:
Class:ClientSender
Ok this is the java code,now to the c# code
c# Code:
Varibales used:
Function: Intelize connection:
Function: ReciveMessages
Function: proceesMessage:
Function sendMessage:
thats it thats all you need to have communication between java server and c# client. if you have any questions dont hesitate to post them here.
选择用于编码/发送字符串的协议。例如:
<字符串长度(4 字节)><字符串数据(长度字节)>
编写一些 Java 代码来发送遵循您在 # 中选择的任何协议的字符串1.因此,使用上面的示例,您可以执行以下操作:
用 C# 编写一些等效代码来解码正在发送的字符串。它看起来很像您的 Java 代码,只不过是读取而不是写入。
Choose a protocol for encoding/sending your strings. For instance:
<length of string (4 bytes)><string data (length bytes)>
Write some Java code to send a string that follows whatever protocol you chose in #1. So using the example above, you could do something like:
Write some equivalent code in C# to decode the strings that are being sent. It will look a lot like your Java code, except with reads instead of writes.
我想我已经找到了一个好的、可行的解决方案。使用 UDP 套接字:
Java 代码
C# 代码
I think I've found a good, working solution. Using UDP sockets:
Java code
C# code
我只想使用 SOAP 协议。您可以在 C# 端使用 WCF,在 Java 端使用 JMS(Java 消息传递)。这两种技术都是基于 SOAP 构建的,因此它们可以读取彼此的消息。他们都使用 WSDL。
I would just use SOAP protocol. You can use WCF on the C# side and JMS (Java messging) on the Java side. Both these technologies are built on SOAP so they can read each other's messages. They both use WSDL.