Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

areong/Socket

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Socket

Gitter chat

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.

About

Threaded Java socket server and client

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.