Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: sending objects from client to server

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default sending objects from client to server

    hi!

    i wrote a short code of client, server. when i send the object from the client to server, it throws exception in the server: java.lang.ClassNotFoundException: client.Person (Person is the object).

    client:
    package client;
     
    import java.net.*;
     import java.io.*;
     
     
     public class SimpleClient {
             public static void main(String args[]){
                     try{
                             Socket s = new Socket("localhost",2002);
                             OutputStream os = s.getOutputStream();
                             ObjectOutputStream oos = new ObjectOutputStream(os);
                             InputStream is = s.getInputStream();
                             ObjectInputStream ois = new ObjectInputStream(is);
     
                             oos.writeObject(new String("another object from the client"));
                             oos.writeObject(new Person ());
                             oos.writeObject("hello!!!");
     
                             System.out.println((String)ois.readObject());
                            Person p = (Person)ois.readObject();
                            System.out.println(p.age + ", " + p.name + ", " + p.id);
                            System.out.println((String)ois.readObject());
     
                             oos.close();
                             os.close();
                             s.close();
                     }
                     catch(Exception e)
                            {System.out.println(e);}
             }
     }

    server:
    package server;
     
    import java.net.*;
     import java.io.*;
     
    public class SimpleServer
    {
             public static void main(String args[])
             {
                     int port = 2002;
                     try
                     {
                             ServerSocket ss = new ServerSocket(port);
                             Socket s = ss.accept();
                             InputStream is = s.getInputStream();
                             ObjectInputStream ois = new ObjectInputStream(is);
                             OutputStream os = s.getOutputStream();
                             ObjectOutputStream oos = new ObjectOutputStream(os);
     
                             System.out.println((String)ois.readObject());
                             Person p = (Person)ois.readObject();
                             System.out.println(p.age + ", " + p.name + ", " + p.id);
                             System.out.println((String)ois.readObject());
     
     
                             oos.writeObject("hello!!!");
                             oos.writeObject(p);
                             oos.writeObject(new String("another object from the server 2 client"));
     
     
                             is.close();
                             s.close();
                             ss.close();
                     }
                     catch(Exception e)
                            {System.out.println(e);}
             }
     }

    class Person - exists in the both packages
    import java.io.Serializable;
     
      class Person implements Serializable
     {
             String id = "012345678";
             int age = 30;
             String name = "Inigo Montoya";
     }

    anyone knows why do i get this exception?

    thank you,
    Moshiko


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: sending objects from client to server

    class Person - exists in the both packages
    What does this mean...does this mean there is a server.Person and client.Person class? If so, there's your problem.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: sending objects from client to server

    Quote Originally Posted by copeg View Post
    What does this mean...does this mean there is a server.Person and client.Person class? If so, there's your problem.
    yes, it exist. but why? i don't see any wrong with that... both server and client should "know" the Person class.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: sending objects from client to server

    Is the Person.class file on the classpath for the Server? Is the Person class in a package? Your source does not show it.
    Can you post the full text of the error message showing the source line number where the error occurred?

    Does the cast require the package name also?

  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: sending objects from client to server

    the structure of the project looks like this:



    server:
    run:
    another object from the client
    java.lang.ClassNotFoundException: client.Person
    BUILD SUCCESSFUL (total time: 3 seconds)

    client:
    run:
    java.net.SocketException: Connection reset
    BUILD SUCCESSFUL (total time: 0 seconds)

    "Does the cast require the package name also? " ? i don't understand what do you mean?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: sending objects from client to server

    client.Person p = (client.Person)ois.readObject();

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: sending objects from client to server

    They are in different packages, may be the same in content but the different in practice. Norm has the correct method if you import them...here's a simple example which will result in a compile time error without all the client server communication:
    2 packages, client and server
    Client Package:
    package client;
     
    public class Client {
     
    	public Client(){
     
    	}
     
    	public void doWork(){
    		Person p = new Person("test");
    		server.Server server = new server.Server();
    		server.receive(p);
    	}
    }
    package client;
     
    public class Person {
     
    	private String name;
     
    	public Person(String n){
    		name = n;
    	}
    }


    Server Package
    package server;
     
    public class Server {
     
    	public Server(){
     
    	}
     
    	public void receive(Person p){
     
    	}
    }
     
     
    package server;
     
    public class Person {
    	private String name;
     
    	public Person(String n){
    		name = n;
    	}
    }

    Notice there are 2 Person classes which are identical, but from the machine standpoint different because they are defined in different packages. This should result in a compile time error when Client tries to call server.receive(p). The fix is remove the duplicate class, and import said class in the package which doesn't have the Person class: a long winded version of Norm's post

  8. #8
    Junior Member
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: sending objects from client to server

    ok, i removed Person from the server and now i need to import it into sever.




    i built client package and then tried these coded

    package server;
     
    import client.Person;
    import java.net.*;
    import java.io.*;

    or

    package server;
     
    import Person;
    import java.net.*;
    import java.io.*;

    i tried but no success...
    how do i import only Person class?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: sending objects from client to server

    import client.Person;
    Does your classpath point to the folder containing the client folder?

  10. #10
    Junior Member
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: sending objects from client to server

    i don't understand what do you mean by saying "classpath point to folder"

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: sending objects from client to server

    What folder contains the client folder?
    Put the path to that folder in the classpath:
    java -cp .;<PATH-TO-FOLDER-WITH-client-FOLDER> ...

    The javac and the java programs use the classpath to find classes. If the package/class is not on the classpath, it isn't found and you get errors.

  12. #12
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: sending objects from client to server

    Norm's advice will solve the compilation time errors, but you may still receive runtime exceptions if client and server are packaged separately since you still need to let the one without Person know where that class is. For example, say you package your projects into Client.jar and Server.jar and each goes to its respective location. If Person resides in Client.jar but this is located on a different machine (the client) when you run Server.jar it will not be able to find the class Person. One solution is to add both Client.jar and Server.jar to the server.

  13. The Following User Says Thank You to copeg For This Useful Post:

    11moshiko11 (October 8th, 2010)

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: sending objects from client to server

    Another solution is to put the client.Person.class file in the Server.jar file.

  15. #14
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: sending objects from client to server

    And yet another solution is to package Person in an entirely separate jar that is distributed to the client and server, a pattern which decouples the design of datatransfer from that of the client or server.

  16. #15
    Junior Member
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: sending objects from client to server

    a lot of ideas. some of them i didn't understand (those which i had to use dos)...

    create a jar for person is the idea that looks the easiest. i tried it before, but i didn't succeeded. i didn't know how to import it to the server and the client.

    i thought about what i'm trying to do (without a success) and i think that it won't be a good mechanism for my project.
    what i have is:

    class Location
    {
           int x;
           int y;
    ...
    }

    class Cube
    {
          char sign;
          Location loc;
    ...
    }

    class Board
    {
            Cube [][] b;
            Ship[] s;
    ...
    }

    class Ship
    {
             int size;
             String name;
    ...
    }

    class Player
    {
             Board board;
    ...
    }
     
    and more classes...

    each class is in different file. all the classes are in the same package.
    the only thing i need to send is the field "board" that in player. Player is a huge class and has a lot of field, all kind (that i don't need to send).

    is this mechanism i'm trying to do, would help me to send this field?

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: sending objects from client to server

    This looks like a question of setting up your IDE. Have you tried looking on a forum for you IDE to find out how to use packages between projects?

  18. The Following User Says Thank You to Norm For This Useful Post:

    11moshiko11 (October 8th, 2010)

  19. #17
    Junior Member
    Join Date
    Oct 2010
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: sending objects from client to server

    mates, thank you for all your help and all the ideas.
    it seems to be impossible. i need more time to solve it, time that i don't have... i spent a full day and a half for that mechanism and i think i'll use non - elegant solution that easier to do. oh well, the homework's checker ckecks only if the game works, not how it been realized...

    when i have some free time, i'll solve it out.

    thank you,
    moshiko

Similar Threads

  1. [Java] Client - server, example
    By Grabar in forum Java Networking
    Replies: 6
    Last Post: January 22nd, 2011, 01:56 PM
  2. Client/Server
    By Dillz in forum Paid Java Projects
    Replies: 2
    Last Post: June 2nd, 2010, 05:19 AM
  3. Client-Server program
    By Reztem in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 12th, 2010, 05:36 PM
  4. Monitor the progress of sending a file to client
    By adumi in forum Java Theory & Questions
    Replies: 0
    Last Post: April 17th, 2010, 07:01 AM
  5. [SOLVED] The concept of Server and Client
    By rendy.dev in forum Java Theory & Questions
    Replies: 3
    Last Post: January 18th, 2010, 04:13 AM

Tags for this Thread