Git Product home page Git Product logo

betterudpsocket's Introduction

BetterUDPSocket

This helps you handle UDP protocol similar to TCP. This will sort your packets into UDPSocket's similar to TCP Socket. You can then write to or read from the socket. As you may know DatagramSocket's don't have continuous I/O. I have made some custom I/O to make allow you to send and receive similar to TCP.

Usage

An example of this project can be found: Test Communication

Creating a server socket

int port = 8080;
UDPServerSocket server = new UDPServerSocket(port);

Creating a socket from server

InetAddress address = InetAddress.getByName("localhost"); //TO ADDRESS
int port = 8080; //TO PORT
UDPSocket socket = server.create(address, port);

Receiving socket from server

server.addUDPListener(new UDPServerSocket.UDPListener(){
    @Override
    public void accept(UDPSocket socket){
    }
}

Closing server server & sockets

server.close(); //CLOSES THE SERVER
socket.close(); //CLOSE THE SOCKET - WONT CLOSE THE SERVER

Set SafeMode - ACK

This will ensure all packets arrive at the destination in order with acknowledgment.

server.setSafeMode(true);

Set KeepAlive

NAT will typically close your port after nothing has been sent or received for 30-60 seconds. Enabling KeepAlive will send a packet for KeepAlive every 25 seconds to ensure the port stays open. If you are using this for P2P where both peers haven't port forwarded only one peer will need to do KeepAlive.

server.setKeepAlive(true);

Set NoDelay

This will send a packet everytime you write to the output stream, there is no need to flush.

server.setNoDelay(true);

Port Forward with UPnP

You can port forward or get your external IP address using UPnP if its availible.

server.openPort();
server.closePort();
server.getExternalIP();

UDP HolePunching

UDP Hole-Punch Image

An example for hole punching with this project can be found: Hole-Punch Server, Hole-Punch Client If you use hole punching the server must be port forwarded while both clients don't have to be. Both clients will then be able to communicate directly without any server relay. This will only work with Non Symmetric NATs.

UDP File Transfer

An example for file transfer with I/O with this project can be found: File-Transfer

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.