Translate
Senin, 26 Agustus 2013
Multichatserver
import java.net.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.util.*;
class ClientInfo{
String name;
Socket cs;
public ClientInfo(String nm,Socket s)
{
name = nm;
cs = s;
}
}
public class multichatserver {
ServerSocket ss;
Socket s;
Hashtable list;
PrintStream chatLog;
Vector namevect;
public multichatserver()
{
list = new Hashtable();
namevect = new Vector();
try
{
chatLog = new PrintStream(new FileOutputStream("multichat.log"));
}catch(IOException e)
{
System.out.println("Error opening file");
System.exit(1);
}
try{
ss = new ServerSocket(2000);
while(true)
{
System.out.println("SERVER READY TO TAKE REQUESTS");
s = ss.accept();
new FileThread(s).start();
}
}catch(IOException ioe)
{
System.out.println("IOException Caught");
}
}
class FileThread extends Thread{
Socket fs;
BufferedReader br;
String name;
public FileThread(Socket s)
{
fs = s;
}
public void run()
{
try
{
InputStream is = fs.getInputStream();
OutputStream os = fs.getOutputStream();
br = new BufferedReader (new InputStreamReader(is));
while((name = br.readLine())!=null)
{
if(list.containsKey(name))
{
continue;
}
else
{
ClientInfo cinfo = new ClientInfo(name,fs);
chatLog.println(name+" Registered from Host:"+fs.getInetAddress().getHostName()+"and IP address:"+fs.getInetAddress().getHostAddress() );
list.put(name,cinfo);
Enumeration enumS = list.elements();
namevect.addElement(name);
while(enumS.hasMoreElements())
{
ClientInfo clinfo =(ClientInfo)enumS.nextElement();
System.out.println("client name"+clinfo.name);
new DataOutputStream(clinfo.cs.getOutputStream()).writeInt(1);
ObjectOutputStream obos = new ObjectOutputStream(clinfo.cs.getOutputStream());
obos.writeObject(namevect);
obos.flush();
}
new messageThread(name,fs).start();
System.out.println("Thread Started.");
break;
}
}
}
catch(IOException ioe)
{
System.out.println("IOException Caught");
}
}
}
class messageThread extends Thread{
Socket ms;
String name;
public messageThread(String nm,Socket s)
{
ms = s;
name = nm;
}
public void run()
{
InputStream is;
OutputStream os;
BufferedReader br;
String message;
try{
is = ms.getInputStream();
os = ms.getOutputStream();
br = new BufferedReader(new InputStreamReader(is));
while((message = br.readLine())!=null)
{
System.out.println("message sent from "+ name);
Enumeration en = list.elements();
while(en.hasMoreElements())
{
ClientInfo clinf = (ClientInfo)en.nextElement();
if(!clinf.name.equals(name))
{
new DataOutputStream(clinf.cs.getOutputStream()).writeInt(0);
//clinf.ps.println(name+": "+message);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(clinf.cs.getOutputStream()),true);
pw.println(name+": "+message);
}
//clinf.ps.println(name+": "+message);
}
}
System.out.println("out of while loop");
}catch(IOException ioe)
{
list.remove(name);
chatLog.println(name + "Quits.");
return;
}
}
}
public static void main(String args[])
{
multichatserver mcs = new multichatserver();
}
}
Multichatclient
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class multichatclient extends Frame implements ActionListener,KeyListener {
Label meslabel;
Vector namevect;
TextField mesg;
Button send,login,exit;
TextArea ta,usrlist;
Panel top,mid,bot;
String message,messg,usr,name;
InputStream is ;
OutputStream os ;
BufferedReader br ;
PrintStream ps ;
Socket s;
public multichatclient(String title)
{
super(title);
initComponents();
setLayout(new BorderLayout());
top = new Panel();
top.setLayout(new GridLayout(1,2));
bot = new Panel();
bot.setLayout(new GridLayout(1,3,5,5));
setSize(500,500);
setBackground(Color.gray);
setForeground(Color.black);
meslabel = new Label("PESAN",Label.RIGHT);
mesg = new TextField(20);
ta = new TextArea("Client: HALLO SEMUA!\n",50,50,TextArea.SCROLLBARS_BOTH);
usrlist = new TextArea("DAFTAR USER",50,20,TextArea.SCROLLBARS_BOTH);
send = new Button("KIRIM");
login = new Button("MASUK");
exit = new Button("KELUAR");
send.setEnabled(false);
top.add(meslabel);
top.add(mesg);
bot.add(login);
bot.add(send);
bot.add(exit);
add(top,BorderLayout.NORTH);
add(bot,BorderLayout.SOUTH);
add(ta,BorderLayout.CENTER);
add(usrlist,BorderLayout.EAST);
send.addActionListener(this);
mesg.addKeyListener(this);
login.addActionListener(this);
exit.addActionListener(this);
try
{
s = new Socket("192.168.30.10",2000);
is = s.getInputStream();
os = s.getOutputStream();
ps = new PrintStream(os);
} catch(IOException ie)
{
ie.printStackTrace();
}
}
public void actionPerformed(ActionEvent ae)
{
Button btn = (Button)ae.getSource();
byte reply = 0;
if(btn == login)
{
loginDialog ld = new loginDialog(this);
ld.show();
usr = ld.user;
//usrlist.append("\n"+usr);
//System.out.println("USER NAME:"+ usr);
ps.println(usr);
ps.flush();
login.setEnabled(false);
send.setEnabled(true);
new MessageReader().start();
}
if(btn == send)
{
String txt = mesg.getText();
ps.println(txt);
ps.flush();
ta.append(txt+"\n");
mesg.setText("");
}
if(btn == exit)
{
try
{
s.close();
}catch(IOException ioe)
{
}
System.exit(1);
}
}
public void keyPressed(KeyEvent ke)
{
}
public void keyReleased(KeyEvent ke)
{
}
public void keyTyped(KeyEvent ke)
{
}
public void initComponents()
{
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we)
{
setVisible(false);
}
});
}
class MessageReader extends Thread {
int check=0;
ObjectInputStream objis;
DataInputStream dis;
String msg;
public void run()
{
try
{
while(true)
{
dis = new DataInputStream(is);
check = dis.readInt();
if(check == 1)
{
objis = new ObjectInputStream(is);
namevect =(Vector)objis.readObject();
System.out.println("No of users in list:"+namevect.size());
Enumeration en = namevect.elements();
usrlist.setText("User List");
while(en.hasMoreElements())
{
name = (String)en.nextElement();
System.out.println("name added to list:"+name);
usrlist.append("\n"+name);
}
System.out.println("check: "+check);
}
else
{
br = new BufferedReader(new InputStreamReader(is));
msg = br.readLine();
ta.append(msg+"\n");
while((msg = br.readLine())!=null)
{
String mymsg = msg;
mymsg = mymsg.substring(4,mymsg.length()-1);
System.out.println("message received is:"+mymsg);
//mymsg = "hello";
JOptionPane.showMessageDialog(null,mymsg,"Message Received",JOptionPane.INFORMATION_MESSAGE);
ta.append(mymsg+"\n");
System.out.println("check: "+check);
}
}
}
}catch(Exception e)
{
System.out.println(e);
}
}
}
public static void main(String s[])
{
multichatclient mcc= new multichatclient("Messenger");
mcc.setVisible(true);
mcc.show();
}
}
class loginDialog extends Dialog implements ActionListener{
Label loginname;
TextField logintext;
Button okay;
Panel top,central;
String user;
loginDialog(Frame parent)
{
super(parent,"User Login",true);
setSize(400,100);
setResizable(false);
setFont(new Font("ComicSans",Font.BOLD|Font.ITALIC,15));
loginname = new Label("Enter Login:",Label.RIGHT);
logintext = new TextField(15);
okay = new Button("OK");
//okay.setBounds(10,10,20,20);
top = new Panel();
central = new Panel();
central.setLayout(new FlowLayout());
top.setLayout(new GridLayout(1,2,5,5));
top.add(loginname);
top.add(logintext);
central.add(okay);
add(central,BorderLayout.CENTER);
add(top,BorderLayout.NORTH);
okay.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
user = logintext.getText();
dispose();
}
}
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class multichatclient extends Frame implements ActionListener,KeyListener {
Label meslabel;
Vector namevect;
TextField mesg;
Button send,login,exit;
TextArea ta,usrlist;
Panel top,mid,bot;
String message,messg,usr,name;
InputStream is ;
OutputStream os ;
BufferedReader br ;
PrintStream ps ;
Socket s;
public multichatclient(String title)
{
super(title);
initComponents();
setLayout(new BorderLayout());
top = new Panel();
top.setLayout(new GridLayout(1,2));
bot = new Panel();
bot.setLayout(new GridLayout(1,3,5,5));
setSize(500,500);
setBackground(Color.gray);
setForeground(Color.black);
meslabel = new Label("PESAN",Label.RIGHT);
mesg = new TextField(20);
ta = new TextArea("Client: HALLO SEMUA!\n",50,50,TextArea.SCROLLBARS_BOTH);
usrlist = new TextArea("DAFTAR USER",50,20,TextArea.SCROLLBARS_BOTH);
send = new Button("KIRIM");
login = new Button("MASUK");
exit = new Button("KELUAR");
send.setEnabled(false);
top.add(meslabel);
top.add(mesg);
bot.add(login);
bot.add(send);
bot.add(exit);
add(top,BorderLayout.NORTH);
add(bot,BorderLayout.SOUTH);
add(ta,BorderLayout.CENTER);
add(usrlist,BorderLayout.EAST);
send.addActionListener(this);
mesg.addKeyListener(this);
login.addActionListener(this);
exit.addActionListener(this);
try
{
s = new Socket("192.168.30.10",2000);
is = s.getInputStream();
os = s.getOutputStream();
ps = new PrintStream(os);
} catch(IOException ie)
{
ie.printStackTrace();
}
}
public void actionPerformed(ActionEvent ae)
{
Button btn = (Button)ae.getSource();
byte reply = 0;
if(btn == login)
{
loginDialog ld = new loginDialog(this);
ld.show();
usr = ld.user;
//usrlist.append("\n"+usr);
//System.out.println("USER NAME:"+ usr);
ps.println(usr);
ps.flush();
login.setEnabled(false);
send.setEnabled(true);
new MessageReader().start();
}
if(btn == send)
{
String txt = mesg.getText();
ps.println(txt);
ps.flush();
ta.append(txt+"\n");
mesg.setText("");
}
if(btn == exit)
{
try
{
s.close();
}catch(IOException ioe)
{
}
System.exit(1);
}
}
public void keyPressed(KeyEvent ke)
{
}
public void keyReleased(KeyEvent ke)
{
}
public void keyTyped(KeyEvent ke)
{
}
public void initComponents()
{
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we)
{
setVisible(false);
}
});
}
class MessageReader extends Thread {
int check=0;
ObjectInputStream objis;
DataInputStream dis;
String msg;
public void run()
{
try
{
while(true)
{
dis = new DataInputStream(is);
check = dis.readInt();
if(check == 1)
{
objis = new ObjectInputStream(is);
namevect =(Vector)objis.readObject();
System.out.println("No of users in list:"+namevect.size());
Enumeration en = namevect.elements();
usrlist.setText("User List");
while(en.hasMoreElements())
{
name = (String)en.nextElement();
System.out.println("name added to list:"+name);
usrlist.append("\n"+name);
}
System.out.println("check: "+check);
}
else
{
br = new BufferedReader(new InputStreamReader(is));
msg = br.readLine();
ta.append(msg+"\n");
while((msg = br.readLine())!=null)
{
String mymsg = msg;
mymsg = mymsg.substring(4,mymsg.length()-1);
System.out.println("message received is:"+mymsg);
//mymsg = "hello";
JOptionPane.showMessageDialog(null,mymsg,"Message Received",JOptionPane.INFORMATION_MESSAGE);
ta.append(mymsg+"\n");
System.out.println("check: "+check);
}
}
}
}catch(Exception e)
{
System.out.println(e);
}
}
}
public static void main(String s[])
{
multichatclient mcc= new multichatclient("Messenger");
mcc.setVisible(true);
mcc.show();
}
}
class loginDialog extends Dialog implements ActionListener{
Label loginname;
TextField logintext;
Button okay;
Panel top,central;
String user;
loginDialog(Frame parent)
{
super(parent,"User Login",true);
setSize(400,100);
setResizable(false);
setFont(new Font("ComicSans",Font.BOLD|Font.ITALIC,15));
loginname = new Label("Enter Login:",Label.RIGHT);
logintext = new TextField(15);
okay = new Button("OK");
//okay.setBounds(10,10,20,20);
top = new Panel();
central = new Panel();
central.setLayout(new FlowLayout());
top.setLayout(new GridLayout(1,2,5,5));
top.add(loginname);
top.add(logintext);
central.add(okay);
add(central,BorderLayout.CENTER);
add(top,BorderLayout.NORTH);
okay.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
user = logintext.getText();
dispose();
}
}
Langganan:
Komentar (Atom)