Friday, September 24, 2010

BroadCastServer

//Client.java

import java.util.*;
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client implements ActionListener
{
JFrame f,f1;
JLabel l,l1,l2;
JTextField tf;
JTextArea ta,ta1,ta2;
JScrollPane js,js1,js2;
JButton b,b1,b2;
Socket s;
DataInputStream din;
DataOutputStream dout;
DataInputStream din1;
DataOutputStream dout1;
String st="",name,st1="",st2="";
int flag=0;
public Client()
{

f=new JFrame("LOGIN FORM");
f1=new JFrame("CHAT");

l=new JLabel("enter user name");
l1=new JLabel("sent items");
l2=new JLabel("wanna send?");
l.setBounds(50,50,100,30);

tf=new JTextField(100);
tf.setBounds(160,50,100,30);

b=new JButton("LOG IN");
b2=new JButton("LOG OUT");

b.setBounds(100,100,90,30);

f.getContentPane().add(l);
f.getContentPane().add(tf);
f.getContentPane().add(b);

ta=new JTextArea(250,60);
ta1=new JTextArea(250,160);
ta2=new JTextArea(250,160);

js=new JScrollPane(ta,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
js1=new JScrollPane(ta1,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
js2=new JScrollPane(ta2,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

js.setBounds(160,230,250,60);
js2.setBounds(450,50,100,160);
js1.setBounds(160,50,250,160);
l2.setBounds(40,230,80,30);
l1.setBounds(40,50,80,30);

b1=new JButton("SEND");
b1.setBounds(75,300,80,30);
b2.setBounds(170,300,120,30);

f1.getContentPane().add(b1);
f1.getContentPane().add(b2);
f1.getContentPane().add(l1);
f1.getContentPane().add(l2);
f1.getContentPane().add(js);
f1.getContentPane().add(js1);
f1.getContentPane().add(js2);

f1.getContentPane().setLayout(null);
f1.setSize(600,600);
f1.setVisible(false);

f.getContentPane().setLayout(null);
f.setSize(300,300);
f.setVisible(true);
b.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
try
{s=new Socket("localhost",8);
din=new DataInputStream(s.getInputStream());
dout=new DataOutputStream(s.getOutputStream());
my m=new my(din);
Thread t1=new Thread(m);
t1.start();
ta2.setText("");
}
catch(Exception e){
System.out.println(""+e);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b)//login
{
try
{
name=tf.getText();
f.setVisible(false);
f1.setVisible(true);
clientchat1();

}catch(Exception l){}
}
if(e.getSource()==b1)//send
{
try
{

clientchat();

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

if(e.getSource()==b2)//logout
{
try
{

clientchat2();
}
catch(Exception j){j.printStackTrace();}
}

}
public void clientchat2() throws IOException
{
dout.writeUTF("('"+name+" logged out"+")");
dout.flush();
System.exit(0);
}
public void clientchat1() throws IOException
{
String s1=name+" logged in";
dout.flush();
dout.writeUTF(s1);
dout.flush();
}

public void clientchat() throws IOException
{
String s1="";
s1=ta.getText();
dout.writeUTF("*"+name+"::"+s1);
dout.flush();
ta.setText("");
}
public static void main(String s[])
{
new Client();
}

class my implements Runnable
{
DataInputStream din;

public void run()
{
String st=" ";
String str[]=new String[40];
st2="";
int j=0;
do
{
try
{

st="";
st=din.readUTF();
System.out.println(st);
if(st.startsWith(""+"'"))
{
ta2.setText(st);
}
else
{
st1=st1+st+"\n";
}
ta1.setText(st1);


}
catch(Exception e){e.printStackTrace();}
}
while(st!=null);

}
my(DataInputStream din)
{
this.din=din;
}
}
}


//Server.java
import java.net.*;
import java.io.*;
import java.util.*;
public class Server
{
ArrayList al,al1;
ServerSocket ss;
Socket s;
int flag=0;
public Server()
{
try
{
al=new ArrayList();
al1=new ArrayList();
ss=new ServerSocket(8);
System.out.println("Server Started");
while(true)
{
s=ss.accept();
System.out.println("client started");
al.add(s);
Runnable r=new mythread(s,al);
Thread t=new Thread(r);
t.start();
}
}
catch(Exception e){}
}
public static void main(String s[])
{
new Server();
}
class mythread implements Runnable
{
Socket s;
ArrayList al;

mythread(Socket s,ArrayList al)
{
this.s=s;
this.al=al;
}
public void run()
{
String s1="";

try
{
DataInputStream din=new DataInputStream(s.getInputStream());
do
{
s1=din.readUTF();
System.out.println(s1);
if(s1.startsWith("*"))
{
System.out.println(s1);
}
else if(s1.startsWith("("))
{
char c1[]=new char[s1.length()];
s1.getChars(0,s1.length()-1,c1,0);
int h=0;
for(int i=0;i {
if(c1[i]==' ')
{
h=i;

break;
}
}

String s=new String(c1);
String stt=s.substring(2,h);
System.out.println(stt);
Object ob[]=al1.toArray();
String sr[]=new String[ob.length];
for(int j=0;j {
int y=0;
sr[j]=(String) ob[j];
char c2[]=new char[sr[j].length()];
sr[j].getChars(0,sr[j].length()-1,c2,0);
for(int i=0;i {
if(c2[i]==' ')
{
y=i;
break;
}
}
String sj=new String(c2);
String stt1=sj.substring(0,y);
System.out.println(stt1);
if(stt1.equals(stt))
{
System.out.println("gh");
al1.remove(j);
al.remove(j);
break;
}
}
String h1="'";

Object ob1[]=al1.toArray();
String sr1[]=new String[ob1.length];
for(int j=0;j {
sr1[j]=(String) ob1[j];
h1=h1+sr1[j]+"\n";
System.out.println("object "+h1);
}
telleveryone(h1);
}
else
{
String h="'";
al1.add(s1);
Object ob[]=al1.toArray();
String sr[]=new String[ob.length];
for(int j=0;j {
sr[j]=(String) ob[j];
h=h+sr[j]+"\n";
System.out.println("object "+h);
}
System.out.println(h);
telleveryone(h);
}
telleveryone(s1);
}
while(s1!=null);


}
catch(Exception e){e.printStackTrace();}
}
public void telleveryone(String s1)
{
Iterator i=al.iterator();
while(i.hasNext())
{
try
{
Socket sc=(Socket)i.next();
DataOutputStream dout=new DataOutputStream(sc.getOutputStream());
dout.writeUTF(s1);
dout.flush();

}

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

}

}
}

Download

0 comments:

Post a Comment