2 Replies - 2506 Views - Last Post: 27 January 2011 - 04:50 PM Rate Topic: -----

#1 parita2  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 4
  • Joined: 18-January 11

chat application in java applet

Posted 27 January 2011 - 12:10 AM

sir i make chat application in java applet it is working on my computer.
but how i connect another computer for chat if i send message from my computer as client how server know that i send message. please help me.

server application/////**************************
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
public class serverapp extends JApplet implements ActionListener,Runnable

{
	// Basic Components
	JPanel panel = new JPanel();
	List list = new List(10);
	JTextField textfield = new JTextField();
	JButton button = new JButton();
	Font ver = new Font("Verdana",1,12);
	Font ver2 = new Font("Verdana",1,14);
	
	// Advanced Components
	String feel[] = {"I Feel ...","Sad","Happy","Angry","Love","Cool","Sorry","Thankful"};
	JComboBox feelings = new JComboBox(feel);
	JButton sendfeel = new JButton();
	JButton exit = new JButton();
	JLabel abuse = new JLabel("PLEASE DO NOT ABUSE ...........");
	
	// All Strings
	String name = "Mr X";
	String name2;
	String texta;
	String feels;
	String send;
	String recive;
	
	// Connectivity
	ServerSocket server;
	BufferedReader fromServername;
	PrintStream toServername;
	BufferedReader fromServermsg;
	PrintStream toServermsg;
	BufferedReader fromServermsg2;
	PrintStream toServermsg2;
	Socket client;
	
	// Threads
	Thread t1 = new Thread(this);
	Messages msg;
	Thread t2 = new Thread(msg);
	
	public void init()
	{
		getContentPane().add(panel);
		panel.setLayout(null);
		panel.setBackground(new Color(242,167,0));
		
		try
		{
		server = new ServerSocket(1001);
		}
		catch(Exception e)
		{
			System.out.println(e);
		}
		
		name = JOptionPane.showInputDialog("Enter You Name");
		if ((name==null) || name.equals(""))
		{
			name = "Mr. X";
			System.out.println(name);
		}
		
		panel.add(list);
		list.setBounds(5,5,450,250);
		list.setFont(ver);
		list.add("Chat Applet By Neville Mehta - neville.m@usa.net");
		list.add(" ");
		
		panel.add(textfield);
		textfield.setBounds(5,265,450,25);
		textfield.setFont(ver);
		textfield.setText("Type The Text Here");
		
		panel.add(button);
		button.setBounds(5,300,450,25);
		button.setText(name + " Says");
		button.setFont(ver);
		button.addActionListener(this);
		
		panel.add(feelings);
		feelings.setBounds(5,335,120,25);
		feelings.setFont(ver);
		panel.add(sendfeel);
		sendfeel.setBounds(135,335,150,25);
		sendfeel.setFont(ver);
		sendfeel.setText("Send Feelings");
		sendfeel.addActionListener(this);
		
		panel.add(exit);
		exit.setBounds(385,335,70,25);
		exit.setFont(ver);
		exit.setText("Exit");
		exit.addActionListener(this);
		
		//panel.add(abuse);
		//abuse.setBounds(5,370,450,25);
		//abuse.setFont(ver2);
		//abuse.setForeground(Color.blue);
		
		t1.start();
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == button)
		{
			texta = textfield.getText();
			
			
			
			list.add(name + " >> " + texta);
			
			
			try
			{
				toServername.println(texta);
				System.out.println("Sent");
			}
			catch(Exception yu)
			{System.out.println(yu);}
			
		}
		
		if (e.getSource() == exit)
		{
			System.exit(0);
		}
		
		if (e.getSource() == sendfeel)
		{
			if (feelings.getSelectedItem().equals("I Feel ..."))
			{
				return;
			}
			
			feels = String.valueOf(feelings.getSelectedItem());
			list.add(name + " >> " + name + " feels " + feels);
			
			try
			{
				toServername.println(name + " feels " + feels);
				System.out.println("Sent");
			}
			catch(Exception yu)
			{System.out.println(yu);}
		}
		
	}
	
	public void run()
	{
		
		while (true)
		{
			try
			{
			client = server.accept();
			Name fromname = new Name(client);
			msg = new Messages(client);
			msg.run();
			
			}
			catch(Exception er)
			{
				System.out.println(er);
			}
		}
		
	}
	
	public class Name extends Thread
	{
		
		public Name(Socket client)
		{
			try
			{
			fromServername = new BufferedReader(new InputStreamReader(client.getInputStream()));
			toServername = new PrintStream(client.getOutputStream());
			
			// Recive Name
			{
				name2 = fromServername.readLine();
				list.add(name2 + " Enters Chat");
				list.add(" ");
			}
		
			// Send Name
			{
				toServername.println(name);
			}
			
			
			
			}
			catch(Exception e3)
			{
				System.out.println(e3);
			}
		}
		
		public void run()
		{
			
		}
	}
	
	public class Messages extends Thread
	{
		
		public Messages(Socket client)
		{
			try
			{
				fromServermsg = new BufferedReader(new InputStreamReader(client.getInputStream()));
				toServermsg = new PrintStream(client.getOutputStream());
			}
			catch (Exception e45)
			{
				System.out.println(e45);
			}
		}
		
		public void run()
		{
			while (true)
			{
				try
				{
				
				// Recive Messages
				{
					recive = fromServermsg.readLine();
					list.add(name2 + " >> " + recive);
					System.out.println("Reciving Msgs");
				}
				
				// Send Messages
				
				}
				catch(Exception e2)
				{
					System.out.println(e2);
				}
			}
		}
	
	}
	
	
}
//<applet code="serverapp.class" height=500 width=500></applet>



client application **************************
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
public class Clientapp extends JApplet implements ActionListener,Runnable
{
	// Basic Components
	JPanel panel = new JPanel();
	List list = new List(10);
	JTextField textfield = new JTextField();
	JButton button = new JButton();
	Font ver = new Font("Verdana",1,12);
	Font ver2 = new Font("Verdana",1,14);
	
	// Advanced Components
	String feel[] = {"I Feel ...","Sad","Happy","Angry","Love","Cool","Sorry","Thankful"};
	JComboBox feelings = new JComboBox(feel);
	JButton sendfeel = new JButton();
	JButton exit = new JButton();
	JLabel abuse = new JLabel("PLEASE DO NOT ABUSE ...........");
		
	
	// All Strings
	String name = "Mr Y";
	String name2;
	String texta;
	String feels;
	String send;
	String recive;
	
	// Connectivity
	Socket client;
	BufferedReader fromServername;
	PrintStream toServername;
	BufferedReader fromServermsg;
	PrintStream toServermsg;
	
	// Threads
	Thread t1 = new Thread(this);
	Messages msg = new Messages();
	Thread t2 = new Thread(msg);
	
	public void init()
	{
		getContentPane().add(panel);
		panel.setLayout(null);
		panel.setBackground(new Color(242,167,0));
		
		try
		{
		client = new Socket("127.0.0.1",1001);
		}
		catch(Exception e)
		{
			System.out.println(e);
		}
		
		name = JOptionPane.showInputDialog("Enter You Name");
		if ((name==null) || name.equals(""))
		{
			name = "Mr. Y";
			System.out.println(name);
		}
		
		panel.add(list);
		list.setBounds(5,5,450,250);
		list.setFont(ver);
		list.add("Chat Applet By Neville Mehta - neville.m@usa.net");
		list.add(" ");
		
		panel.add(textfield);
		textfield.setBounds(5,265,450,25);
		textfield.setFont(ver);
		textfield.setText("Type The Text Here");
		
		panel.add(button);
		button.setBounds(5,300,450,25);
		button.setText(name + " Says");
		button.setFont(ver);
		button.addActionListener(this);
		
		panel.add(feelings);
		feelings.setBounds(5,335,120,25);
		feelings.setFont(ver);
		panel.add(sendfeel);
		sendfeel.setBounds(135,335,150,25);
		sendfeel.setFont(ver);
		sendfeel.setText("Send Feelings");
		sendfeel.addActionListener(this);
		
		panel.add(exit);
		exit.setBounds(385,335,70,25);
		exit.setFont(ver);
		exit.setText("Exit");
		exit.addActionListener(this);
		
		//panel.add(abuse);
		//abuse.setBounds(5,370,450,25);
		//abuse.setFont(ver2);
		//abuse.setForeground(Color.blue);
		
		
		
		t1.start();
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == button)
		{	
			texta = textfield.getText();
			
			/*if (texta.equals("fuck") || texta.equals("Fuck") || texta.equals("chutia") || texta.equals("Chutia") || texta.equals("lund") || texta.equals("Lund") || texta.equals("Bhadwa") || texta.equals("bhadwa") || texta.equals("chut") || texta.equals("Chut") || texta.equals("Ghandu") || texta.equals("ghandu") || texta.equals("lavda") || texta.equals("Lavda") || texta.equals("Rande") || texta.equals("rande") || texta.equals("Chinaal") || texta.equals("chinaal") || texta.equals("mother fucker") || texta.equals("Mother fucker") || texta.equals("mother Fucker") || texta.equals("Mother Fucker") || texta.equals("dick") || texta.equals("Dick") || texta.equals("cunt") || texta.equals("Cunt") || texta.equals("harami") || texta.equals("Harami") || texta.equals("bhen chood") || texta.equals("Bhen Chood") || texta.equals("bhen Chood") || texta.equals("Bhen chood") || texta.equals("mother chood") || texta.equals("mother Chood") || texta.equals("Mother chood") || texta.equals("Mother Chood"))
			{
				texta = "I am a dog";
			}*/
			
			
			list.add(name + " >> " + texta);
			
			try
			{
				fromServermsg = new BufferedReader(new InputStreamReader(client.getInputStream()));
				toServermsg = new PrintStream(client.getOutputStream());
				
				toServermsg.println(texta);
				System.out.println("sent");
			}
			catch(Exception vb)
			{System.out.println(vb);}
			
		}
		
		if (e.getSource() == exit)
		{
			System.exit(0);
		}
		
		if (e.getSource() == sendfeel)
		{
			if (feelings.getSelectedItem().equals("I Feel ..."))
			{
				return;
			}
			
			feels = String.valueOf(feelings.getSelectedItem());
			list.add(name + " >> " + name + " feels " + feels);
			
			try
			{
				fromServermsg = new BufferedReader(new InputStreamReader(client.getInputStream()));
				toServermsg = new PrintStream(client.getOutputStream());
				
				toServermsg.println(name + " feels " + feels);
				System.out.println("sent feel");
			}
			catch(Exception vb)
			{System.out.println(vb);}
		}
		
	}
	
	public void run()
	{
		try
		{
		fromServername = new BufferedReader(new InputStreamReader(client.getInputStream()));
		toServername = new PrintStream(client.getOutputStream());
		
		// Send Name
		{
			toServername.println(name);
		}
		
		// Recive Name
		{
			name2 = fromServername.readLine();
			list.add(name2 + " Enters Chat");
			list.add(" ");
		}
		
		}
		catch(Exception e3)
		{
			System.out.println(e3);
		}
		
		t2.start();
		
	}
	
	public class Messages extends Thread
	{
		public void run()
		{
			while (true)
			{
				try
				{
				fromServermsg = new BufferedReader(new InputStreamReader(client.getInputStream()));
				toServermsg = new PrintStream(client.getOutputStream());
				
				// Recive Messages
				{	
					recive = fromServermsg.readLine();
					list.add(name2 + " >> " + recive);
				}
				
				}
				catch(Exception e2)
				{
					System.out.println(e2);
				}
			}
		}
	}
	
	
}
//<applet code="clientapp.class" height=500 width=500></applet>



Is This A Good Question/Topic? 0
  • +

Replies To: chat application in java applet

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: chat application in java applet

Posted 27 January 2011 - 01:32 AM

In the case of Java applets (as opposed to applications) the browser may impose security restrictions - many applets can only communicate with the host from which they were downloaded from. Your chat applet would therefore be unable to open communication with a different server. I have always implemented simple chat programs as applications. What you could do is to run a applet from a server which acts as an exchange. It keeps a list of connected clients and the clients can select another client to talk to, the communication is then routed via the server.
Was This Post Helpful? 0
  • +
  • -

#3 Ooh  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 17
  • Joined: 27-January 11

Re: chat application in java applet

Posted 27 January 2011 - 04:50 PM

View Posthorace, on 27 January 2011 - 01:32 AM, said:

In the case of Java applets (as opposed to applications) the browser may impose security restrictions - many applets can only communicate with the host from which they were downloaded from. Your chat applet would therefore be unable to open communication with a different server. I have always implemented simple chat programs as applications. What you could do is to run a applet from a server which acts as an exchange. It keeps a list of connected clients and the clients can select another client to talk to, the communication is then routed via the server.


You could of course fix the browser security restriction problem by including the applet in an iframe and running it off of the original server, while including the iframe on your separate website.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1