Posted by : Unknown Friday, 26 June 2015

Client Side Program

import java.io.*; 
import java.net.*; 
class ChatClient extends Thread 
    Socket s; 
    static PrintWriter out; 
    static BufferedReader read,in; 
    Thread t;

    ChatClient() throws Exception 
    { 
        s = new Socket("127.0.0.1",5000); 
        System.out.println("client connected to server");  
        read = new BufferedReader(new InputStreamReader(System.in)); 
        in=new BufferedReader(new InputStreamReader(s.getInputStream())); 
        out=new PrintWriter(s.getOutputStream()); 
        t=new Thread(this); 
        t.start(); 
    } 
     
    public void run() 
    { 
        String str1; 
        while(true) 
        { 
            try 
            { 
                      str1=in.readLine(); 
                      System.out.println("\n"+"From server: "+str1); 
                } 
                catch(Exception e) 
                { 
                       System.out.println(e); 
            } 
        } 
    }

    public static void main(String args[]) throws Exception 
    { 
        String str2; 
        new ChatClient(); 
        while(true) 
        { 
            str2=read.readLine(); 
            out.println(str2); 
            out.flush();   
           } 
    } 
}

Server Side Program

import java.io.*;
import java.net.*;

class ChatServerTCP extends Thread
{
    ServerSocket server;
    Socket s;
    static PrintWriter out;
    static BufferedReader read,in;
    Thread t;

ChatServerTCP() throws Exception
{
    server = new ServerSocket(5000);
    System.out.println("Server is waiting for client");
    s=server.accept();
    System.out.println("connection is established "+s.getInetAddress());
    read = new BufferedReader(new InputStreamReader(System.in));
    in=new BufferedReader(new InputStreamReader(s.getInputStream()));
    out=new PrintWriter(s.getOutputStream());
    t=new Thread(this);
    t.start();
}

public void run()
{
    String str1;
    while(true)
    {
        try
            {
                  str1=in.readLine();
                  System.out.println("\n"+"From client: "+str1);
            }
            catch(Exception e)
            {
                   System.out.println(e);
            }
      }
}

public static void main(String args[]) throws Exception
{
    ChatServerTCP cs = new ChatServerTCP();
    String str2;
    while(true)
    {
             str2=read.readLine();
             out.println(str2);
             out.flush(); 
       }
}
}

How to run:

  1. First run server program then next run client program
  2. Give input from client side
  3. Next give input in server side

Client Side Output:

client connected to server
hi

From server: hi da

how r u?

From server: fine

Server Side Output:

Server is waiting for client 
connection is established /127.0.0.1 

From client: hi 
hi da
From client:

From client: how r u? 
fine

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Welcome to My Blog

Translate

Popular Post

Total Pageviews

Blog Archive

- Copyright © Learning Java Program - Powered by Blogger -