D
Digital Puer
Hi, I am encountering a strange problem with a client/server socket
program. In a tight loop, I am changing a field of an object and then
sending it over a socket. However, the change is not evident at the
receiving socket at the other end! This problem occurs with the
client/server pair on the same machine.
My code looks like this:
public class Datum implements java.io.Serializable
{
public String _data;
Datum() { }
}
In the sending program, I have this code:
Datum data = new Datum();
for (int i = 0; i < 10; i++)
{
data._data = "data"+i;
System.out.println("sending " + data._data);
oos.writeObject(data);
oos.flush();
}
At the sender, this properly prints data1, data2, data3, etc.
However, at the other end of the socket, I am getting data1, data1,
data1, etc.
When I instantiate 'new Datum' inside the loop, it works properly.
Does anyone have an idea?
Below is all the source code.
public class Datum implements java.io.Serializable
{
public String _data;
Datum() { }
}
import java.io.*;
import java.net.*;
class Client
{
public static void main(String argv[])
{
try
{
Socket socket = new Socket("localhost", 3000);
ObjectInputStream ois = new
ObjectInputStream(socket.getInputStream());
Datum data;
for (int i = 0; i < 10; i++)
{
data = (Datum) ois.readObject();
System.out.println("got '"+data._data+"'");
}
}
catch(Exception ex)
{
//System.out.println(ex);
ex.printStackTrace();
}
}
}
import java.io.*;
import java.net.*;
public class Server
{
public static void main(String argv[])
{
//send_method_1(); // THIS ONE WORKS
send_method_2(); // THIS ONE DOESN'T WORK!!
}
/*********************************************************************/
static void send_method_1()
{
try
{
ServerSocket socket = new ServerSocket(3000);
System.out.println("waiting for connection");
Socket client_socket = socket.accept();
ObjectOutputStream oos =
new
ObjectOutputStream(client_socket.getOutputStream());
for (int i = 0; i < 10; i++)
{
Datum data = new Datum();
data._data = "data"+i;
System.out.println("sending " + data._data);
oos.writeObject(data);
oos.flush();
}
}
catch(Exception ex)
{
//System.out.println(ex);
ex.printStackTrace();
}
}
/*********************************************************************/
static void send_method_2()
{
try
{
ServerSocket socket = new ServerSocket(3000);
System.out.println("waiting for connection");
Socket client_socket = socket.accept();
ObjectOutputStream oos =
new
ObjectOutputStream(client_socket.getOutputStream());
Datum data = new Datum();
for (int i = 0; i < 10; i++)
{
data._data = "data"+i;
System.out.println("sending " + data._data);
oos.writeObject(data);
oos.flush();
}
}
catch(Exception ex)
{
//System.out.println(ex);
ex.printStackTrace();
}
}
}
program. In a tight loop, I am changing a field of an object and then
sending it over a socket. However, the change is not evident at the
receiving socket at the other end! This problem occurs with the
client/server pair on the same machine.
My code looks like this:
public class Datum implements java.io.Serializable
{
public String _data;
Datum() { }
}
In the sending program, I have this code:
Datum data = new Datum();
for (int i = 0; i < 10; i++)
{
data._data = "data"+i;
System.out.println("sending " + data._data);
oos.writeObject(data);
oos.flush();
}
At the sender, this properly prints data1, data2, data3, etc.
However, at the other end of the socket, I am getting data1, data1,
data1, etc.
When I instantiate 'new Datum' inside the loop, it works properly.
Does anyone have an idea?
Below is all the source code.
public class Datum implements java.io.Serializable
{
public String _data;
Datum() { }
}
import java.io.*;
import java.net.*;
class Client
{
public static void main(String argv[])
{
try
{
Socket socket = new Socket("localhost", 3000);
ObjectInputStream ois = new
ObjectInputStream(socket.getInputStream());
Datum data;
for (int i = 0; i < 10; i++)
{
data = (Datum) ois.readObject();
System.out.println("got '"+data._data+"'");
}
}
catch(Exception ex)
{
//System.out.println(ex);
ex.printStackTrace();
}
}
}
import java.io.*;
import java.net.*;
public class Server
{
public static void main(String argv[])
{
//send_method_1(); // THIS ONE WORKS
send_method_2(); // THIS ONE DOESN'T WORK!!
}
/*********************************************************************/
static void send_method_1()
{
try
{
ServerSocket socket = new ServerSocket(3000);
System.out.println("waiting for connection");
Socket client_socket = socket.accept();
ObjectOutputStream oos =
new
ObjectOutputStream(client_socket.getOutputStream());
for (int i = 0; i < 10; i++)
{
Datum data = new Datum();
data._data = "data"+i;
System.out.println("sending " + data._data);
oos.writeObject(data);
oos.flush();
}
}
catch(Exception ex)
{
//System.out.println(ex);
ex.printStackTrace();
}
}
/*********************************************************************/
static void send_method_2()
{
try
{
ServerSocket socket = new ServerSocket(3000);
System.out.println("waiting for connection");
Socket client_socket = socket.accept();
ObjectOutputStream oos =
new
ObjectOutputStream(client_socket.getOutputStream());
Datum data = new Datum();
for (int i = 0; i < 10; i++)
{
data._data = "data"+i;
System.out.println("sending " + data._data);
oos.writeObject(data);
oos.flush();
}
}
catch(Exception ex)
{
//System.out.println(ex);
ex.printStackTrace();
}
}
}