Code Monkey home page Code Monkey logo

sockets's Introduction

sockets

Sockets project is a simpler implementation of Client Server interaction using Java Sockets.

Created a server at port 5556

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

Created a client communicating to Server at port # 5556

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

Send an xml string to Server client.sendMessageToServer(str);

RequestHandler implements the Message Handler Interface and overrides onRecieve method


	class RequestHandler implements MessageHandler {
	    @Override
	    public void onReceive(Connection connection, String message) {
	      InputSource newstream = new InputSource(new StringReader(message));
				Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(newstream);
				if(getElementValue("Command",doc.getDocumentElement()).equals("") || getElementValue("Command",doc.getDocumentElement())==null){
					message  = message + " Unknown Command";
					System.out.println("Unknown Command");
				}else{
					System.out.println(getElementValue("Command",doc.getDocumentElement()));
	      }
	    }
	  }
  

Features:

  1. SocketServer hanldles incoming client connections using threads. Each time a new client connects or open the socket connection it creates a new thraed.
  2. Includes a server Listening Thread which is looking for incoming client connections and whenever it sees a new connection it creates a new ConnectionThread and let it handle the communication between sever and client.
  3. This can let many incoming threads keep coming in while an existing client is communicating with server.

Possible Extension:

  1. To include Gui interface using Jwing and Crate a Server JPanel which controls the starting of server and another button to initiate a connection as client with server.
  2. Along with GUI SocketServer can have its statusBoard of connected clients, so make the class observable and all other clients being connected as as observers which can update all clients whenever a new client connects and corrosponding client can be displayed on server status board.

sockets's People

Contributors

gautamdudeja90 avatar

Watchers

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