aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Rewriting data into a socket fails...? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Rewriting data into a socket fails...?" Watch "Rewriting data into a socket fails...?" New topic
Author

Rewriting data into a socket fails...?

janne jounivich
Ranch Hand

Joined: Jan 11, 2005
Posts: 45
Hi Java experts,

I am desparate with my applet attempting to write some data into
a socket stream. Actually I am able to write once something into the socket but as soon as I am doing it the second time, the execution of the applets fails.

My scenario is the following. I have gotta a component placed inside JApplet. Certain actions are listened on this component. These actions trigger, fire up a process at the end of which some data is written into the socket stream. However something goes very wrong when the second action is triggered.

Here is my simplified code

public class className extends javax.swing.JApplet implements ComponentListener {
public void init(){
try {
ConnectSocket("127.0.0.1",9000);
OpenWriters();
} catch(Throwable thro){
System.out.println("ERROR:"+thro.getMessage());
}}
}

//this method is triggered when user does certain
//actions on component
public boolean method_Trying_To_Write_To_Socket(String url,int query_length) {
try {
out.println("TEXT THAT I WANNA WRITE TO SOCKET");
catch (Throwable tho) {
System.err.println("Throwable: " + tho);
}
}



public void stop(){
CloseSocket();
}

public void ConnectSocket(String server,int port) throws Throwable {
pe = new SocketPermission(server+":"+port,"connect,accept,resolve");
socket = new Socket(server, port);
}

public void OpenWriters() throws Throwable {
out = new PrintWriter(socket.getOutputStream(),true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}

public void CloseSocket() {
try {
in.close();
out.close();
socket.close();
} catch (Exception e){
}
}


I have gotta server implemented with Java listening to a certain port.

Could any of you help me out with what goes actually wrong? Why am I not able to write into the socket another time?
[ June 04, 2008: Message edited by: janne jounivich ]

----
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8713
    
    6

Can you show us the stack trace of the exception?


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
janne jounivich
Ranch Hand

Joined: Jan 11, 2005
Posts: 45
Just wondering the following.

During the applet session ( from init() to stop() ) I want to
use the same socket connection and readers, writers created
upon it. Only when stop() or destroy() are invoked, only then
I want to close all streams.

Should I put all the creations of sockets and streams into init()
method? Or somewhere else?

What about if I wanna use over and over again the same socket and
output stream? If I have some actionPerformed kind of method in which I
want to write data into the stream, can't I basically just write to this with out.println("xxx"); out.flush();

What I want is that my socket and output readers should be persistence during the appelet session (from init to stop)? How to achieve this?

BR
Janne
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8713
    
    6

Originally posted by janne jounivich:
What I want is that my socket and output readers should be persistence during the appelet session (from init to stop)?


It sounds like you tried it and ran into problems.
I don't see why it would not work. We don't have enough information to solve the problem you ran into.
There are many reasons why a socket connection will terminate (network congestion, connection timeout and so on). Your best bet is to plan for a persistent connection AND plan to recover the connection when it is lost.
janne jounivich
Ranch Hand

Joined: Jan 11, 2005
Posts: 45
In my code, the actionPerformed kind of method when I am studying the boolean values of socket.isConnected() and socket.isBound(), both values are true but still out.println("xxx") and out.flush() don't write anything to a output stream. Wandering why? If those above mentioned variable values are true, according to my logic the socket connection should be active, alive and shouldn't I then be able to perform the writing successfully?

As for exceptions, I get NullPointerExceptions very seldomly (maybe 1/5) in that part of my code where I am writing text into PrintWriter object (like out.println("xxx");

BR
-janne-
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8713
    
    6

I have no idea what could be wrong. Have a look through our FAQ, HowToAskQuestionsOnJavaRanch, in particular IsolateTheProblem and PostRealCode for some ideas as to how to narrow down the problem so we can help you better.
 
 
subject: Rewriting data into a socket fails...?
 
Similar Threads
Proxy Server!!!
Rendering dynamic web content
Java client socket to talk to C server socket
how to make web server
Connection Refused Error