Git Product home page Git Product logo

socket's Introduction

Socket

Threaded Java socket server and client

Example

Create a server at port 5556 with a MessageHandler, which is the EchoHandler in this example.

SocketServer server = new SocketServer(5556, new EchoHandler());

Create a client connecting to localhost's port 5556.

SocketClient client = new SocketClient(InetAddress.getLocalHost(), 5556);

Send a message from the client.

client.println("Hello!");

Print out the message from the server. The function SocketClient.readLine() blocks.

System.out.println(client.readLine());

The EchoHandler implements the MessageHandler interface and overrides the abstract method onReceive(). The argument connection enables you to send a string back to the client.

public class EchoHandler implements MessageHandler {
    @Override
    public void onReceive(Connection connection, String message) {
        connection.send(message);
    }
}

Features

SocketServer is threaded. It creates a thread for accepting connections and creates a new thread each time a new client is connected. SocketClient is not threaded and the function readLine() blocks.

Current Issues

  • 2014.12.13. Do not have methods to close connections from SocketServer nor SocketClient. Connection does not close until the program exits. Using Socket.close() and ServerSocket.close() does not work elegantly.

socket's People

Contributors

areong avatar

Watchers

James Cloos avatar kevin chen 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.