Hi everyone,
Hows life? Well we are making a online support for our shopping mall and i have to make an
applet that simply allows a user to chat with a person sitting at the server side who might provide him with help in his queries.Now i have written a few classes for that but there seems to be some problem as the applet cant seem to communicate to the server from which it is downloaded? Is there any security manager that i have to override?? The code for the client class is as below:
and do give me more suggestions if any:
import java.util.*;
import java.awt.*;
import java.awt.List;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
import java.applet.Applet;
public class Client extends Applet implements ActionListener,Runnable
{
DatagramSocket receiver;
DatagramSocket dispatch;
TextArea display;
TextField msg;
Panel p1;
Panel p2;
Panel p3;
Thread t;
String nameuser;
boolean flag;
String computer;
int port;
static final int DEFAULT_PORT=2000;
public void init()
{
nameuser="default";
flag=false;
setLayout(new BorderLayout());
p1=new Panel();
p1.setLayout(new BorderLayout());
p2=new Panel();
p2.setLayout(new BorderLayout());
p3=new Panel();
p3.setLayout(new BorderLayout());
msg=new TextField(60);
msg.setText("type ur messgs here");
msg.addActionListener(this);
display=new TextArea(10,60);
display.setEditable(false);
t=new Thread(this);
t.start();
p3.setBackground(Color.green);
p3.setForeground(Color.white);
p1.setBackground(Color.white);
p1.setForeground(Color.blue);
p2.setBackground(Color.white);
p2.setForeground(Color.red);
p3.add(new Label("Welcome to Online Help"),BorderLayout.NORTH);
p1.add(display,BorderLayout.CENTER);
p2.add(msg,BorderLayout.CENTER);
add(p3,BorderLayout.NORTH);
add(p1,BorderLayout.CENTER);
add(p2,BorderLayout.SOUTH);
}
public void run()
{
try
{
receiver=new DatagramSocket(2001);
while(flag)
{
byte b[]=new byte[256];
DatagramPacket dp=new DatagramPacket(b,b.length);
receiver.receive(dp);
b=dp.getData();
display.append("\n"+new String(b));
}
}
catch(Exception e)
{
System.out.println("run: "+e);
}
}
public Insets getInsets()
{
//specify border around applet
return new Insets(3,3,3,3);
}
public void start()
{
if(computer==null)
try
{
String portStr=getParameter("port");
if(portStr==null)
{port=DEFAULT_PORT;}
else
{
try
{
port=Integer.parseInt(portStr);
}
catch(NumberFormatException e)
{
port=DEFAULT_PORT;
}
}
computer=getParameter("Server");
if(computer==null)
computer=getCodeBase().getHost();
}
catch(Exception ee)
{
System.out.println("Server not resolvable");
}
}
public void actionPerformed(ActionEvent ae)
{
try
{
dispatch=new DatagramSocket();
String message=nameuser+" :-) "+msg.getText();
byte b[]=message.getBytes();
DatagramPacket dp=new DatagramPacket(b,b.length,InetAddress.getByName(computer),port);
dispatch.send(dp);
msg.setText("");
flag=true;
}
catch(Exception e)
{
System.out.println("action: "+e);
}
}
}