• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Connection doubt

 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assume that client has established a connection with server & both are communnicating happily.

Assume that at the server side there are two threads:
1) One handles opening & closing of ServerSocketConnection
2) Second one deals with reading from & writing into streams(for client at the other end).

Assume that both the threads at the server side are Active(running).

Question:Now if ServerSocketConnection is closed, what happens at the other thread which is reading from & writing into streams?

I guess it throws some kind of Exception...

Share your thoughts on this.

Thanks..
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Huh?

I create a Connection, on the server side must be some kind of listener, I connect, I finish my work, then disconnect. There are no threads running anymore.

Mark
 
Faraz Masood
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

you just have answered it yourself

yes an exception is thrown , and we can cleanup in the catch block of that exception .. i dont remember exactly but i exactly did the same for some academic project , and i guess the exception thrown is SocketClosed or something like that

do consult the docs to get the exact name
 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Faraz

Hi Mark,

I Am doing my work on the server side in a bit different way:

Regular Approach

In a single Thread:
1) Establish server connection end point(using StreamConnectionNotifier)
2) Wait for the client call(using acceptAndOpen())
3) Get input & output streams for communication with client
4) server & client communicate to achieve some task
5) close streams
6) close server connection

Different Approach

In First thread:
1) Establish server connection end point(using StreamConnectionNotifier)
2) Wait for the client call(using acceptAndOpen())
3) Create & start Second thread
4) some delay
5) Assume that second thread is in step 2; Now if we close server connection WHAT HAPPENS?

In Second thread:
1) Get input & output streams for communication with client
2) server & client are communicating with each other
3) close streams
4) end of thread

I came across this kind of situation when I was learning OBEX protocol(server side coding) few days back.

Hope I Am clear.

Thanks..
[ October 26, 2006: Message edited by: Girish Nagaraj ]
 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
close

public void close()
throws IOExceptionClose the connection.

When a connection has been closed, access to any of its methods that involve an I/O operation will cause an IOException to be thrown. Closing an already closed connection has no effect. Streams derived from the connection may be open when method is called. Any open streams will cause the connection to be held open until they themselves are closed. In this latter case access to the open streams is permitted, but access to the connection is not.


Throws:
IOException - If an I/O error occurs

---------------------*****------------------------

The above description is copied from j2me doc from sun site.

Faraz: It's clear that accessing streams(which are already open), after closing connection will not thow any exception instead streams will held open connection until they themselves are closed.
 
Faraz Masood
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am sorry , i misunderstood the earlier description , because in my case it was a doc sharing application , what i did was whenever a new client arrive

socket = serverSocket.accept();

// create a new thread with reference to socket and do all sort of things there

/*new thread was like */
try{

reading .. writing .. all other stuff ...

}catch(SomeException se){
// this was thrown when the client goes away
clear everything ...
}

however , i dont know much if we are to do the same stuff with mobile devices ....

i hope that it must be something similar , but if its not and you found something new .. please do share
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic