0% found this document useful (0 votes)
47 views20 pages

Fspchatbot

The document contains code for a multi-threaded chat bot application with client-server architecture. The server code initializes socket connections and handles multiple clients concurrently using threads. The client code connects to the server, allows input/output between the client and server, and closes the connection when the user enters "Over". A separate ClientHandler class manages individual client connections and threads.

Uploaded by

Bilal Imran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views20 pages

Fspchatbot

The document contains code for a multi-threaded chat bot application with client-server architecture. The server code initializes socket connections and handles multiple clients concurrently using threads. The client code connects to the server, allows input/output between the client and server, and closes the connection when the user enters "Over". A separate ClientHandler class manages individual client connections and threads.

Uploaded by

Bilal Imran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

SERVER CODE :

package server1;

//the set of libraries we are usig for our project"chat bot"

import java.io.IOException;

import java.net.BindException;

import java.io.*;

import java.net.ServerSocket;

import java.net.Socket;

import java.lang.ClassNotFoundException;

import java.util.ArrayList;

import java.util.Scanner;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import javafx.application.Application;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.stage.Stage;

import javax.swing.JOptionPane;
class Introduction extends Thread {

public void base() throws InterruptedException

int basechoice=0;

Scanner scan = new Scanner ( System.in);

System.out.println("*********************************************");

Thread.sleep(500);

System.out.println("*********************************************");

Thread.sleep(500);

System.out.println("*********************************************");

Thread.sleep(500);

System.out.println(" WELCOME TO THE CHAT BOT ");

System.out.println("*********************************************");

Thread.sleep(500);

System.out.println("*********************************************");

Thread.sleep(500);

System.out.println(" PRESS 1 TO CONTINUE AND 2 FOR INSTRUCTIONS ");

Thread.sleep(500);

System.out.println("*********************************************");

Thread.sleep(500);

System.out.println("*********************************************");

Thread.sleep(500);
basechoice = scan.nextInt();

if ( basechoice == 1 )

System.out.println(" NOW YOUR'E PROCEESING CLIENT ");

Thread.sleep(500);

System.out.println("*********************************************");

Thread.sleep(500);

System.out.println("*********************************************");

Thread.sleep(500);

class optionpane

{
}

public class SERVER1 {

//initialize socket and input stream

private Socket socket = null;

private ServerSocket server = null;

private DataInputStream in = null;

private static ArrayList<CLIENTHANLDLER> clients = new ArrayList<> ();

private static ExecutorService pool = Executors.newFixedThreadPool(4);

private DataInputStream din = null;

private DataOutputStream dout = null;

// constructor with port

public SERVER1(int port , String s)

// starts server and waits for a connection

System.out.println(" the Server has started");


try

int i =0 ;

server = new ServerSocket(port);

while ( true )

socket = server.accept();

System.out.printf(" %s Says welcome to our Client #%d \n \n " , s , i);

i++;

System.out.println("Client has accepted");

CLIENTHANLDLER clientobject = new CLIENTHANLDLER(socket , clients );

clients.add(clientobject);
pool.execute(clientobject);

// takes input from the client socket

catch(IOException i)

System.out.println(i);

public static void main(String args[]) throws InterruptedException

// Introduction obj = new Introduction ();

//

// obj.base();

// Application.launch(args);
JOptionPane.showMessageDialog( null, " We are now Coonecting " , " CHAT BOT ",
JOptionPane.PLAIN_MESSAGE );

String firstNumber = JOptionPane.showInputDialog( " Enter Host Name " );

String name = firstNumber;

SERVER1 server = new SERVER1(5000 , name );

panes obj2 = new panes();

} CLIENT CODE :

package client1;

import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

import java.io.*;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.net.*;

import java.util.Scanner;
import javax.swing.JOptionPane;

class Introduct2 extends Thread {

public void base2() throws InterruptedException

int clientchoice =0;

Scanner scan = new Scanner ( System.in);

System.out.println("*********************************************");

Thread.sleep(500);

System.out.println("*********************************************");

Thread.sleep(500);

System.out.println("*********************************************");

Thread.sleep(500);

System.out.println(" WELCOME TO THE CHAT BOT ");

System.out.println("*********************************************");

Thread.sleep(500);

System.out.println("*********************************************");

Thread.sleep(500);

System.out.println(" NOW YOUR'E CONNECTED CLIENT ");

Thread.sleep(500);

System.out.println("*********************************************");
Thread.sleep(500);

System.out.println("*********************************************");

Thread.sleep(500);

do{

System.out.println("PRESS 1 FOR INSTRUCTIONS AND PRESS 2 TO CONTINUE");

clientchoice=scan.nextInt();

if(clientchoice==1)

System.out.println("*********************************************");

System.out.println("*********************************************");

System.out.println("1. YOU CAN NOW START TYPING ");

System.out.println("2. THERE IS NO WORD LIMITS FOR MESSAGE ");

System.out.println("3. CONNECTION IS MAINTAINED AS LONG YOU DON'T TYPE \" Over \" ");

System.out.println("*********************************************");

System.out.println("*********************************************");

else if (clientchoice == 2 )

{
System.out.println("*********************************************");

System.out.println("*********************************************");

System.out.println("1. YOU CAN NOW START TYPING ");

System.out.println("*********************************************");

System.out.println("*********************************************");

else

System.out.println("*********************************************");

System.out.println("*********************************************");

System.out.println(" IN-VALID ENTRY DEAR !! ");

System.out.println("*********************************************");

System.out.println("*********************************************");

} while ( clientchoice != 2 );

class Client {

String Name ;
// initialize socket and input output streams

private Socket socket = null;

private DataInputStream input = null;

private DataOutputStream out = null;

//pp

// constructor to put ip address and port

public Client(String address, int port , String names) throws IOException

int a=0;

// establish a connection

try

socket = new Socket(address, port);

System.out.println("**** Connection Mainatained Succesfully **** ");

System.out.println("**** Start your typing from here **** ");

System.out.printf(" %s has joined to the server's Network \n" ,names);

// takes input from terminal

input = new DataInputStream(System.in);

// sends output to the socket

out = new DataOutputStream(socket.getOutputStream());


}

catch(UnknownHostException u)

System.out.println(u);

catch(IOException i)

System.out.println(i);

String line = "";

// keep reading until "Over" is input

while (!line.equals("Over"))

try

System.out.printf(" %s says : " , names );

line = input.readLine();

out.writeUTF(line);
}

catch(IOException i)

System.out.println(i);

// close the connection

try

input.close();

out.close();

socket.close();

catch(IOException i)

System.out.println(i);

public class CLIENT1 {

// initialize socket and input output streams


private Socket socket = null;

private DataInputStream input = null;

private DataOutputStream out = null;

public static void main(String[] args)throws UnknownHostException, IOException,


ClassNotFoundException, InterruptedException{

//Introduct2 obj2= new Introduct2();

//obj2.base2();

String firstNumber = JOptionPane.showInputDialog( " Enter Client Name " );

String name = firstNumber;

Client client = new Client("127.0.0.1", 5000, name);

}
CLIENTHANDLER CODE ( MULTIPLE CLIENT
CONNECTION ) :
/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package server1;

import java.io.BufferedInputStream;

import java.io.BufferedReader;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.Socket;

import java.util.ArrayList;

/**

* @author Zainab Ghauri

*/

public class CLIENTHANLDLER implements Runnable {


private Socket socket = null;

private DataInputStream in = null;

private DataOutputStream out = null;

private ArrayList<CLIENTHANLDLER> clients;

public CLIENTHANLDLER( Socket c , ArrayList<CLIENTHANLDLER> clients ) throws IOException

this.socket=c;

this.clients= clients;

in = new DataInputStream( new BufferedInputStream(socket.getInputStream()));

// sends output to the socket

out = new DataOutputStream(socket.getOutputStream());

// CLIENTHANLDLER(Socket socket, ArrayList<CLIENTHANLDLER> clients) {

// throw new UnsupportedOperationException("Not supported yet."); //To change body of


generated methods, choose Tools | Templates.

// }

@Override

public void run() {

String line = " ";


//int ok = 1;

try{

// reads message from client until "Over" is sent

while (!line.equalsIgnoreCase("over"))

try

//System.out.printf(" Chat from client # %d ", ok );

line = in.readUTF();

System.out.println(line);

// outToAll(line.substring(1));

catch(IOException i)

System.out.println(i);

System.out.println("Closing connection");

//ok++;

// close connection

socket.close();

in.close();

catch (IOException e)
{

e.printStackTrace();

// private void outToAll(String substring) {

//

// for ( CLIENTHANLDLER aclient : clients )

// {

//

//

// }

// }

//

Output :
It ends when you write Over

But I want to implement kay server ko show ho kay kon message kar
raha ha like client 1 ya client 2
As

Client 1 ( his/her message )

Client 2 ( his /her message )

Or secondly , server sey message sab client ko jay , I tried using this but
it fails !

You might also like