套接字:将字符串从Java发送到C#? [英] Sockets: Send strings from Java to C#?

查看:89
本文介绍了套接字:将字符串从Java发送到C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我需要将Java程序中的字符串消息实时发送到C#程序.
Internet上有很多示例,但是我找不到适合我目的的(可能是)Java客户端(套接字代码)和c#服务器(套接字代码).
谢谢.

Hello,

I need to send string messages from Java program to C# program in real time.
There are many examples in the Internet but I can''t find anything good for my purpose that is (probably) Java client (sockets code) and c# server (sockets code).
Thank you.

推荐答案

对于C#部分,请查看 TcpClient [^ ]来自MSDN的两个示例都是不错的初学者示例,应该可以帮助您入门.
For the C# part, check out TcpListener [^] and TcpClient[^] both example from MSDN are good beginner samples and should get you started.


还有很多

我找到了一个很好的可行的解决方案.使用UDP套接字:

Java代码:
< code>
public void runJavaSocket(){
System.out.println("Java套接字程序已启动."); int i = 0;

尝试{
DatagramSocket socket = new DatagramSocket();
System.out.println(正在发送udp套接字...");
//发送消息"HI"
socket.send(toDatagram("HI",InetAddress.getByName("127.0.0.1"),3800));
而(true)
{
System.out.println("Send hi" + i);
Thread.currentThread();
Thread.sleep(1000);
socket.send(toDatagram("HI" + String.valueOf(i),InetAddress.getByName("127.0.0.1"),3800));
i ++;
}
} catch(Exception e){
e.printStackTrace();
}
公共DatagramPacket toDatagram(
字符串s,InetAddress destIA,int destPort){
//在Java 1.1中已弃用,但可以使用:
byte [] buf =新的byte [s.length()+1];
s.getBytes(0,s.length(),buf,0);
//正确的Java 1.1方法,但这是
//损坏(将字符串截断):
//byte [] buf = s.getBytes();
返回新的DatagramPacket(buf,buf.length,
destIA,destPort);
}
</code>

C#代码:
< code>
字符串returnData;
byte [] receiveBytes;
//ConsoleKeyInfo cki = new ConsoleKeyInfo();

使用(UdpClient udpClient = new UdpClient(new IPEndPoint(IPAddress.Parse("127.0.0.1"),3800)))
{
IPEndPoint remoteIpEndPoint =新的IPEndPoint(IPAddress.Parse("127.0.0.1"),3800);
而(true)
{
receiveBytes = udpClient.Receive(ref remoteIpEndPoint);
returnData = Encoding.ASCII.GetString(receiveBytes);
Console.WriteLine(returnData);
}
}
< code>
I''ve found a good, working solution. Using UDP sockets:

Java code:
<code>
public void runJavaSocket() {
System.out.println("Java Sockets Program has started."); int i=0;

try {
DatagramSocket socket = new DatagramSocket();
System.out.println("Sending the udp socket...");
// Send the Message "HI"
socket.send(toDatagram("HI",InetAddress.getByName("127.0.0.1"),3800));
while (true)
{
System.out.println("Sending hi " + i);
Thread.currentThread();
Thread.sleep(1000);
socket.send(toDatagram("HI " + String.valueOf(i),InetAddress.getByName("127.0.0.1"),3800));
i++;
}
} catch (Exception e) {
e.printStackTrace();
}
public DatagramPacket toDatagram(
String s, InetAddress destIA, int destPort) {
// Deprecated in Java 1.1, but it works:
byte[] buf = new byte[s.length() + 1];
s.getBytes(0, s.length(), buf, 0);
// The correct Java 1.1 approach, but it''s
// Broken (it truncates the String):
// byte[] buf = s.getBytes();
return new DatagramPacket(buf, buf.length,
destIA, destPort);
}
</code>

C# code:
<code>
string returnData;
byte[] receiveBytes;
//ConsoleKeyInfo cki = new ConsoleKeyInfo();

using (UdpClient udpClient = new UdpClient(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3800)))
{
IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3800);
while (true)
{
receiveBytes = udpClient.Receive(ref remoteIpEndPoint);
returnData = Encoding.ASCII.GetString(receiveBytes);
Console.WriteLine(returnData);
}
}
<code>


这篇关于套接字:将字符串从Java发送到C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆