import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author ILLUMINATI
*/
class chatClient implements Runnable{
Socket SOCK;
ClientGUI GUI;
PrintWriter OUT;
Scanner sc;
chatClient(Socket SOCK, ClientGUI GUI) throws IOException{
this.SOCK = SOCK;
this.GUI = GUI;
}
@Override
public void run() {
try {
OUT = new PrintWriter(SOCK.getOutputStream());
sc = new Scanner(SOCK.getInputStream());
} catch (IOException ex) {
Logger.getLogger(chatClient.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Cannot set input and output streams");
}
OUT.println(GUI.getTextthname());
OUT.println(GUI.getTextthname());
OUT.println("Welcome");
OUT.flush();
String inp;
while(true){
if(sc.hasNext()){
inp = sc.nextLine();
System.out.println(inp);
GUI.setTextchat(inp + "\n");
}
}
}
public void sendMsg(String msg){
OUT.println(msg);
OUT.flush();
}
}
public class ClientGUI extends javax.swing.JFrame {
chatClient Client;
boolean isConnected;
int PORT;
String HOST;
Socket SOCK;
String username;
/**
* Creates new form ClientGUI
*/
public ClientGUI() {
initComponents();
// sendB.setEnabled(false);
sendB.setEnabled(false);
Nametf.setEnabled(false);
message.setEnabled(false);
chat.setEditable(false);
}
public String getTextthname(){
return thname.getText();
}
public void setTextchat(String msg){
chat.append(msg);
}
public void Connect() throws IOException{
SOCK = new Socket (HOST,PORT);
isConnected = true;
Client = new chatClient(SOCK,this);
Thread tClient = new Thread(Client);
tClient.start();
String msg = "You are connected to : " + HOST + ":" + PORT;
System.out.println(msg);
chat.append( msg + "\n");
}
public boolean checkConnected(){
if(SOCK.isConnected()){
return true;
} else {
isConnected = false;
return false;
}
}
/**
* 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")
// //GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
Nametf = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
Hosttf = new javax.swing.JTextField();
Porttf = new javax.swing.JTextField();
sendB = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
chat = new javax.swing.JTextArea();
jLabel5 = new javax.swing.JLabel();
logoutB = new javax.swing.JButton();
clearB = new javax.swing.JButton();
jLabel6 = new javax.swing.JLabel();
thname = new javax.swing.JTextField();
connect = new javax.swing.JButton();
message = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Name :");
jLabel2.setText("Message:");
jLabel3.setText("Server:");
jLabel4.setText("Port:");
Hosttf.setText("localhost");
Porttf.setText("1234");
sendB.setText("Send");
sendB.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sendBActionPerformed(evt);
}
});
chat.setColumns(20);
chat.setRows(5);
jScrollPane2.setViewportView(chat);
jLabel5.setText("Chat History");
logoutB.setText("Disconnect");
clearB.setText("Clear");
clearB.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearBActionPerformed(evt);
}
});
jLabel6.setText("Name:");
connect.setText("Connect");
connect.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
connectActionPerformed(evt);
}
});
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(30, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 694, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(18, 18, 18)
.addComponent(Hosttf, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(Porttf, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel6)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(thname, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(connect)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(clearB)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(logoutB))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(Nametf, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(message, javax.swing.GroupLayout.PREFERRED_SIZE, 387, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(sendB, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(Hosttf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4)
.addComponent(Porttf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(logoutB)
.addComponent(clearB)
.addComponent(jLabel6)
.addComponent(thname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(connect))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 296, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(13, 13, 13)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Nametf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2)
.addComponent(message, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(sendB))
.addGap(50, 50, 50))
);
pack();
}// //GEN-END:initComponents
private void sendBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendBActionPerformed
if(!checkConnected()){
chat.append("Connection Lost!! :(\n");
sendB.setEnabled(false);
Nametf.setEnabled(false);
message.setEnabled(false);
} else {
String msg = Nametf.getText() + "\n" + message.getText();
Client.sendMsg(msg);
chat.append("To " + Nametf.getText() + " : " + message.getText() + "\n");
message.setText("");
message.requestFocus();
}
}//GEN-LAST:event_sendBActionPerformed
private void connectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connectActionPerformed
if(!isConnected){
HOST = Hosttf.getText();
String temp_name = thname.getText();
if(temp_name == null || temp_name.equals("")){
chat.append("Improper username\n");
System.out.println("Inavalid username");
return;
}
try{
PORT = Integer.parseInt(Porttf.getText());
Connect();
sendB.setEnabled(true);
Nametf.setEnabled(true);
message.setEnabled(true);
this.getRootPane().setDefaultButton(sendB);
Nametf.requestFocus();
} catch (NumberFormatException e){
System.out.println("Invalid port no.");
chat.append("Improper port " + Porttf.getText() + "\n");
Porttf.requestFocus();
return;
} catch (IOException ex) {
System.out.println(ex);
chat.append("Not found " +HOST + ":" + PORT + "\n");
return;
}
}
}//GEN-LAST:event_connectActionPerformed
private void clearBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearBActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_clearBActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//
/* 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(ClientGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ClientGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ClientGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ClientGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ClientGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextField Hosttf;
private javax.swing.JTextField Nametf;
private javax.swing.JTextField Porttf;
private javax.swing.JTextArea chat;
private javax.swing.JButton clearB;
private javax.swing.JButton connect;
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.JScrollPane jScrollPane2;
private javax.swing.JButton logoutB;
private javax.swing.JTextField message;
private javax.swing.JButton sendB;
private javax.swing.JTextField thname;
// End of variables declaration//GEN-END:variables
}