Вы находитесь на странице: 1из 16

/********************************************************************************* ************************ LAN CHATING APPLICATION

********************************************************************************** ***********************/

//Client_ side Application CLient.java (java code)

import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*;

class client { public static Frame

// main class

Lgn1,Lgn2;

public static Frame msg; static String yn; static String x="->"; public static TextField txtSpw =new TextField(8);

public static String client_name; static class openConnection { // Sub-class for socket connection between Client & Server

public openConnection() & Server {

// constructor for Socket & port connection between Client

Socket s; BufferedReader br; BufferedWriter bw;

// //buffer

client socket

try {

s = new Socket("127.0.0.1",6000); br = new BufferedReader(new InputStreamReader(s.getInputStream())); bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); Lgn1=new LoginFrame(s,br,bw); Lgn1.setVisible(true);

}catch(Exception e){System.out.println(e);}

} }

static class LoginFrame extends Frame implements ActionListener // frame declaration for login screen { Socket s; Label Spw Button Connect BufferedReader br; BufferedWriter bw; =new Label ("ENTER YOUR NAME"); =new Button("Connect");

public LoginFrame(Socket s1,BufferedReader br1,BufferedWriter bw1) //Constructor

{ s=s1; br=br1; bw=bw1; setTitle("C O N N E C T..2..O P E N C H A T");//Setting Frame Title setLocation(231, 175); /* Displaying Login Form */ setSize(340,194); setLayout(null); //Setting Layout of Frame setVisible(true);

//txtSpw.setEchoChar('*'); addWindowListener(new XWindow()); add(Spw); add(txtSpw); add(Connect); Spw.setBounds(13,69,184,19); txtSpw.setBounds(199,66,88,22); Connect.setBounds(120,129,109,25); Connect.addActionListener(this); } public void actionPerformed ( ActionEvent e) // function connect button {

if (e.getSource().equals(Connect)) { try {

bw.newLine(); // bw.write(txtSpw.getText()); //bw.newLine(); bw.flush(); //s.close(); yn=br.readLine();

if(yn.equals("1")) { setVisible(false); Lgn2=new Client_connect(s,br,bw);

Lgn2.setVisible(true); } else { setVisible(false); msg=new MsgBox();

msg.setVisible(true); //setVisible(true);

} } catch(Exception a){} //System.exit(0); } else

{ try { bw.write(x); bw.write(txtSpw.getText()); bw.newLine(); bw.flush(); }catch(Exception m){} } } static class XWindow extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(1); } } } static class Client_connect extends Frame implements ActionListener,Runnable // frame declaration for chating screen { Label head= new Label ("Welcome To Open Chat "); TextField text; Button button1,button2; List list; Socket s; BufferedReader br; BufferedWriter bw;

public void run() {

try {

s.setSoTimeout(1); }catch(Exception e){}

while (true) // reading from the server socket & writing in the client list box { try { list.addItem(br.readLine()); } } } catch (Exception h){}

public Client_connect(Socket s1,BufferedReader br1,BufferedWriter bw1) { s=s1; br=br1; bw=bw1; setTitle("CHAT WINDOW client logon ");//Setting Frame Title setSize(200,300); setLocation(300,0);

this.setLayout(new BorderLayout()); button1 = new Button("Exit"); button2 = new Button("Send");

button1.addActionListener(this); button2.addActionListener(this);

list = new List(); text = new TextField(); add(list,"Center"); add(head,"North"); add(button1,"East"); add(button2,"West"); add(text,"South"); setVisible(true);

try { bw.newLine(); bw.flush();

Thread th; declaration for Thread th = new Thread(this); th.start(); } catch(Exception e){}

// threads for

} public void actionPerformed ( ActionEvent e) // function for obtaing values { if (e.getSource().equals(button1)) { try { s.close(); } catch(Exception a){} System.exit(0); } else { try { bw.write(txtSpw.getText()); bw.write(text.getText()); bw.newLine();bw.flush(); }catch(Exception m){} } } } public static class MsgBox extends Frame // Message box class { Label lblMsg=new Label("INVALID Id Number ** N O M A T C H ** SORRRY!! "); Button OK=new Button("OK");

public MsgBox() { setTitle("Notify"); setLocation(220,250); setSize(350,150); setResizable(false); setLayout(null); add(lblMsg); add(OK); lblMsg.setBounds(25,15,330,90); OK.setBounds(130,110,80,30); OK.addActionListener(new OK_Click());

} //------------------------------------------------------public class OK_Click implements ActionListener // closing the frame { public void actionPerformed(ActionEvent e) { setVisible(false); } }

//------------------------------------------------------} public static void main(String arg[])

{ new openConnection(); }

//Server Application

import java.net.*; import java.io.*; import java.sql.*; import java.util.*; //================================================================== class ServerApp { ServerSocket s; Socket s1;

static int client_counter=0; BufferedReader net_input[]; BufferedWriter net_output[];

public static void main(String arg[]) // main function { new ServerApp(); main function } // constructor called in the

public ServerApp() { net_input=new BufferedReader[20];

// Constructor initialised

// Object declared for reading

net_output=new BufferedWriter[20]; // Writing from the socket

try { s=new ServerSocket(6000); Port } catch(IOException e) { System.out.println("error in server socket"); return; } // handling errors // Socket created at the defined

System.out.println("waiting for users..........");

while(true) // Waiting for Client's Socket for connection { try { s1=s.accept(); java.net.* " packages service_request(s1); } catch(Exception e){} } // function called // accept function is defined in the "

public void service_request(Socket socket) // Socket connected { BufferedReader input; BufferedWriter output;

try { input=new BufferedReader(new InputStreamReader(socket.getInputStream())); // Reading data from client socket output = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); // Writing data to Client socket }

catch(IOException e) // handling error {

System.out.println("unable to get input/output streams"); return; } net_input[client_counter]=input; net_output[client_counter]=output;

System.out.println(client_counter+ " connected! " + socket); (new reader(this,client_counter)).start(); client_counter++; } } //***********************************************************************

class reader extends Thread { ServerApp s; int index; String string,strings;

// new class created

public reader(ServerApp s,int index) { this.s=s; this.index=index; } //.............................................................. public void run() {

setPriority(MIN_PRIORITY);

try {

strings="1"; s.net_output[index].write(strings);

s.net_output[index].newLine();

s.net_output[index].flush();

}catch (Exception e) { e.printStackTrace(); }

while(true) { try { string=s.net_input[index].readLine();

} catch (Exception h){} if(string==null) break;

for(int i=0;i<s.client_counter;i++) // writing to all client output screen { try{ s.net_output[i].write(string);

s.net_output[i].newLine();

s.net_output[i].flush(); } catch(Exception m){} }

} if(-s.client_counter==0) System.exit(0); } }

Вам также может понравиться