Академический Документы
Профессиональный Документы
Культура Документы
********************************************************************************** ***********************/
// 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
// //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");
{ 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 {
msg.setVisible(true); //setVisible(true);
{ 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;
try {
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);
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);
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); } }
{ new openConnection(); }
//Server Application
import java.net.*; import java.io.*; import java.sql.*; import java.util.*; //================================================================== class ServerApp { ServerSocket s; Socket s1;
public static void main(String arg[]) // main function { new ServerApp(); main function } // constructor called in the
// Constructor initialised
try { s=new ServerSocket(6000); Port } catch(IOException e) { System.out.println("error in server socket"); return; } // handling errors // Socket created at the defined
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 }
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();
s.net_output[i].newLine();
} if(-s.client_counter==0) System.exit(0); } }