Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-26-2007, 09:13 PM
Senior Member
 
Join Date: Nov 2007
Posts: 115
ravian is on a distinguished road
TCPIP Client (handling specific exceptions)
I have a TCPIP client with following code.

Code:
try { smtpSocket = new Socket("javabook", 22122); os = new DataOutputStream(smtpSocket.getOutputStream()); is = new DataInputStream(smtpSocket.getInputStream()); } catch (Exception ex) { System.out.println("Some error has occured"); }
I want to catch specific exceptions for Port and for hostname. Please advice.

Last edited by ravian : 11-28-2007 at 11:47 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-28-2007, 11:45 AM
Member
 
Join Date: Nov 2007
Posts: 12
javagk is on a distinguished road
It is always a wise idea to catch specific exceptions. Try the following:

Code:
try { smtpSocket = new Socket("javabook", 22122); os = new DataOutputStream(smtpSocket.getOutputStream()); is = new DataInputStream(smtpSocket.getInputStream()); } catch (UnknownHostException e) { System.err.println("Don't know about host: hostname"); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to: hostname"); }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-28-2007, 11:48 AM
Senior Member
 
Join Date: Nov 2007
Posts: 115
ravian is on a distinguished road
Thanks javagk.
I tested it and it works perfectly fine.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-29-2007, 11:56 AM
Member
 
Join Date: Nov 2007
Posts: 97
javaplus is on a distinguished road
Thanks guys. It was helpful.
Just checked the Java docs and it says:

Code:
public Socket(InetAddress address, int port) Throws: IOException - if an I/O error occurs when creating the socket. SecurityException - if a security manager exists and its checkConnect method doesn't allow the operation.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Better way to handle exceptions javaplus Advanced Java 2 01-16-2008 08:47 PM
Select specific cell Echilon New To Java 1 01-01-2008 09:47 AM
Rejecting connections from a specific IP javaplus Networking 2 12-21-2007 04:28 PM
Identify Client in Socket Client Server Application masadjie Networking 1 12-20-2007 11:18 AM
Defining own Exceptions JavaForums Java Blogs 0 11-12-2007 06:00 PM


All times are GMT +3. The time now is 02:13 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org