Java socket getOutputStream, lose connection

B

Berlin Brown

Maybe I have this completely wrong, but for a java based client-server
chat style application. Cant I just open a socket at init or load for
example and then re-open the outputstream or inputstream at any time and
send or receive accordingly. Well that doesnt work. I call
getOutputStream(), send my data and I lose my socket connection when I
enter this method again? Too much code to post, but here is the jist.

Applet() {

init(){

socket = new Socket()

}

actionPerformed() {

OutputStream o = socket.getOutputStream()

o.write.flush()

InputStream i = getBytes()

//sometimes I close outputstream, sometimes I dont
// same result


When I call the actionPerformed again, I get a SocketException the
socket is closed. The data goes and I can get data back.

To fix for the time being, I just put the socket connection open in my
actionperformed.

Maybe my thinking of java sockets is wrong, I know this standard fair in C.

}
 
B

Berlin Brown

Berlin said:
Maybe I have this completely wrong, but for a java based client-server
chat style application. Cant I just open a socket at init or load for
example and then re-open the outputstream or inputstream at any time and
send or receive accordingly. Well that doesnt work. I call
getOutputStream(), send my data and I lose my socket connection when I
enter this method again? Too much code to post, but here is the jist.

Applet() {

init(){

socket = new Socket()

}

actionPerformed() {

OutputStream o = socket.getOutputStream()

o.write.flush()

InputStream i = getBytes()

//sometimes I close outputstream, sometimes I dont
// same result


When I call the actionPerformed again, I get a SocketException the
socket is closed. The data goes and I can get data back.

To fix for the time being, I just put the socket connection open in my
actionperformed.

Maybe my thinking of java sockets is wrong, I know this standard fair in C.

} String _final_res = null;

BufferedOutputStream _output_stream = null;

try {

_output_stream = new
BufferedOutputStream(_socket.getOutputStream());

DataOutputStream _data_stream = new
DataOutputStream(_output_stream);

String _send = _str + "\r\n";

byte _getData [] = _send.getBytes(_Encoding);

_data_stream.write(_getData);
_data_stream.flush();
 
B

BarryNL

Berlin said:
Maybe I have this completely wrong, but for a java based client-server
chat style application. Cant I just open a socket at init or load for
example and then re-open the outputstream or inputstream at any time and
send or receive accordingly. Well that doesnt work. I call
getOutputStream(), send my data and I lose my socket connection when I
enter this method again? Too much code to post, but here is the jist.

Applet() {

init(){

socket = new Socket()

}

actionPerformed() {

OutputStream o = socket.getOutputStream()

o.write.flush()

InputStream i = getBytes()

//sometimes I close outputstream, sometimes I dont
// same result


When I call the actionPerformed again, I get a SocketException the
socket is closed. The data goes and I can get data back.

To fix for the time being, I just put the socket connection open in my
actionperformed.

Maybe my thinking of java sockets is wrong, I know this standard fair in C.

}

try opening the sockets in the init() method, eg:

private Socket socket;
private OutputStream out;

init() {
socket = new Socket(...);
out = socket.getOutputStream();
}

actionPerformed() {
out.write(..);
out.flush();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top