简体   繁体   中英

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.

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.

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? (Such that a TCP client can send messages to UDP, and UDP messages can be listened to and send over the TCP connection)

I'll try a very direct answer to the question stated in the title: "How to simulate UDP with TCP in java?"

In short, you can't. TCP guarantees a reliable transmission, while UDP is just fire-and-forget. Therefore, you can always send UDP datagrams into the network without knowing or noticing if the receiver is existant and listening.

TCP, on the other hand, requires a connection to a peer before you can send anything. Therefore, any attempt to "simulate UDP with TCP" must fail when you are trying send while no one is listening.

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; or likewise a program that listens on TCP and resends what it gets by 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.

B (the TCP server socket) gets the packets and must send them via UDP to a client socket call 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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