Friday, 28 April 2023

Advance Java-Slip1-Write a socket program in java for chatting application.(Use Swing)

 Write a socket program in java for chatting application.(Use Swing)

 //Server Side Program

 import java.io.*;

import java.awt.*;

import java.awt.event.*;

import java.net.*;

 

public class Slip1b extends Frame implements ActionListener, Runnable {

     BufferedReader br;

    PrintWriter pw;

     Button b1;

    TextField t1;

    TextArea ta;

    Thread t;

     public Slip1b() {

       Frame f = new Frame("Server");

        f.setLayout(new FlowLayout());

 

        b1 = new Button("Send");

 

        b1.addActionListener(this);

 

        t1 = new TextField(20);

 

        ta = new TextArea(10, 20);

 

        f.add(t1);

 

        f.add(ta);

 

        f.add(b1);

 

        try {

 

            ServerSocket ss = new ServerSocket(2000);

 

            Socket s = ss.accept();

 

            System.out.println(s);

 

            br = new BufferedReader(new InputStreamReader(s.getInputStream()));

 

            pw = new PrintWriter(s.getOutputStream(), true);

 

        } catch (Exception e) {

 

        }

 

        t = new Thread(this);

 

        t.start();

 

        f.setSize(400, 400);

 

        f.setVisible(true);

 

    }

 

 

 

    public void actionPerformed(ActionEvent ae) {

 

        pw.println(t1.getText());

 

        t1.setText("");

 

    }

 

 

    public void run() {

 

        while (true) {

 

            try {

 

                String str = br.readLine();

 

                ta.append(str + "\n");

 

            } catch (Exception e) {

 

            }

 

        }

 

    }

 

 

 

    public static void main(String[] args) {

 

        Slip1b c = new Slip1b();

 

    }

 

}

 

 

 

C:\Program Files\Java\jdk1.7.0_80\bin> javac Slip1b.java

 

C:\Program Files\Java\jdk1.7.0_80\bin> javac Slip1b.java

 

C:\Program Files\Java\jdk1.7.0_80\bin>java Slip1b

Socket[addr=/127.0.0.1,port=58990,localport=2000]

 

 

//Client Side Program

 

import java.io.*;

import java.awt.*;

 

import java.awt.event.*;

 

import java.net.*;

 

 

public class Slip1bb extends Frame implements ActionListener, Runnable {

 

   BufferedReader br;

 

    PrintWriter pw;

    Button b1;

    TextField t1;

    TextArea ta;

    Thread t;

    Socket s;

 

 

    public Slip1bb() {

 

        Frame f = new Frame("Client");

        f.setLayout(new FlowLayout());

        b1 = new Button("Send");

        b1.addActionListener(this);

        t1 = new TextField(15);

        ta = new TextArea(12, 20);

        f.add(t1);

        f.add(ta);

        f.add(b1);

 

        try {

 

           s = new Socket("localhost",2000);

 

            br = new BufferedReader(new InputStreamReader(s.getInputStream()));

 

            pw = new PrintWriter(s.getOutputStream(), true);

 

        } catch (Exception e) {

 

        }

 

        t = new Thread(this);

 

        t.start();

 

        f.setSize(400, 400);

 

        f.setVisible(true);

 

    }

 

 

    public void actionPerformed(ActionEvent ae) {

 

        pw.println(t1.getText());

 

        t1.setText("");

 

    }

 

 

    public void run() {

 

        while (true) {

 

            try {

 

                String str = br.readLine();

 

                ta.append(str + "\n");

 

            } catch (Exception e) {

 

            }

 

        }

 

    }

 

 

    public static void main(String[] args) {

 

        Slip1bb c = new Slip1bb();

 

    }

 

}    

 

 

C:\>cd C:\Program Files\Java\jdk1.7.0_80\bin

 

C:\Program Files\Java\jdk1.7.0_80\bin>javac Slip1bb.java

 

C:\Program Files\Java\jdk1.7.0_80\bin>java Slip1bb