dcsimg
August 17, 2018
Hot Topics:

User Code: ChatKaro

  • June 13, 2001
  • By S. Balasubramanian
  • Send Email »
  • More Articles »

Tips to run this:

Step 1. Open command prompt and run 'java chatkaro121'.

Step 2. Open another prompt and run 'java chatkaro121 127.0.0.1 (or IP address of the server).


*/	


//  Program Starts...

import java.io.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import java.awt.Button.*;
import javax.swing.*;
public class chatkaro121 extends Panel 
{
Socket sock;
TextArea rtext;
ImageIcon i1,i2;
Frame frame;
private JLabel label;
private int port=8080;
private TextArea stext;
JButton send,exit;
private DataOutputStream remoteOut;
public static void main(String args[])

{
	Frame f=new Frame("Just Wait...");
	f.setCursor(Frame.HAND_CURSOR);
	String s=null;
	if(args.length>0)
		s=args[0];
	chatkaro121 chat=new chatkaro121(f);
	f.add("Center",chat);
	f.setSize(800,600);
	f.show();
	if(s==null)
		chat.server();
	else
		chat.client(s);
}

public chatkaro121(Frame f)
{
frame =f;
frame.addWindowListener(new WindowExitHandler());
label=new JLabel("msg to Send : ");
add(label);
stext=new TextArea(15,30);
add(stext);
label=new JLabel("msg received : ");
add(label);
rtext=new TextArea(15,30);
add(rtext);
ImageIcon i1=new ImageIcon("click.gif");
send=new JButton("Send",i1);
send.setToolTipText("Send");
send.setMnemonic('s');
add(send);
send.addActionListener(new TextActionHandler());

ImageIcon i2=new ImageIcon("doorin2.gif");
exit=new JButton("eXit",i2);
exit.setToolTipText("eXit");
exit.setMnemonic('x');
add(exit);
exit.addActionListener(new EXIT());
}
void server()
{
ServerSocket k=null;
try
{
InetAddress addr=InetAddress.getByName(null);
message("Waiting for connection : " + addr.getHostName() + "port : " + port);
k =new ServerSocket(port,1);
sock=k.accept();
message(" CHATKARO-By Balu. -Welcome " + sock.getInetAddress().getHostName());
remoteOut=new DataOutputStream(sock.getOutputStream());
new chatkaro121Receive(this).start();
}
catch(IOException e)
{
message(e.getMessage() + " : Failed to connect to client.");
}
finally
{
if(k !=null)
{
	try
	{
	k.close();
	}
	catch(IOException x)
	{}
}
}
}

void client(String serverName)
{
try
{
if(serverName.equals("local"))
serverName=null;
InetAddress addr=InetAddress.getByName(serverName);
sock =new Socket(addr.getHostName(),port);
remoteOut=new DataOutputStream(sock.getOutputStream());
message("Chat@Balunet : FTR-(One-to-One)-Connected to server " + addr.getHostName() + " on port " + sock.getPort());
new chatkaro121Receive(this).start();
}
catch(IOException e)
{
message(e.getMessage() + " : Failed to connect.");
}
}
void message(String s) 
{
frame.setTitle(s);
}
protected void finalize() throws Throwable
{
try
{
if(remoteOut!=null)
remoteOut.close();
if(sock!=null)
sock.close();
}
catch(IOException x)
{
}
super.finalize();
}
class WindowExitHandler extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
Window w=e.getWindow();
w.setVisible(false);
w.dispose();
System.exit(0);
}
}

class EXIT implements ActionListener
 {
   public void actionPerformed(ActionEvent e)
  {
   if((JOptionPane.showConfirmDialog(new Frame(),"Are You Sure to close the Session?"))==JOptionPane.YES_OPTION)              
              System.exit(0);
    	
  }
 }
class TextActionHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
remoteOut.writeUTF(stext.getText());
JOptionPane.showMessageDialog(new Frame(),"Your msg has been sent ","Confirmation",0);
stext.setText("");
}
catch(IOException x)
{
message(x.getMessage() + " : Connection to peer lost .");
}
}
}
}
class chatkaro121Receive extends Thread
{
private chatkaro121 chat;
private DataInputStream remoteIn;
private boolean listening=true;
public chatkaro121Receive(chatkaro121 chat)
{
this.chat=chat;
}
public synchronized void run()
{
String s;
try
{
remoteIn=new DataInputStream(chat.sock.getInputStream());
while(listening)
{
s=remoteIn.readUTF();
chat.rtext.setText(s);
}
}
catch(IOException e)
{
chat.message(e.getMessage()+" : Disconnection.");
}
finally
{
try
{
if(remoteIn!=null)
remoteIn.close();
}
catch(IOException  x)
{
}}}}

Downloads

Download source file = 4K

Dedication: To my Brothers and Sisters.

This code was contributed by S. Balasubramanian.

[To contribute a code article to Gamelan, please contact kmurphy@internet.com.]






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.

Sitemap

Thanks for your registration, follow us on our social networks to keep up-to-date