Git Product home page Git Product logo

framedtcp's Introduction

FramedTCP

A simple Java networking library that encapsulates messages using length-prefixing

The problem

When you send messages over TCP sockets, you're not guaranteed to receive those messages exactly how you sent them. TCP only guarantees that you'll receive all packets in the correct order, but does not keep bounderies between messages. This means that if you were to read the entire buffer, you may get a partial message or multiple messages at once.

The solution

A possible solution is to encapsule messages into an application-level frame that also carries the length of the message. This way, the receiver always knows where a message starts and where it ends.

Build

This project uses Maven as its build system.

How to use

The library provides a series of classes that you can use to take advantage of its features. Unfortunatelly these are NOT standardized for now. This is how you build an echo server, for example.

Server

try (ServerSocket serverSocket = new ServerSocket(8000)) {
    Socket client = serverSocket.accept();
    FramedReader reader = new FramedReader(client.getInputStream());
    FramedWriter writer = new FramedWriter(new OutputStreamWriter(client.getOutputStream()));
    writer.writeBytes(reader.readBytes());
} catch (Exception e) {
    throw new RuntimeException(e);
}

Client

try (Socket socket = new Socket("", 8000)) {
    FramedReader reader = new FramedReader(socket.getInputStream());
    FramedWriter writer = new FramedWriter(new OutputStreamWriter(socket.getOutputStream()));
    writer.writeString("Hello world");
    System.out.println("ECHO: " + reader.readString());
} catch (Exception e) {
    throw new RuntimeException(e);
}

License

This projects is license under the MIT License.

framedtcp's People

Contributors

alessandro-salerno avatar

Stargazers

 avatar  avatar

Watchers

 avatar

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.