The Artima Developer Community
Sponsored Link

Java Answers Forum
Why my"readObject()" in my server side doesn't work?

4 replies on 1 page. Most recent reply: Apr 15, 2002 10:29 AM by Yoon-Ling

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Yoon-Ling

Posts: 8
Nickname: fan
Registered: Apr, 2002

Why my"readObject()" in my server side doesn't work? Posted: Apr 14, 2002 8:36 PM
Reply to this message Reply
Advertisement
Question: "obj= (DATA)ois.readObject(); "which is in class User. After compiling,I got a message indicates "unreported exception java.lang.ClassNotFoundException; must be
caught or declared to be thrown "
what does that means?

I did implements Serializable in DATA class.
Please tell me what is wrong or what the error message means.

the following is my code
import java.io.*;
import java.net.*;
/*using readObject() to read the DATA object*/
public class serSock{

private int port=8765;
private ServerSocket myServer;
private User user;
serSock(){
try{
myServer= new ServerSocket(port); // setting up Server
System.out.println("Server is set up");
}catch(IOException e){
e.printStackTrace();
System.exit(1);
}
}


public void execute(){
try{
user = new User(myServer.accept(),this); // Server listening
user.start();
System.out.println("Server is listening");
}catch(IOException e){
e.printStackTrace();
System.exit(1);
}

}


public static void main(String args[])
{
serSock ser1= new serSock();
ser1.execute();

}


}

class User extends Thread
{
Socket conn;
ObjectInputStream ois;
serSock control;
DATA obj=null;


User(Socket s, serSock t){
conn=s;
control=t;
try{
ois = new ObjectInputStream(conn.getInputStream());
}catch(IOException e){
e.printStackTrace();
System.exit(1);
}

}
public void run(){

System.out.println("The user's IP is " + conn.getInetAddress());
try{
obj= (DATA)ois.readObject();

}catch(IOException e){e.printStackTrace();
//System.out.print(obj.getOrigin() + " From server's user class");
System.out.println("This is from user run function");}

try{
conn.close();
}catch (IOException e){}
}
}


Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: Why my"readObject()" in my server side doesn't work? Posted: Apr 14, 2002 10:09 PM
Reply to this message Reply
according to the documentation of readObject this method throws ClassNotFoundException so in your run method either have a try..catch block enclosing ois.readObject() or specify ClassNotFoundException using throws clause.

Yoon-Ling

Posts: 8
Nickname: fan
Registered: Apr, 2002

Re: Why my"readObject()" in my server side doesn't work? Posted: Apr 15, 2002 7:13 AM
Reply to this message Reply
hey, thanks for ur response. I did put a try-catch block enclosing ois.readObject()statement, however, the error message still occurs. Is anything else u could think of that might be the reason cause it happenes?

Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: Why my"readObject()" in my server side doesn't work? Posted: Apr 15, 2002 8:12 AM
Reply to this message Reply
I dont know how you tried to resolve the problem.

Your run method should be like this....

 public void run(){
        
        System.out.println("The user's IP is " + conn.getInetAddress());
        try{
            obj= (DATA)ois.readObject();
            
        }catch(IOException e){
            e.printStackTrace();
        //System.out.print(obj.getOrigin() + " From server's user class");
        System.out.println("This is from user run function");
        } catch (ClassNotFoundException ex){
            System.err.println("YOUR PROBLEM WAS HERE...");
        }
        
        try{
            conn.close();
        }catch (IOException e){}
    }

Yoon-Ling

Posts: 8
Nickname: fan
Registered: Apr, 2002

Re: Why my"readObject()" in my server side doesn't work? Posted: Apr 15, 2002 10:29 AM
Reply to this message Reply
hey,
thanks, now I know where is the problem!

Flat View: This topic has 4 replies on 1 page
Topic: Question for Bill Venners Previous Topic   Next Topic Topic: Interface Question

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use