=Norm;150947]
//ChatServer:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ChatServer implements Runnable
{
ServerSocket chatSer;
Socket chatServer;
BufferedReader br=null;
BufferedWriter bw=null;
NewFrame chatServerObj;
public void startChatServer( String d1) throws Exception
{
try
{
// chatServerObj=obj1;
System.out.println("Server in accept Mode" + d1 );
Integer port1=Integer.parseInt(d1);
chatSer=new ServerSocket(port1);
chatServer = chatSer.accept();
System.out.println("Server Connected to Client");
br=new BufferedReader(new InputStreamReader(chatServer.getInputStream()));
bw=new BufferedWriter(new OutputStreamWriter(chatServer.getOutputStream()));
bw.write("Hello...welcome to Chat..Lets Play");
bw.newLine();
bw.flush();
Thread th=new Thread(this);
th.start();
//System.out.println("Thred for client is created.. for read..");
//System.out.println("\n\nClient is connected..: IP : ");
}
catch(Exception e)
{
//str="Network Connection Problem..";
//JOptionPane.showConfirmDialog(F1, str + " " + e.getMessage());
}
}
public void sendData(String data) throws Exception
{
try
{
System.out.print(" ACCEPT Data "+ data);
bw.write(data);
bw.newLine();
bw.flush();
}
catch(Exception e)
{
System.out.print("Error Data "+ e.getMessage());
}
}
//thread for reading the data...!!
public void run()
{
String data;
try
{
while(true)
{
data=br.readLine();
System.out.println("Server side Data : "+data);
handleDataRead(data);
}
}
catch (Exception e)
{
System.out.print("In read Server.."+e.getMessage());
}
}
public void handleDataRead(String data) throws Exception
{
try
{
System.out.println("In handle data read - Server:"+data);
chatServerObj.chatMessage_1(data);
}
catch(Exception e)
{}
}
public void dataPass(String data) throws Exception
{
try
{
//will be called whenever the data need to be passed between cleint and server...!!
bw.write(data);
bw.newLine();
bw.flush();
}
catch(Exception e)
{
}
}
}
//ChatClient:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
//package justchat;
import java.awt.Color;
import java.util.Random;
import java.util.Collections;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.net.Socket.*;
public class ChatClient implements Runnable
{
Socket chatClient;
BufferedReader br=null;
BufferedWriter bw=null;
NewFrame chatClientObj;
public void startConnectToServer(String d1) throws Exception
{
try
{
//chatClientObj=obj1;
Integer port1=Integer.parseInt(d1);
chatClient=new Socket("localhost",port1);
br=new BufferedReader(new InputStreamReader(chatClient.getInputStream()));
bw=new BufferedWriter(new OutputStreamWriter(chatClient.getOutputStream()));
bw.write("Hello...welcome to Bingo V1.0..Lets Play");
bw.newLine();
bw.flush();
Thread th=new Thread(this);
th.start();
//System.out.println("Thred for client is created.. for read..");
//System.out.println("\n\nClient is connected..: IP : ");
}
catch(Exception e)
{
//str="Network Connection Problem..";
//JOptionPane.showConfirmDialog(F1, str + " " + e.getMessage());
}
}
public void sendData(String data) throws Exception
{
try
{
bw.write(data);
bw.newLine();
bw.flush();
}
catch(Exception e)
{
//System.out.print("Error Data "+ e.getMessage());
}
}
//thread for reading the data...!!
public void run()
{
String data;
try
{
while(true)
{
data=br.readLine();
System.out.println("Client Side data : "+data);
handleDataRead(data);
}
}
catch (Exception e)
{
System.out.print("In chat Server.."+e.getMessage());
}
}
public void handleDataRead(String data) throws Exception
{
try
{
System.out.println("In handle data read - Client :"+data);
String dataCopy=data;
System.out.println("Data Copy - Client :"+dataCopy);
//dataPass(dataCopy);
chatClientObj.chatMessage_1(data);
}
catch(Exception e)
{
//System.out.println(e.pr());
}
}
public void dataPass(String data) throws Exception
{
try
{
System.out.println("Data Pass - Client :"+data);
//will be called whenever the data need to be passed between cleint and server...!!
//bw.write(data);
//bw.newLine();
//bw.flush();
}
catch(Exception e)
{
}
}
}
//GUI(CHAT DESIGN) class name: NewFrame
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.Border;
//import GUIchat.ActionHandler;
public class NewFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JButton but_start,but_send, but_connect;
JLabel lab_port;
JTextField txt_port;
JTextArea txt_chat,txt_send;
//client server objects..
// ChatServer serverobj=new ChatServer();
// ChatClient clientObj1=new ChatClient();
int flagWho;
public void chatMessage_1(String data)throws Exception
{
System.out.print("in Chat Message..."+ data);
try
{
System.out.println("in Chat Message..."+ data);
txt_send.getText();
}
catch(Exception e)
{}
}
NewFrame()
{
ActionListener actionHandler = new ActionHandler();
setTitle("CHAT WINDOW");
JPanel panel = new JPanel();
getContentPane().add(panel);
panel.setLayout(null);
panel.setToolTipText("A Panel container");
lab_port = new JLabel("port NO:");
lab_port.setBounds(10, 10, 90, 30);
but_connect = new JButton("Connect Server");
but_connect.setBounds(170, 10, 150, 30);
but_connect.addActionListener(actionHandler);
but_start = new JButton("Start Server");
but_start.setBounds(320, 10, 150, 30);
but_start.addActionListener(actionHandler);
// btn1.setToolTipText("Client port number");
txt_chat=new JTextArea();
Border border = BorderFactory.createLineBorder(Color.BLACK);
txt_chat.setBounds(10,50,400,300);
txt_chat.setBorder(border);
txt_send=new JTextArea();
Border border1 = BorderFactory.createLineBorder(Color.DARK_GRAY);
txt_send.setBounds(10,380,400,60);
txt_send.setBorder(border1);
but_send = new JButton(" SEND ");
but_send.setBounds(430,380,90,60);
but_send.addActionListener(actionHandler);
txt_port=new JTextField();
txt_port.setBounds(65,10,70,30);
panel.add(txt_send);
panel.add(txt_chat);
panel.add(txt_port);
panel.add(but_connect);
panel.add(but_start);
panel.add(but_send);
panel.add(lab_port);
setSize(700,500);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private class ActionHandler implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
Object source = evt.getSource();
ChatServer serverobj=new ChatServer();
ChatClient clientObj1=new ChatClient();
//server Code
if (source == but_start)
{
String d1=txt_port.getText();
System.out.println(txt_port.getText());
flagWho=1;
try
{
serverobj.startChatServer(d1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
//client connecting to server
if (source == but_connect)
{
System.out.println("Connecting in Progress");
try
{
flagWho=0;
String d1=txt_port.getText();
clientObj1.startConnectToServer(d1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//send data
if (source == but_send)
{
System.out.println("Send Data...");
//ChatClient clientObj1=new ChatClient();
try
{
if(flagWho==1)
{
//server send data
serverobj.sendData(txt_send.getText().toString());
}
else
{
//client send data..
clientObj1.sendData(txt_send.getText().toString());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//Method for Display the chat Data..
public static void main(String[]args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
NewFrame c1=new NewFrame();
c1.setVisible(true);
}
});
}
}