//Server.java
import java.net.*;
import java.io.*;
import java.util.*;
public class Server{
private ServerSocket ss;
private Socket socket;
Map<Socket,DataOutputStream> map=new HashMap<Socket,DataOutputStream>();
public static void main(String[] args)
{
try
{
int port=Integer.parseInt(args[0]);
new Server(port);
}
catch(NumberFormatException nfe)
{
System.out.println("Usage:> java Server <portno(int)>");
return;
}
catch(ArrayIndexOutOfBoundsException aioobe)
{
System.out.println("Usage:> java Server <portno(int)>");
return;
}
}
public Server(int port)
{
try{
listen(port);
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
private void listen(int port) throws IOException{
ss=new ServerSocket(port);
System.out.println("Server started");
while(true)
{
socket=ss.accept();
System.out.println("connected "+socket);
DataOutputStream dout=new DataOutputStream(socket.getOutputStream());
map.put(socket,dout);
new ServerThread(this,socket);
}
}
public void sendToAll(String message,Socket cur_soc)
{
synchronized(map){
Set<Socket> socket_set=map.keySet();
Iterator<Socket> itr=socket_set.iterator();
while(itr.hasNext())
{
try{
Socket s=itr.next();
if(s!=cur_soc)
{
DataOutputStream dout=map.get(s);
dout.writeUTF(message);
}
}
catch(IOException ioe){}
}
}
}
public void removeConnection(Socket soc)
{
synchronized(map)
{
map.remove(soc);
System.out.println("removed "+soc);
try
{
soc.close();
}
catch(IOException e)
{
}
}
}
}
//ServerThread.java
import java.net.*;
import java.io.*;
public class ServerThread extends Thread{
private Server server;
private Socket socket;
public ServerThread(Server server,Socket socket)
{
this.server=server;
this.socket=socket;
start();
}
public void run()
{
try{
DataInputStream din=new DataInputStream(socket.getInputStream());
while(true)
{
String message=din.readUTF();
server.sendToAll(message,socket);
}
}
catch(EOFException eof){}
catch(IOException ioe){}
finally{
server.removeConnection(socket);
}
}
}
// client.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Client1.java
*
* Created on May 17, 2010, 1:28:24 AM
*/
/**
*
* @author COM
*/
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class Client1 extends javax.swing.JFrame implements Runnable {
/** Creates new form Client1 */
public Client1() {
initComponents();
registerComponents();
}
public void run(){
try
{
while(true)
{
String msg=din.readUTF();
jconv_area.append(msg+"\n");
}
}
catch(IOException ioe){
System.out.println("IOError");
}
}
private void registerComponents(){
jlogin_btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(jlogin_field.getText().equals(""))
juser_info.setText("Please specify Name");
else{
try{
addr=new InetSocketAddress(InetAddress.getLocalHost().getHostName(),5000);
//wait while connection establishes
setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.WAIT_CURSOR));
socket.connect(addr);
din=new DataInputStream(socket.getInputStream());
dout=new DataOutputStream(socket.getOutputStream());
new Thread(Client1.this).start();
}
catch(UnknownHostException uhe){
JOptionPane.showMessageDialog(Client1.this,"Unknown Host!!!","Global-Chat",JOptionPane.INFORMATION_MESSAGE);
return;
}
catch(IOException ioe){
JOptionPane.showMessageDialog(Client1.this,"Connection refused!!!","Global-Chat",JOptionPane.INFORMATION_MESSAGE);
return;
}
finally{
setCursor(java.awt.Cursor.getDefaultCursor());
}
login_name=jlogin_field.getText();
juser_info.setText("Welcome "+login_name);
jlogin_btn.setEnabled(false);
jlogin_field.setEnabled(false);
jmsg_area.setEnabled(true);
jsend.setEnabled(true);
jdisconnect.setEnabled(true);
}
}
});
jdisconnect.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(ae.getActionCommand().equals("Really???"))
try{
socket.close();
Client1.this.setVisible(false);
System.exit(0);
}
catch(IOException ioe){}
if(ae.getActionCommand().equals("Disconnect"))
jdisconnect.setText("Really???");
}
});
jmsg_area.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent ke){
try{
if(ke.getKeyCode()==KeyEvent.VK_ENTER){
write();
}
}
catch(IOException ioe){}
}
});
jsend.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
try{
write();
}
catch(IOException ioe){}
}
});
}
private void write() throws IOException{
if(jdisconnect.getText().equals("Really???"))
jdisconnect.setText("Disconnect");
String msg=jmsg_area.getText().trim();
System.out.print(msg);
dout.writeUTF(login_name+":"+msg);
jconv_area.append("You:"+msg+"\n");
jmsg_area.setText("");
jmsg_area.moveCaretPosition(0);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
String look="com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
try{
UIManager.setLookAndFeel(look);
SwingUtilities.updateComponentTreeUI(this);
}
catch(Exception e){}
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jconv_area = new javax.swing.JTextArea();
jPanel3 = new javax.swing.JPanel();
jLabel6 = new javax.swing.JLabel();
jlogin_field = new javax.swing.JTextField();
jlogin_btn = new javax.swing.JButton();
juser_info = new javax.swing.JLabel();
jScrollPane2 = new javax.swing.JScrollPane();
jmsg_area = new javax.swing.JTextArea();
jmsg_area.setEnabled(false);
jdisconnect = new javax.swing.JButton();
jdisconnect.setEnabled(false);
jsend = new javax.swing.JButton();
jsend.setEnabled(false);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Global-Chat");
setResizable(false);
jLabel1.setFont(new java.awt.Font("Kristen ITC", 1, 36)); // NOI18N
jLabel1.setForeground(new java.awt.Color(255, 51, 102));
jLabel1.setText("G");
jPanel1.add(jLabel1);
jLabel2.setFont(new java.awt.Font("Kristen ITC", 1, 36)); // NOI18N
jLabel2.setForeground(new java.awt.Color(11, 187, 59));
jLabel2.setText("LO");
jPanel1.add(jLabel2);
jLabel3.setFont(new java.awt.Font("Kristen ITC", 1, 36)); // NOI18N
jLabel3.setForeground(new java.awt.Color(50, 8, 241));
jLabel3.setText("BA");
jPanel1.add(jLabel3);
jLabel4.setFont(new java.awt.Font("Kristen ITC", 1, 36)); // NOI18N
jLabel4.setForeground(new java.awt.Color(249, 11, 237));
jLabel4.setText("L");
jPanel1.add(jLabel4);
jLabel5.setFont(new java.awt.Font("Tempus Sans ITC", 1, 24)); // NOI18N
jLabel5.setForeground(java.awt.Color.orange);
jLabel5.setText("C H A T");
jPanel1.add(jLabel5);
jconv_area.setColumns(20);
jconv_area.setEditable(false);
jconv_area.setFont(new java.awt.Font("Monospaced", 0, 14)); // NOI18N
jconv_area.setForeground(new java.awt.Color(255, 102, 102));
jconv_area.setRows(5);
jScrollPane1.setViewportView(jconv_area);
jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jLabel6.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
jLabel6.setText("Login Name");
jlogin_btn.setText("Login");
juser_info.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
juser_info.setText("Login to Chat");
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(8, 8, 8)
.addComponent(jlogin_field, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE))
.addGroup(jPanel3Layout.createSequentialGroup()
.addGap(54, 54, 54)
.addComponent(jlogin_btn))
.addComponent(juser_info, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE))
.addContainerGap())
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addGap(37, 37, 37)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jlogin_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6))
.addGap(18, 18, 18)
.addComponent(jlogin_btn)
.addGap(26, 26, 26)
.addComponent(juser_info)
.addContainerGap(234, Short.MAX_VALUE))
);
jmsg_area.setColumns(20);
jmsg_area.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
jmsg_area.setForeground(new java.awt.Color(6, 123, 252));
jmsg_area.setLineWrap(true);
jmsg_area.setRows(5);
jmsg_area.setWrapStyleWord(true);
jmsg_area.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jmsg_area.setMargin(new java.awt.Insets(5, 10, 2, 2));
jScrollPane2.setViewportView(jmsg_area);
jdisconnect.setText("Disconnect");
jsend.setText("Send");
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 409, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jdisconnect)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 444, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jsend, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)))
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE))
.addGap(20, 20, 20)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jsend, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jdisconnect, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 624, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 490, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Client1().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea jconv_area;
private javax.swing.JButton jdisconnect;
private javax.swing.JButton jlogin_btn;
private javax.swing.JTextField jlogin_field;
private javax.swing.JTextArea jmsg_area;
private javax.swing.JButton jsend;
private javax.swing.JLabel juser_info;
private Socket socket=new Socket();
private InetSocketAddress addr;
private String login_name;
private DataInputStream din;
private DataOutputStream dout;
// End of variables declaration//GEN-END:variables
}