• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Server Socket for chat

 
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I,am using server sockets for making a chat server. i have an applet which lists the clients connected to the chat server. Whenever a client connects to the chat server a window on the server's end is popped up for one to one communication with the connected client . E.g if there are 5 client , there would be 5 windows on the servers's end which would talk to 5 diff clients . What is presently happeneing in my code is whenever a client is coonected a window for chat is popped up at the servers end and one to one communication begins . As soon any other get's connected another window is opened and all the messages are displayed in the latest window(which has recently been popped up).
How can i load diff instances of the frame class to talk to diff connected clients(one to one communication).
Please guide me a bit .
Thanks in adavance
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is very hard to be specific about what you're doing wrong -- but basically you are having some shared state (a field) somewhere which should not be shared.
This could be a servlet or JSP instance field (always suspect, that), or an application-scoped variable, a static variable, or something else.
- Peter
 
Raghav Mathur
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the code for server and the client .

_______________code for client____________________

Please if you could tell me a bit more where i could be wrong.
Thanks in advance
[ April 12, 2003: Message edited by: raghav mathur ]
 
Raghav Mathur
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've really got stuck ... please if you could help me a bit.
Thanks in advance
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case, the shared state I was talking about is the TextArea tf variable in the servunis class. All instances of the subthread1 inner class (and hence all clients) share one copy.
But your real problem goes a bit deeper than that: you are way too fond of instance variables. Virtually all of them should be replaced by method-local variables, and remember always to choose the smallest scope possible, i.e. do not declare all your variables at the start of a method!
And while we're at it, please use the Sun Java coding conventions; your code is almost unreadable. That means SubThread1 instead of subthread1 (although ClientHandlerThread would be a much better name).
- Peter
 
Raghav Mathur
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok . I'll try to explain what the code actually does ( I agree ,it is almost ureadable )
I've made some changes to the initial code.
1) I have a class servunis from where i begin this aplication.In the static void main method i run an infinite loop to accept connections from other clients.
2) Whenever a client gets connected ,I spawn a new Thread(the class is chatterbox) Which pops up a chatting interface and also spawn 2 new threads for accepting input and writing output.
3) There are 2 inner classes inside the chatterbox class named as readingclient and writingclient which are used for taking input from the client and writing output to the client .
4) Now in the class readingclient i,am accepting input from the client and displaying it in a list (the messages).
5) In the writeclient i,am writing text from another interface(frame)to the client.
Its working fine ( one to one )
I guess the only thing i have to do is to merge the the 2 frames(writing and reading)in one single frame.
First of all ... is my design for a one to one chat application correct ?
Second of all .... please also have a look at the code.

[ April 16, 2003: Message edited by: raghav mathur ]
 
Raghav Mathur
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I,am facing one more problem . As i said earlier I've created 2 class ( 2 frames) . One for reading and onw for writing. Similar code has ben writin on the client .
Now when the server writes a message to the client , it should be displayed to the client and also to the server and similarly for the client.
Now i have one infinite loop at the server's end and one at the client's end which reads the incomming messages and writes back so that it gets displayed on both the chat windows.
As soon as any of the two start sending messages , it obviously gets into an infinite loop .
PS : The code for this is the same as given above . i've not given the client code as it is almost the same .
Thanks in advance
 
Raghav Mathur
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI
The prob i was mentioning has been rectified . The application works fine.
But ..... if i run my main thread from an applet then only the client on the same machine ( where the server resides)are able to to connect . Clients from other machines cannot and i don't even any sort of exceptions.
Any suggestions
Thanks in advance
 
Raghav Mathur
Ranch Hand
Posts: 641
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Peter
I,am stuck on the last stage of this application that is invoking the server from an applet . But when clients from different machines try to get connected , they fail in doing so . The Code does not give any exceptions ... thiough a client residing on the same machine is able to connect to the server . Is it due the fact that the applet is not letting itself to be shared among the clients cos it doesn't have a static void main method ? .
In that case how am i suppose to display the clients who are conected to the chat server?
If i exclude the Applet model from my application and display the list of clients in a frame which will not be embedded insode a browser)then how will i run my server at www.xyz.com . I,am a bit confused . Kindly help me please . My deadline is near!!
Thanks in advance
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic