GeeCON Prague 2014*
The moose likes Sockets and Internet Protocols and the fly likes Socket Sorrows 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 "Socket Sorrows" Watch "Socket Sorrows" New topic
Author

Socket Sorrows

Gad Flailimbs
Greenhorn

Joined: Mar 21, 2007
Posts: 15
I've tried using sockets to pass an object accross to a server which then passes this object down to multiple clients accross the network. The problem I've run into is that the program starts chewing up memory and I believe this is because it remembers the object that was sent.

I've read that if the same object is sent accross a socket then a termination signal is sent instead. I was unable to have the object update and be sent to the clients unless I kept creating a new one to send accross. Now it is my assumption that all these objects are just piling up and that the way to solve it must be to clear the buffer of these saved objects (or ids of objects).

Have any of you had experience with this before? Is there a way to get around it? I tried running a clear command or something on the Socket but that didn't work, please let me know if you know how to fix this.

Either this or if you know a way of getting RMI client / server to work through a firewall.


Java Game:<br /><a href="http://pokemon.ele-mentalfury.com/" target="_blank" rel="nofollow">http://pokemon.ele-mentalfury.com/</a><br />Webcomic:<br /><a href="http://ele-mentalfury.com/" target="_blank" rel="nofollow">http://ele-mentalfury.com/</a>
S Hayes
Greenhorn

Joined: Jun 13, 2006
Posts: 6
I don't know if you've tried it, but you could use the flush() method on whatever Writer class you're using to force the current object out the stream.
Gad Flailimbs
Greenhorn

Joined: Mar 21, 2007
Posts: 15
Here's the code from back in the day when I was working on it:

public void send()
{
//Note there is a potential issue if the user hasn't moved
//and a new user gets into the server then it won't return
//its Entity
synchronized(this)
{
try
{
if (am.update == true)
{
//This could be outside the if statement but then
//there would be ceaseless and possible pointless
//communication with the server
myUser.setImage(myTrainer.getImage());
myUser.setX(myTrainer.getTrueX());
myUser.setY(myTrainer.getTrueY());
myUser.setTrueX(myTrainer.getTrueX());
myUser.setTrueY(myTrainer.getTrueY());
am.update = false;
out.writeObject(myUser);
}
}
catch(Exception ex)
{
System.out.println("Send Failed");
}
}
}

I eventually got to the point that I had to use:
out.writeObject(new Trainer(myTrainer.getImage() ... ));

And so it went. The flush command didn't help even though it seems like it should do something like what I want. Has anyone else had experience with this problem? Have you solved it?

flush:
Flushes the stream. This will write any buffered output bytes and flush through to the underlying stream.

You may notice that I am not closing the connection after it is output. I'd prefer to keep the connection open as there is almost always data being sent.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I could give you the answer, but instead I'll teach you to fish: have a look at the Javadoc for ObjectOutputStream. Read the whole thing, but pay special attention to methods whose names start with "r".

Here's a copy for you right here.


[Jess in Action][AskingGoodQuestions]
Gad Flailimbs
Greenhorn

Joined: Mar 21, 2007
Posts: 15
I suck.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
We all do. We learn one thing, then suck at the next one. For only a while if we're lucky. Keep having fun!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
GeeCON Prague 2014
 
subject: Socket Sorrows