简体   繁体   中英

Need help with udp socket programming in java

Hey guys I am working on a project where I need to broadcast a UDP packet on the actual internet and also receive them on the client. Currently I am using the multicast socket for broadcasting the packet on the local lan. I had come across this project called jstunt for NAT traversal of a UDP datagram but cant find any relevant documentation on it and also no implementation. I am familiar with the concepts of Nat Traversal, UDP hole punching but am facing the same problem as above, no relevant documentation and implementation. So can anyone please help and also suggest some other techniques for achieving this.

As the other poster has mentioned, you can't really "broadcast" a packet freely to the internet. If that were possible, networks could easily be DOS'd and incredible congestion would result. Even within controlled networks, broadcasts are usually tightly controlled so that they do not get out of hand. That said, perhaps you don't really need to "broadcast" the packet.

If you need to create a UDP "tunnel" across the internet, such as how P2P software works, it can be done. The trick is usually NAT. You mentioned you were already familiar with UDP hole punching, but couldn't figure out how to make it work. Here are some Java libraries that can be used for this:

http://www.masquerade.cz/en/nat-tunel-metodou-udp-hole-punching-v-jazyce-java/ http://ulno.net/projects/jpunch/ http://samy.pl/pwnat/

Also check out UPNP: http://en.wikipedia.org/wiki/Universal_Plug_and_Play

And the STUNT library: http://nutss.gforge.cis.cornell.edu/stunt.php

It's highly unlikely that your ISP, and your clients', will support UDP broadcast, let alone what the Internet backbone may or may not allow. You need to investigate that first. It's not a programming problem yet, it's a feasibility problem.

ITS BASIC DAY TIME SERVER IMPLEMENTATION USING UDP SOCKET ITS HELP YOU

CLIENT

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

public class DayTime_Client_Udp 
{
    public static void main (String[] args) throws IOException 
    {
            String hostname= "localhost";
            int port=13;

        if ((args.length == 1))
{
                hostname=args[0];
            }
else if ((args.length==2))
        {
                hostname=args[0];
                port=Integer.parseInt(args[1]);
            }

        InetAddress host = InetAddress.getByName(hostname);
            DatagramSocket socket = new DatagramSocket ();

            DatagramPacket packet=new DatagramPacket (new byte[100], 0,host, port);


            socket.send (packet);

            packet.setLength(100);
            socket.receive (packet);
            socket.close ();

        byte[] data = packet.getData ();
            String time=new String(data);  // convert byte array data into string
        System.out.println(time);
    }
}

SERVER

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

public class DayTime_Server_Udp
{
    public static final int DEFAULT_PORT = 3001;

    public static void main (String[] args) throws IOException 
    {
            if (args.length > 1)
{
            throw new IllegalArgumentException ("Syntax: DaytimeServer [<port>]");
            }

DatagramSocket socket = new DatagramSocket(args.length == 0 ?
DEFAULT_PORT : Integer.parseInt (args[0]));

        DatagramPacket packet = new DatagramPacket (new byte[1], 1);

        while (true) 
        {
                socket.receive (packet);
                System.out.println("Received from: " + packet.getAddress () + ":" +
 packet.getPort ());
                byte[] outBuffer = new java.util.Date ().toString ().getBytes ();
                packet.setData (outBuffer);
                packet.setLength (outBuffer.length);
                socket.send (packet);
            }
    }
}

IF MORE HELP REQUIRED THAN COMMENT IT I ALSO HAVE OTHER PROGRAM WITH UDP BASD SO ITS HELP UU

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