• Post Reply Bookmark Topic Watch Topic
  • New Topic

Connecting to a Java Socket

 
Gagan Tiwari
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
I have written a code which connects to a Java Socket (TCPIP) and then I start processing the Data coming from the Stream.
However there is a limitation in this code i.e it tries to connect once and if connection is not present (say due to any error or server not present) it simply does nothing. I need to enhance the connection logic so that if for any reason the connection was not possible then it should automatically try after some time...

the code is as follows:

public void startProcess(){
bolean connection = getConnection();
if (connection){
doProcess();
}
else {
print No connection is available;
}
}

public boolean getConnection(){
try {
Socket socket = new Socket();
reader object creation;
writer obk=ject creation;
boolean status = socket.isConnected();
catch(exception e) {
status=false;
}
return status;
}

Kindly suggest....If we can do this without threading it will be of great help.
thanks in advance
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gagan:
If we can do this without threading it will be of great help.


Well if you want to "try after sometime" then you have to make the currently processing thread sleep for sometime.
Otherwise you can keep on checking in a while loop. Although this will work but it will make people scream, shout, spank and kill you!!

So, if you are worried of writing this scheduling logic yourself, then you can use a Timer
[ June 19, 2008: Message edited by: Nitesh Kant ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!