aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Server Socket for chat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Server Socket for chat " Watch "Server Socket for chat " New topic
Author

Server Socket for chat

Raghav Mathur
Ranch Hand

Joined: Jan 12, 2001
Posts: 641
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


Raghav.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Jan 12, 2001
Posts: 641
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

Joined: Jan 12, 2001
Posts: 641
I've really got stuck ... please if you could help me a bit.
Thanks in advance
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Jan 12, 2001
Posts: 641
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

Joined: Jan 12, 2001
Posts: 641
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

Joined: Jan 12, 2001
Posts: 641
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

Joined: Jan 12, 2001
Posts: 641
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Server Socket for chat