简体   繁体   English

如何在Java中使用TCP模拟UDP?

[英]How to simulate UDP with TCP in java?

I want to be able to have a TCP client send basic strings to be broadcast over UDP. 我希望能够让TCP客户端发送基本字符串以通过UDP广播。

I can't seem to find any resources on how to write UDP Datagrams into TCP or send messages over TCP that get intercepted and translated into UDP. 我似乎找不到有关如何将UDP数据报写入TCP或通过TCP发送被拦截并转换为UDP的消息的任何资源。

Sender: 发件人:

import java.io.*;
import java.net.*;
public class MulticastSender {
  public static void main(String[] args) {
    DatagramSocket socket = null;
    DatagramPacket outPacket = null;
    byte[] outBuf;
    final int PORT = 8888;

    try {
      socket = new DatagramSocket();
      long counter = 0;
      String msg;

      while (true) {
        msg = "This is multicast! " + counter;
        counter++;
        outBuf = msg.getBytes();

        //Send to multicast IP address and port
        InetAddress address = InetAddress.getByName("224.2.2.3");
        outPacket = new DatagramPacket(outBuf, outBuf.length, address, PORT);

        socket.send(outPacket);

        System.out.println("Server sends : " + msg);
        try {
          Thread.sleep(500);
        } catch (InterruptedException ie) {
        }
      }
    } catch (IOException ioe) {
      System.out.println(ioe);
    }
  }
}

Receiver: 接收器:

import java.io.*;
import java.net.*;

public class MulticastReceiver {
  public static void main(String[] args) {
    MulticastSocket socket = null;
    DatagramPacket inPacket = null;
    byte[] inBuf = new byte[256];
    try {
      //Prepare to join multicast group
      socket = new MulticastSocket(8888);
      InetAddress address = InetAddress.getByName("224.2.2.3");
      socket.joinGroup(address);

      while (true) {
        inPacket = new DatagramPacket(inBuf, inBuf.length);
        socket.receive(inPacket);
        String msg = new String(inBuf, 0, inPacket.getLength());
        System.out.println("From " + inPacket.getAddress() + " Msg : " + msg);
      }
    } catch (IOException ioe) {
      System.out.println(ioe);
    }
  }
}

Is it possible for some way for a TCP client to write to a TCP socket opened on a server, and put the contents to be sent over listening UDP to a UDP client? TCP客户端是否可以通过某种方式写入服务器上打开的TCP套接字,然后将要通过侦听UDP的内容发送到UDP客户端? (Such that a TCP client can send messages to UDP, and UDP messages can be listened to and send over the TCP connection) (这样,TCP客户端可以将消息发送到UDP,并且可以侦听UDP消息并通过TCP连接发送UDP消息)

I'll try a very direct answer to the question stated in the title: "How to simulate UDP with TCP in java?" 我将尝试直接回答标题中提到的问题:“如何在Java中使用TCP模拟UDP?”

In short, you can't. 简而言之,你不能。 TCP guarantees a reliable transmission, while UDP is just fire-and-forget. TCP保证可靠的传输,而UDP就是一劳永逸的。 Therefore, you can always send UDP datagrams into the network without knowing or noticing if the receiver is existant and listening. 因此,您始终可以将UDP数据报发送到网络中,而无需知道或通知接收方是否存在并正在侦听。

TCP, on the other hand, requires a connection to a peer before you can send anything. 另一方面,TCP需要先连接到对等方,然后才能发送任何内容。 Therefore, any attempt to "simulate UDP with TCP" must fail when you are trying send while no one is listening. 因此,在没有人收听的情况下尝试发送时,任何“用TCP模拟UDP”的尝试都必须失败。

Obviously, it's not very difficult to write a program that listens on UDP, and resends any data it receives to a peer over TCP; 显然,编写一个侦听UDP并通过TCP将其接收的所有数据重新发送给对等方的程序并不是很难。 or likewise a program that listens on TCP and resends what it gets by UDP. 或类似的程序,它侦听TCP并重新发送它从UDP获得的内容。 But that's not "simulating", just resending with a different protocol. 但这不是“模拟”,只是重新发送了不同的协议。

I'm not sure if I did understand the question. 我不确定我是否理解这个问题。

Correct me if I'm wrong: 如果我错了纠正我:

A - a client is sending TCP packets to a TCP Server Socket called B. A-客户端正在将TCP数据包发送到称为B的TCP服务器套接字。

B (the TCP server socket) gets the packets and must send them via UDP to a client socket call C? B(TCP服务器套接字)获取数据包,并且必须通过UDP将其发送到客户端套接字调用C? (in this case this client socket as you call it is in reality a server socket.) (在这种情况下,您所说的此客户端套接字实际上是服务器套接字。)

If this is all you want to do the answer is trivial. 如果这是您要做的全部,那么答案很简单。

You get the TCP messages on you server socket called B, you get the content out of them and you put the content on a UDP datagram and send them through UDP to C. 您在称为B的服务器套接字上获取TCP消息,从中获取内容,然后将内容放在UDP数据报上,并通过UDP将其发送到C。

The only suggestion from me is: Once you receive the TCP messages on your server B, instantiate a new thread to work the message received (get the content and then put it into a UDP packet) and send it to the client C. 我的唯一建议是:一旦在服务器B上收到TCP消息,请实例化一个新线程以处理接收到的消息(获取内容,然后将其放入UDP数据包中)并将其发送给客户端C。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM