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 5 of 5

Thread: Sending object through socket

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Sending object through socket

    Hello!

    My code is misbehaving and I can't see where the fault is, but I'm almost sure that it's a beginner's mistake. I'm sending objects through a socket in a while loop. The object has 4 fields: first three are double[] arrays and the last one is a Timestamp. The objects are being received also in a loop.

    On the server side, before sending the object I print its fields and they are ok (the data in the object's fields changes at each iteration) but when I receive the object on the client side it has the same values in the three double[] fields as the first object that was sent (the first object was correctly received). The strange thing is that the Timestamp field is correct for all the received objects.

    Here's the relevant code:

    public class Server extends Thread{
     
            ServerSocket ss=null;
    	Socket socket=null;
    	String line="";
    	Measurements mes = new Measurements();
     
     
    public void run(){
    	try{
     
    		ss = new ServerSocket(1905); 
    		socket = ss.accept(); 
     
    		BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    		ObjectOutputStream out = new ObjectOutputStream((socket.getOutputStream()));
     
    		double x[] = new double[6];
    		double u[] = new double[6];
    		double v[] = new double[6];
     
    		line = in.readLine();
     
    		while ((!line.equals("STOP")) && (line.equals("Get Measurements"))){			
     
    			for (int i=0;i<6;i++){
     
    				synchronized (Simulator.lakes[i]){
    				v[i]=Simulator.lakes[i].getInFlow();
    				u[i]=Simulator.lakes[i].getOutFlow();
    				x[i]=Simulator.lakes[i].getLevel();}
    			}
     
    			mes=new Measurements();
    			mes.setX(x);
    			mes.setU(u);
    			mes.setV(v);
     
    			Calendar calendar = Calendar.getInstance();
    			java.util.Date now = calendar.getTime();
    			java.sql.Timestamp currentTimestamp = new java.sql.Timestamp(now.getTime());
    			mes.setDate(currentTimestamp);
     
    			System.out.println("In server:  x[1]="+ mes.getX()[1]+"  u[1]="+mes.getU()[1]+"  v[1]="+mes.getV()[1]+"date= "+mes.getDate());
     
    			out.writeObject(mes);
     
    			sleep(1000);
    			out.flush();
    			line = in.readLine();
    		}
     
    	}catch(Exception e){e.printStackTrace();}
    }
    }


    In the client class:

    public Measurements getMeasurements(){
     
     
    		out.println("Get Measurements");
     
    		try {
     
    			m=(Measurements) in.readObject();
     
    			System.out.println("In client:  x[1]="+ m.getX()[1]+"  u[1]="+m.getU()[1]+"  v[1]="+m.getV()[1]+" date= "+m.getDate());
     
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		return m;
    	}

    The output is:

    In server: x[1]=77.3336 u[1]=0.7897 v[1]=0.5128 date= 2010-11-15 08:26:57.681
    In server: x[1]=32.5530 u[1]=0.6430 v[1]=0.9706 date= 2010-11-15 08:26:58.768
    In server: x[1]=46.0680 u[1]=0.3521 v[1]=0.7266 date= 2010-11-15 08:26:59.774
    In server: x[1]=49.1065 u[1]=0.1337 v[1]=0.3319 date= 2010-11-15 08:27:00.775
    ...

    In client: x[1]=77.3336 u[1]=0.7897 v[1]=0.5128 date= 2010-11-15 08:26:57.681
    In client: x[1]=77.3336 u[1]=0.7897 v[1]=0.5128 date= 2010-11-15 08:26:58.768
    In client: x[1]=77.3336 u[1]=0.7897 v[1]=0.5128 date= 2010-11-15 08:26:59.774
    In client: x[1]=77.3336 u[1]=0.7897 v[1]=0.5128 date= 2010-11-15 08:27:00.775
    ...



    Any ideas why this is happening?

    Thanks,
    Alexandra


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

    Default Re: Sending object through socket

    I don't see a problem with the above code that would result in the output you are getting. That being said, I could make an educated guess that the values you are trying to access in measurements are static.

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

    javapenguin (November 15th, 2010)

  4. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Sending object through socket

    Unfortunately it's not the case. I've tried to send an array, randomly generated and the same thing happens, but when I send a String it's working fine.

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,307
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Sending object through socket

    Can you post a bit more code, for example the Measurements class and how the client is actually calling getMeasurements?

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

    Alexandrinne (November 16th, 2010)

  7. #5
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Talking Re: Sending object through socket

    I solved the problem with an out.reset(); in the server, after the object in (ObjectOutputStream) out was sent through the socket. I found an explanation for the problem here[<- link]. Anyway, thanks copeg for trying to help!

Similar Threads

  1. Problem sending POST request with Java..
    By lost in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 20th, 2010, 09:16 PM
  2. sending objects from client to server
    By 11moshiko11 in forum What's Wrong With My Code?
    Replies: 16
    Last Post: October 8th, 2010, 04:47 AM
  3. Sending XML over HTTP to Another Application
    By darasgar in forum Java Servlet
    Replies: 2
    Last Post: July 14th, 2010, 01:56 PM
  4. sending an email with pdf file everyday
    By pradeepptp in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: June 21st, 2009, 10:49 AM
  5. Sending and Receiving mail using J2ME without server
    By chals in forum Java ME (Mobile Edition)
    Replies: 5
    Last Post: June 2nd, 2009, 09:59 AM