This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Address already in use and Socket operation on nonsocket

 
Piradharsan Navaratnam
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi brothers and sisters,

I wanted to check whether the a server is running or not. So I wrote a client socket program to connect and check the status of the server.

I faced lot of problems that it i wanted to continuoustly check the server so i wrote the code inside a infinite while loop.

I got the above exceptions not both at once.
The address already exists exception occurred when i connected using the below code.

loop:
while(true){

tyr{
Socket s = new Socket(ip, port);
s.close();
}catch(Exception e){
continue loop;
}
}

The Socket operation on nonsocket exception occurred when i used the below command

the same while loop but I tried to connect through
new Socket.connect(SocketAddress){}

please help
 
Joe Ess
Bartender
Posts: 9319
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code works for me when I use a String for the IP. Exactly what are you passing as the IP (Socket takes several different arguments: InetAddress, String (host) String (ip))?
Are you certain you were not using ServerSocket? I would expect an "Address already in use" exception if you tried to bind to the same socket a server was using.
One further note, it is a good idea to put either Thread.yield or Thread.sleep calls in when writing infinite loops. That way other code can get to use the CPU once in a while.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic