aspose file tools
The moose likes Java in General and the fly likes Sending Serialized Objects to A Server And Displaying the contents Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Sending Serialized Objects to A Server And Displaying the contents" Watch "Sending Serialized Objects to A Server And Displaying the contents" New topic
Author

Sending Serialized Objects to A Server And Displaying the contents

ronan carty
Greenhorn

Joined: Nov 14, 2005
Posts: 4
hi, i am trying to input a student name on a client(which is working for me) , which is then sent to a server and displayed on the server screen!! i just cant get this to work for me for some reason!

It stops at "s = (Student)istream.readObject();" on the server, could someone please help me on this one as i've pulled out nearly all my hair at this stage! ;-)

i have added the functions (minus some catch expressions) below, the server and sockets etc are all working fine! cheers.
Any tips, help would be very much appreciated!



<Server code>
private static void run()
{
try
{
link = servSock.accept();
PrintWriter out = new PrintWriter(link.getOutputStream(),true);
ObjectInputStream istream = new ObjectInputStrea(link.getInputStream());

Student s = null;
Try
{
s = (Student)istream.readObject();
System.out.println("Name=" + s.getName());
} catch (Exception e){ }
...............

}
<End of Server Code>

<Client Code>
private static void run() throws ClassNotFoundException
{
try
{

Socket link = new Socket(host,PORT); //Step 1.

BufferedReader in = new BufferedReader(new
InputStreamReader(link.getInputStream()));
ObjectOutputStream out =
new ObjectOutputStream(link.getOutputStream());

BufferedReader userEntry = new
BufferedReader(new InputStreamReader(System.in));


System.out.print("Enter Name");
name = userEntry.readLine();

...................

<end of client code>
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16480
    
    2

I don't see any code in your client code that writes a Student object to the ObjectOutputStream. Your client doesn't send anything, that's why the server is waiting.
ronan carty
Greenhorn

Joined: Nov 14, 2005
Posts: 4
hi, thanks very much for the reply!! here's the revised client code


Socket link = new Socket(host,PORT);
BufferedReader in = new BufferedReader(new InputStreamReader(link.getInputStream()));
ObjectOutputStream out = new ObjectOutputStream(link.getOutputStream());

BufferedReader userEntry = new BufferedReader(new InputStreamReader(System.in));


System.out.print("Enter Name");
name = userEntry.readLine();
Student studentObj = new Student(name);
out.writeObject(studentObj);
out.close();
ronan carty
Greenhorn

Joined: Nov 14, 2005
Posts: 4
Just to clarify!

It still does not work with the way i have writeobjects above!
ronan carty
Greenhorn

Joined: Nov 14, 2005
Posts: 4
here's the answer, no thanks to anyone here! lol

private static void run() throws ClassNotFoundException
{

try
{

Socket link = new Socket(host,PORT);
String name,address,str_id;
int id;

BufferedReader in = new BufferedReader(new InputStreamReader(link.getInputStream()));

ObjectOutputStream out = new ObjectOutputStream(link.getOutputStream());

BufferedReader UserEntry = new BufferedReader(new InputStreamReader(System.in));


System.out.print("Enter Name: ");
name = UserEntry.readLine();

System.out.println("Enter Student Id: ");
str_id = UserEntry.readLine();
id = Integer.parseInt(str_id);

System.out.print("Enter Address: ");
address = UserEntry.readLine();



Student studentObj = new Student(name, id, address);
out.writeObject(studentObj);
out.close();


}
catch (IOException e)
{
System.out.println(e.getMessage());
}
}
}
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Sending Serialized Objects to A Server And Displaying the contents
 
Similar Threads
Help Debugging my TCP client server program to solve the producer consumer problem
BufferedReader conflicting with InputStream ?
Echo client/server application
Help debugging and running My GUI TCP Chatroom!
Send a String to Server and server send it somewhere on network