• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Chat Applet!!!!

 
Ajay Singhal
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! friends
i'm designing a chat applet.
but not getting a idea how to design the communication between client and server.
and also wants to refresh the applet with maximum performance.
give me some tips to complete the Chat Applets.I'll be greatly thankful to U.
------------------
 
landon manning
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Communications:
Applets are able to open a socket to the server from which they come from. Sockets may be a way to go.
Refresh:
Have your server broadcast the message and then the client append any message (after filtering) to a TextArea. That way you don't have to worry about refreshing. The TextArea will do it for you.
 
Ajay Singhal
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks landon for ur cooperation!
but i've tried the applet using sockets.
when i'm using socket it raise an exception indicating the security violation.
why it is so.
and could u explain the concept of broadcasting.
ajay singhal
------------------
 
Ajay Singhal
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! friends
thanks for ur support.
I've got ur idea of broadcasting the messages from server.
can U please explain how to Broadcast the message from server to different client applets connected to server.
Ajay Singhal
------------------
 
Tanveer Rameez
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
You can use URL and URLConnect classes of java.net to connect applet to servlet.In the applet you give something like this(I forgot the exact syntax, so this code may need some correction-but the besic idea is same):
<code>
void sendToGet()
String servleturl="http://...whatever your servlet URL is";
//if you are invoking servlet's doGet method and want to pass
//parameters you append the name-value pair to the servleturl
servleturl=servleturl+"?name1=value1&name2=value2";
URL=new URL(servleturl);
URLConnect con=url.getConnection();
}
void sendToPost()
{
String servleturl="http://...whatever your servlet URL is";
URL=new URL(servleturl);
URLConnect con=url.getConnection();
//If you are invoking the servlet's doPost method you pass the
//name=value pair in the body part of your request. So obtain the
//output stream.
OutputStream ois=con.getOutputStream();
ois.setDoInput(); //i forgot the exact method name
ois.setDoOutput();////i forgot the exact method name
//mymessage is the name of the parameter whose value is read
//from a TextField
ois.write("mymessage="+mytextfield.getText())
....
//if you are invoking the do method of the servlet and the
//you want to retrieve what the servlet is sending you back
byte[] buffer=ois.read();
mytextarea.append("\n"+new string(buffer));
}
</code>
I suggest you read the book-'Servlet Programming (O'Reilly)' chapter- 'applet servlet communication'.

I once developed a chat applet complete with roomlist,userlist and private message boxes(like in yahoo chat) using applet-servlet communication.In the moment I am busy with something so I can't explain it to you. Maybe later on.The book will give you a nice idea as it describes a simple chat applet with servlet the server end but it was the basis of my chat server.
Write again after you have gone thru the book.
visit my site:
<a href=http://clik.to/tanveerrameez>http://clik.to/tanveerrameez</a>
, mainly the programming page(java section) to learn some more about chat applet
regards
Tanveer
 
Marine
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am also desiging a chat system. Although i am using an application right now which uses DataGram's SOcket and Pocket. Will it be possible for me to later convert it to an applet ? Or i am going to be forced to use sockets instead ? Thanks.
 
Tanveer Rameez
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Marine
If you want to change your application to applet later on, I suggest you write the datagram connection code in a separate class(say an innner class) which implements an interface:
interface myinnerclassinterface{
send(String mymessage);
String get();
}
Then invoke the methods like:
myinnerclassinterface obj=new mydatagramclass(hostname,port);
obj.send(mymessage);
byte[] message=obj.get()

When you change it to an applet, replace the inner class with another inner class which implements the same interface but invokes the servlet's doGet method in the get method, and the servlet's doPost method in the send method.Then only thing in the application code you have to change is the constructor for the inner class(ofcourse other than extending the Applet class) :
myinnerclassinterface obj=new myurlclass(myurl);

Note one thing that sometimes I faced problem if I tried to access another class from an applet, even if it belonged to the same package. This should not be the case. But to avoid such problem try to use inner classes.
hope this helps
Tanveer
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic