In my last post i showed how TCP programming is done using java. In this post i will show you how to build a very basic command line chat application.
Very Basic Chat App:-
- Establish Connection between sender and receiver (Client & Server).
- sender(Client) sends the message Receiver(Server) display it continuously .
- when client send the message "quit" both server and client quits.
Server Logic:-
- Create a ServerSocket object.
- Wait for a client to connect.
- If connection is established receive message .(Do this in a While loop so it can receive msg after msg)
- If message is "quit" break the loop.
- close the connection and terminate.
Server Code:-
I have commented all the importent points in code.
import java.net.*; import java.io.*; public class ChatServer { private Socket socket = null; private ServerSocket server = null; private DataInputStream streamIn = null; public ChatServer(int port) { try { //server code to read msg from connection and write it to the cmd System.out.println("Binding to port " + port + ", please wait ..."); server = new ServerSocket(port); //acquiring port System.out.println("Server started: " + server); System.out.println("Waiting for a client ..."); socket = server.accept(); //waiting for connection System.out.println("Client accepted: " + socket); open(); boolean done = false; while (!done) //reading again and again until quit message is not recived { try { String line = streamIn.readUTF(); System.out.println(line); done = line.equals(".bye"); } catch(IOException ioe) { done = true; } } close(); } catch(IOException ioe) { System.out.println(ioe); } } public void open() throws IOException //get the input stream to read msg from connection { streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream())); } public void close() throws IOException //close the connection { if (socket != null) socket.close(); if (streamIn != null) streamIn.close(); } public static void main(String args[]) //Main Method { ChatServer server = null; if (args.length != 1) System.out.println("Usage: java ChatServer port"); else server = new ChatServer(Integer.parseInt(args[0])); } }
Client Logic:-
- create a Socket.
- establish the connection
- read message from cmd and send it to connection
- on "quit" msg exit
Client Code:-
I have commented all the importent points in code.
import java.net.*; import java.io.*; public class ChatClient { private Socket socket = null; private DataInputStream console = null; private DataOutputStream streamOut = null; public ChatClient(String serverName, int serverPort) { System.out.println("Establishing connection. Please wait ..."); try { socket = new Socket(serverName, serverPort); System.out.println("Connected: " + socket); start(); } catch(UnknownHostException uhe) { System.out.println("Host unknown: " + uhe.getMessage()); } catch(IOException ioe) { System.out.println("Unexpected exception: " + ioe.getMessage()); } String line = ""; while (!line.equals(".quit")) //quit logic { try { line = console.readLine(); streamOut.writeUTF(line); streamOut.flush(); } catch(IOException ioe) { System.out.println("Sending error: " + ioe.getMessage()); } } } public void start() throws IOException //Start { console = new DataInputStream(System.in); streamOut = new DataOutputStream(socket.getOutputStream()); } public void stop() //stop { try { if (console != null) console.close(); if (streamOut != null) streamOut.close(); if (socket != null) socket.close(); } catch(IOException ioe) { System.out.println("Error closing ..."); } } public static void main(String args[])// Main method { ChatClient client = null; if (args.length != 2) System.out.println("Usage: java ChatClient host port"); else client = new ChatClient(args[0], Integer.parseInt(args[1])); } }
How to Run the Code :-
- Open two cmd
- compile both file
- first run ChatServer using the cmnd---> java ChatServer 5001 (where 5001 is port no)
- than run ChatClient using the cmnd---->java ChatClient 127.0.0.1 5001 (127.0.0.1 is your own machine address and 5001 is the port on which Server is running)
My next post is on making this chat app to handle multiple client and may be add some gui to give it a touch.
If you liked or learned something from post than follow me on google+ and share the post .
ThankYou.
No comments:
Post a Comment