The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
February 2002

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

readObject() method problem

Posted by Oha on February 20, 2002 at 1:37 PM

Hear is a simple client\server thats shows the problem.
The value of 'j' should go from 1 to 10 if the vector is being updated.

//////////////////////////
Client
/////////////////////////
import java.net.*;
import java.io.*;
import java.util.*;

public class testClient extends Object implements Serializable
{
public static void main(String args[])
{
Socket clientSocket; // Client socket object
ObjectInputStream is = null; // Input stream
ObjectOutputStream os = null; // Output stream
Vector input = new Vector();
Vector output = new Vector();
int j = 0;
String k;

try
{
clientSocket = new Socket("localHost",6060);

os = new ObjectOutputStream(clientSocket.getOutputStream());
is = new ObjectInputStream(clientSocket.getInputStream());
}
catch(Exception e)
{
System.out.println("This is before the while loop \n" + e);
System.exit(1);
}

while(true)
{

try
{
k = "j is = " + j;
j++;
output.clear();
output.add(k);

os.writeObject(output);
os.flush();

input = (Vector)is.readObject();

for(int i= 0; i < input.size(); i++)
{
System.out.println(input.get(i));
}
input.clear();
}
catch(Exception e)
{
System.out.println("It messed up ! !! !!! !!!!\n" + e);
System.exit(1);
}
if(j == 10)
System.exit(0);
}
}
}

//////////////////////////
Server
/////////////////////////
import java.net.*;
import java.io.*;
import java.util.*;

public class testServer extends Object implements Serializable
{

public static void main(String args[])
{
Socket clientSocket = null; // Client socket object
ObjectInputStream is = null; // Input stream
ObjectOutputStream os = null; // Output stream
Vector input = new Vector();
Vector output = new Vector();
int j = 0;
String k;
ServerSocket serverSocket = null;

try
{
serverSocket = new ServerSocket(6060);
System.out.println("Started listening on port 6060");
}
catch (IOException e)
{
System.out.println("Cannot listen on port: " + 6060 + ", " + e);
System.exit(1);
}

try
{
clientSocket = serverSocket.accept();
System.out.println("Accepted socket connection from client");
}
catch (IOException e)
{
System.out.println("Accept failed: 6060 " + e);
System.exit(1);
}

try
{
is = new ObjectInputStream(clientSocket.getInputStream());
os = new ObjectOutputStream(clientSocket.getOutputStream());

}
catch(Exception h)
{
System.out.println(h);
}

while(true)
{

try
{
input = (Vector)is.readObject();

for(int i = 0; i < input.size(); i++)
{
System.out.println(input.get(i));
}

k = "" + j;
output.clear();
output.add(k);

j++;

os.writeObject(output);
os.flush();
}
catch(Exception e)
{
System.out.println("It messed up ! !! !!! !!!!" + e);
System.exit(0);
}
if(j == 10)
{
System.exit(0);
}
}
}
}



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us