Chat Server and Client in Java

Submitted by Karthikeyan on
A simple Java Console Application for Chat Server and Client
It uses Sockets in Java

Chat Server

chats.java

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
class chats {
    public static void main(String args[]) throws Exception
    {
        ServerSocket svrsoc = new ServerSocket(2000);
        while(true)
        {
            Socket soc = svrsoc.accept();
            InputStream is = soc.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String str = br.readLine();
            System.out.println("Client connected.."+str);
        }
    }
    
}

Chat Client

chatc.java

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
public class chatc {
    public static void main(String args[]) throws Exception
    {
        while(true)
        {
            Socket soc = new Socket("localhost",2000);
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
            String str = br.readLine();
            OutputStream os = soc.getOutputStream();
            PrintWriter pw = new PrintWriter(os,true);
            pw.println(str);
        }
    }
}