• Post Reply Bookmark Topic Watch Topic
  • New Topic

Serialized object is not tranfering data between client and server

 
Basatwar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
i wrote a program to trancefer one string and one interger from server to client thrugh serialized object. But not getting values set in server to client side. Please help me.
Code is as
Server:
import java.lang.*;
import java.util.*;
import java.io.*;
import java.rmi.*;
import java.net.*;
public class Server extends Thread
{
public ServerSocket sc;
public dataStore ds;
String str;
int i;
Server()
{
try
{
sc = new ServerSocket(4050);
System.out.println("Socket created");
this.start();
ds = new dataStore();
ds.setValue("hi, there" , 10);

}
catch(Exception e)
{
System.out.println(e);
}
}
public void run()
{
try {
Socket _clientSoc = sc.accept();
}
catch (IOException ex)
{
System.out.println(ex);

}
}
public static void main(String a[])
{
Server s = new Server();

}
}
Client:
import java.lang.*;
import java.util.*;
import java.io.*;
import java.rmi.*;
import java.net.*;
public class client
{
String s;
int i;
Socket cl;
dataStore ds;

public client()
{
try
{

cl = new Socket("127.0.0.1",4050);
System.out.println("Socket got");
ds = new dataStore();
s = ds.getString();
System.out.println("String is" + s);
i= ds.getInt();
System.out.println("Integer is " + i);
}
catch (Exception e)
{
System.out.println(e);
}
}


public static void main (String a[])
{
client c= new client();
}
}
Serialized object:
import java.io.*;
public class dataStore implements Serializable
{
public String s;
public int i;
public void setValue(String dStr, int dInt)
{
s = dStr;
i=dInt;

}
public String getString()
{
return s;
}
public int getInt()
{
return i;
}
}
Thank u in advance
 
Ryan Langley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Your problem is that you are assuming that the DataStore object instance you create in the server is the same as the DataStore object instance that you create in the client. They are not the same instances.. They are separate. What you need to do is create the instance in the server, and then PASS that instance to the client. The client can then get the data from the passed DataStore object instance. Here are some simple modifications I made that work.. Good luck!
Cheers,
RL
Server:
<pre>
import java.lang.*;
import java.util.*;
import java.io.*;
import java.rmi.*;
import java.net.*;
public class Server extends Thread {
private ServerSocket sc;
private Socket _clientSoc;
private ObjectOutputStream oout;
private DataStore ds = new DataStore();
private String str;
private int i;
Server() {
try {
sc = new ServerSocket(4050);
System.out.println("Socket created");
this.start();
} catch(Exception e) {
System.out.println(e);
}
}
public void run() {
try {
_clientSoc = sc.accept();
oout = new ObjectOutputStream(_clientSoc.getOutputStream());
ds.setValue("hi, there" , 10);
oout.writeObject(ds);
} catch (IOException ex) {
System.out.println(ex);
}
}
public static void main(String a[]) {
Server s = new Server();
}
}
</pre>
Client:
<pre>
import java.lang.*;
import java.util.*;
import java.io.*;
import java.rmi.*;
import java.net.*;
public class Client {
private String s;
private int i;
private Socket cl;
private ObjectInputStream oin;
private DataStore ds;
public Client() {
try {
cl = new Socket("127.0.0.1",4050);
System.out.println("Socket got");
oin = new ObjectInputStream(cl.getInputStream());
ds = (DataStore)oin.readObject();
s = ds.getString();
System.out.println("String is " + s);
i= ds.getInt();
System.out.println("Integer is " + i);
} catch (Exception e) {
System.out.println(e);
}
}
public static void main (String a[]) {
Client c= new Client();
}
}
</pre>
DataStore:
<pre>
import java.io.*;
public class DataStore implements Serializable {
private String s;
private int i;
public void setValue(String dStr, int dInt) {
s = dStr;
i = dInt;
}
public String getString() {
return s;
}
public int getInt() {
return i;
}
}
</pre>
 
Basatwar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ryne,
it worked fine...
Ritesh Basatwar
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!