Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

need to send exceptions to anthor method

Thread view: 
islamelnaggar@gmail.com - 28 Dec 2007 20:53 GMT
Hello java guys
i'm writing multi-thread server so i need help with this topic

the point is i'm calling method to read socket input i need my called
class thread to be stopped
if the reader method catch an exception
thanks

class testclass{
   public testclass(){
       //some code to read data from socket
      try{
      socket.read();
     }
     catch(IOException e){
      //Dosomething
     }
   }

}

public class main{
  public static void main(){
      testclass = new testclass();
      // i need this class to stop if called class catch an
exception ...

 }
}
Vince - 28 Dec 2007 21:13 GMT
> Hello java guys
> i'm writing multi-thread server so i need help with this topic
[quoted text clipped - 25 lines]
>   }
> }

Don't catch your exception within your testclass and instead throw an
exception... -> class testclass throws IOException...

Then in your main class you try / catch to react on it...

Cheerio Vince

Signature

Posted via a free Usenet account from http://www.teranews.com

Lew - 29 Dec 2007 03:04 GMT
> Hello java guys
> i'm writing multi-thread server so i need help with this topic
[quoted text clipped - 25 lines]
>   }
> }

As written, your code will stop after one socket read anyway, assuming
'socket' is of a type that has a read() method.

Even if you put the read() in a loop, inside the try {} block, the loop will
break on the exception and will end.  No special action needed on your part.

You also do not need to rethrow the exception.

You absolutely DO need to move the read() out of the constructor for the
class.  You also should name the class with an initial upper-case letter.  You
should consider making the class public.  The name should reflect the purpose
of a class, not that it is a class.  Of course it's a class.  Every class is a
class.

If your main() method lacks a String [] parameter and is not static, you
cannot run it from the command line.

public class TestSocket
{
 private static final int BUFZ = 8192;

 public byte [] readSocket( String host, int port )
 {
   Socket socket;
   try
   {
     socket = new Socket( host, port );
   }
   catch ( Exception e )
   {
     throw new IllegalStateException( e );
   }

   try
   {
     return readSocket( socket );
   }
   finally
   {
     try
     {
       socket.close();
     }
     catch ( IOException e )
     {
       System.err.println( "Socket close. "+e.getMessage();
     }
   }
 }

 public byte [] readSocket( Socket socket )
 {
   ByteArrayOutputStream baos = new ByteArrayOutputStream( BUFZ );
   try
   {
     InputStream ins = socket.getInputStream();

     byte buf [] = new byte [BUFZ];
     for( int bred; (bred = ins.read( buf )) > -1; )
     {
       baos.write( buf, 0, bred );
     }
     return baos.toByteArray();
   }
   catch( IOException e )
   {
     throw new IllegalStateException( e );
   }
   finally
   {
     try { baos.close(); } catch ( IOException e ) {}
   }
 }

 public static void main( String [] args )
 {
   TestSocket tester = new TestSocket();
   try
   {
     byte [] got = tester.readSocket( "localhost", 6666 );
     System.out.println( got );
   }
   catch ( IllegalStateException e )
   {
     System.err.println( "TestSocket.readSocket(): " + e.getMessage() );
   }
 }
}

Signature

Lew

islamelnaggar@gmail.com - 31 Dec 2007 03:11 GMT
> islamelnag...@gmail.com wrote:
> > Hello java guys
[quoted text clipped - 119 lines]
> --
> Lew

thanks  sir for your sir
i know what are u explaining above . but i wrote this code just to
explain what i need
the project code is completely different
thanks for ur help again


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.