Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Detection of socket connecting process, two thread problem

Reply
Thread Tools

Detection of socket connecting process, two thread problem

 
 
artik
Guest
Posts: n/a
 
      01-25-2013
Hi,
I use one socket and two threads with it: one for reading and one for writing to it. My problem is in moment when error occurs and two thread want to do connection again on the same socket by closing the secont at start. Result of this is that while one one thread (the firtst of catching error) wants to start reconnecting the second one close socket to perform it for reconnecting. Classic dead lock. Therefore I ask about possibility to read stateof socket exactly state of try to connecting?

Regards,
Artik
 
Reply With Quote
 
 
 
 
artik
Guest
Posts: n/a
 
      01-25-2013
I'm newbie in Java and after several attempts and changing approaches i've decided to use synchronization like this below. I think it protects code (?) to use socket in the same time. Two threads using this procedures (disconnect and connect after) wait (if they need) when the second thread finished connecting and start to do the same: disconnect and connect. I can't findthe way: don't doing disconnection-connection by the second thread when the first one started this just second before.

When error connections occurs in the threads (reading/writing) then call
reconnection procedure insede it are:
{...
close_connection();
set_connection();
....
}
which are below

public synchronized void close_connection() {
try {
socket.shutdownInput();
socket.shutdownOutput();
socket.close();
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

public synchronized boolean setconnection() {
boolean result=true;
socket = new Socket();
try {
socket.connect(new InetSocketAddress(address, port), 500);
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream()));
} catch (IOException e)
{result=false;
}
return result;
}

Artik
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      01-25-2013
On 1/25/2013 2:33 AM, artik wrote:
> I'm newbie in Java and after several attempts and changing approaches i've decided to use synchronization like this below. I think it protects code (?) to use socket in the same time. Two threads using this procedures (disconnect and connect after) wait (if they need) when the second thread finished connecting and start to do the same: disconnect and connect. I can't find the way: don't doing disconnection-connection by the second thread when the first one started this just second before.
>
> When error connections occurs in the threads (reading/writing) then call
> reconnection procedure insede it are:
> {...
> close_connection();
> set_connection();
> ...
> }
> which are below
>
> public synchronized void close_connection() {
> try {
> socket.shutdownInput();
> socket.shutdownOutput();
> socket.close();
> try {
> Thread.sleep(500);
> } catch (InterruptedException e1) {
> e1.printStackTrace();
> }
> } catch (IOException e1) {
> // TODO Auto-generated catch block
> e1.printStackTrace();
> }
> }
>
> public synchronized boolean setconnection() {
> boolean result=true;
> socket = new Socket();
> try {
> socket.connect(new InetSocketAddress(address, port), 500);
> in = new BufferedReader(new InputStreamReader(
> socket.getInputStream()));
> out = new BufferedWriter(new OutputStreamWriter(
> socket.getOutputStream()));
> } catch (IOException e)
> {result=false;
> }
> return result;
> }
>
> Artik
>


I like to keep this sort of thing very simple. Divide up the connection
handling part from the I/O part.

Main thread
Loop
Handle connection
Spawn service threads that handle I/O by passing socket
Wait until all service threads are finished
EndLoop
End Main thread

Service thread
Loop
Create readers and writers as appropriate
Do I/O and process

If error
Close socket to stop all service threads
Break out of loop
End Loop
End service thread

--

Knute Johnson
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 1 02-03-2009 06:20 AM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 0 02-01-2009 12:45 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM



Advertisments