You are on page 1of 23

// 1

package client;

import data.Data;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class Client extends javax.swing.JFrame {

public Client() {
initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();


jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
txt = new javax.swing.JTextArea();
jButton2 = new javax.swing.JButton();
txtName = new javax.swing.JTextField();
jComboBox1 = new javax.swing.JComboBox<>();
txtIp = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setFont(new java.awt.Font("sansserif", 1, 18)); // NOI18N


jLabel1.setText("Client");

jButton1.setText("Auto Connect");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

txt.setEditable(false);
txt.setColumns(20);
txt.setRows(5);
jScrollPane1.setViewportView(txt);

jButton2.setText("Send File");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

txtName.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtNameActionPerformed(evt);
}
});
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Image",
"File" }));

txtIp.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtIpActionPerformed(evt);
}
});

jLabel2.setText("IP");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());


getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1))
.addComponent(jScrollPane1)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addGap(18, 18, 18)
.addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 306,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtIp, javax.swing.GroupLayout.PREFERRED_SIZE, 135,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(0, 26, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 210,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2)
.addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtIp, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addGap(0, 28, Short.MAX_VALUE))
);

pack();
setLocationRelativeTo(null);
}// </editor-fold>

private Socket socket;


private ObjectOutputStream out;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
socket = new Socket(txtIp.getText().trim(), 1001);
txt.append("Connect success .\n");
out = new ObjectOutputStream(socket.getOutputStream());
Data data = new Data();
data.setStatus("new");
data.setName("Faria");
out.writeObject(data);
out.flush();
} catch (Exception e) {

}
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {


try {
JFileChooser ch = new JFileChooser();
int c = ch.showOpenDialog(this);
if (c == JFileChooser.APPROVE_OPTION) {
File f = ch.getSelectedFile();
FileInputStream in = new FileInputStream(f);
byte b[] = new byte[in.available()];
in.read(b);
Data data = new Data();
data.setStatus(jComboBox1.getSelectedItem() + "");
data.setName(txtName.getText().trim());
data.setFile(b);
out.writeObject(data);
out.flush();
txt.append("send 1 file /n");
}
} catch (Exception e) {

}
// TODO add your handling code here:
}

private void txtNameActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
}

private void txtIpActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Client().setVisible(true);
}
});
}

// Variables declaration - do not modify


private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JComboBox<String> jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea txt;
private javax.swing.JTextField txtIp;
private javax.swing.JTextField txtName;
// End of variables declaration
}
//2

package server;

import data.Data;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class Server extends javax.swing.JFrame {

public Server() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();


jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
txt = new javax.swing.JTextArea();
jScrollPane2 = new javax.swing.JScrollPane();
list = new javax.swing.JList<>();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setFont(new java.awt.Font("sansserif", 1, 18)); // NOI18N


jLabel1.setText("Server");

jButton1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N


jButton1.setForeground(new java.awt.Color(255, 0, 51));
jButton1.setText("Start Server");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

txt.setEditable(false);
txt.setColumns(20);
txt.setRows(5);
jScrollPane1.setViewportView(txt);

list.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
listMouseClicked(evt);
}
});
jScrollPane2.setViewportView(list);

jButton2.setText("Open");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jButton3.setText("Save");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

jLabel2.setText("Double click below to view file");

jLabel3.setText("Chat room activities");

javax.swing.GroupLayout layout = new


javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignmen
t.LEADING)
.addComponent(jLabel1)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE,
83, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 227,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addGap(32, 32, 32)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignmen
t.LEADING)
.addComponent(jLabel2)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignm
ent.TRAILING)
.addComponent(jButton3,
javax.swing.GroupLayout.PREFERRED_SIZE, 85,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jScrollPane2,
javax.swing.GroupLayout.PREFERRED_SIZE, 161,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jButton1))
.addGap(0, 12, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignmen
t.BASELINE)
.addComponent(jLabel1)
.addComponent(jButton1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELA
TED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignmen
t.BASELINE)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATE
D)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignmen
t.TRAILING)
.addComponent(jScrollPane2,
javax.swing.GroupLayout.PREFERRED_SIZE, 253,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 253,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATE
D, 25, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignmen
t.BASELINE)
.addComponent(jButton3)
.addComponent(jButton2))
.addContainerGap())
);

pack();
setLocationRelativeTo(null);
}// </editor-fold>

private ServerSocket serverSocket;


private ObjectOutputStream objectOutputStream;
private ObjectInputStream objectInputStream;
private DefaultListModel defaultListModel = new DefaultListModel();
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
list.setModel(defaultListModel);
new Thread(new Runnable() {
@Override
public void run() {
try {
serverSocket = new ServerSocket(1001);
txt.append("Server starting \n");
Socket s = serverSocket.accept();
objectInputStream = new ObjectInputStream(s.getInputStream());
Data data = (Data) objectInputStream.readObject();
String name = data.getName();
txt.append(" " + name + " has been connected \n");
while (true) {
data = (Data) objectInputStream.readObject();
defaultListModel.addElement(data);
txt.append("1 file added \n");
}
} catch (Exception e) {

}
}).start();
}

private void listMouseClicked(java.awt.event.MouseEvent evt) {


if (evt.getClickCount() == 2) {
if (!list.isSelectionEmpty()) {
if (SwingUtilities.isLeftMouseButton(evt)) {
open();
} else if (SwingUtilities.isRightMouseButton(evt)) {
save();
}

}
}
// TODO add your handling code here:
}

private void open() {


Data data = (Data) defaultListModel.getElementAt(list.getSelectedIndex());
if (data.getStatus().equals("Image")) {
ShowImage obj = new ShowImage(this, true);
ImageIcon icon = new ImageIcon(data.getFile());
obj.set(icon);
obj.setVisible(true);
}
}

private void save() {


Data data = (Data) defaultListModel.getElementAt(list.getSelectedIndex());
JFileChooser ch = new JFileChooser();
int c = ch.showSaveDialog(this);
if (c == JFileChooser.APPROVE_OPTION) {
try {
FileOutputStream out = new FileOutputStream(ch.getSelectedFile());
out.write(data.getFile());
out.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e, "Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
if (!list.isSelectionEmpty()) {
save();
}
// TODO add your handling code here:
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {


if (!list.isSelectionEmpty()) {
open();
}
// TODO add your handling code here:
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code
(optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look
and feel.
* For details see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(Server.class.getName()).log(java.util.logging.Lev
el.SEVERE, null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(Server.class.getName()).log(java.util.logging.Lev
el.SEVERE, null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(Server.class.getName()).log(java.util.logging.Lev
el.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(Server.class.getName()).log(java.util.logging.Lev
el.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>

/* Create and display the form */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Server().setVisible(true);
}
});
}

// Variables declaration - do not modify


private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JList<String> list;
private javax.swing.JTextArea txt;
// End of variables declaration
}
// 3

package data;

import java.io.Serializable;
import javax.swing.ImageIcon;

public class Data implements Serializable {

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getStatus() {


return status;
}

public void setStatus(String status) {


this.status = status;
}
public ImageIcon getImage() {
return image;
}

public void setImage(ImageIcon image) {


this.image = image;
}

@Override
public String toString() {
return name;
}

public byte[] getFile() {


return file;
}

public void setFile(byte[] file) {


this.file = file;
}

private String status;


private ImageIcon image;
private byte[] file;
private String name;
}
// 4

package server;

import javax.swing.ImageIcon;

public class ShowImage extends javax.swing.JDialog {

/**
* Creates new form ShowImage
*/
public ShowImage(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
}

public void set(ImageIcon icon) {


lb.setIcon(icon);
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
lb = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

lb.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

javax.swing.GroupLayout layout = new


javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lb, javax.swing.GroupLayout.DEFAULT_SIZE, 350,
Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lb, javax.swing.GroupLayout.DEFAULT_SIZE, 231,
Short.MAX_VALUE)
);

pack();
setLocationRelativeTo(null);
}// </editor-fold>

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code
(optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look
and feel.
* For details see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(ShowImage.class.getName()).log(java.util.loggin
g.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(ShowImage.class.getName()).log(java.util.loggin
g.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(ShowImage.class.getName()).log(java.util.loggin
g.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(ShowImage.class.getName()).log(java.util.loggin
g.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>

/* Create and display the dialog */


java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
ShowImage dialog = new ShowImage(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}

// Variables declaration - do not modify


private javax.swing.JLabel lb;
// End of variables declaration
}

You might also like