A problem with ObjectInputStream

I am a coding newbe and I recently tried to write a very simple app - an engine to send and recive objects through the net. This is what i get:

//Serwer class:

import java.net.*;

import java.io.*;

public class Serwer {

public Serwer() {

try {

ServerSocket serw = new ServerSocket(23);

System.out.println("Server up and running");

while (true) {

Socket soc = serw.accept();

Obsluga obs = new Obsluga(soc);

obs.start();

}

}

catch (Exception e) {

System.out.println(e);

}

}

public static void main(String[] args)

{

Serwer serwer = new Serwer();

}

}

class Obsluga extends Thread {

private Socket soc;

public Obsluga(Socket soc) {

this.soc = soc;

}

public void run() {

try {

System.out.println("Connected from: " + soc.getInetAddress());

ObjectOutputStream out = new ObjectOutputStream(

new BufferedOutputStream(soc.getOutputStream()));

//ObjectInputStream in = new ObjectInputStream(

//new BufferedInputStream(soc.getInputStream()));

//Here's the problem

int i = 1;

while(i<100) {

Ob obiekt = new Ob(i,"text");

out.writeObject(obiekt);

i++;

}

out.close();

}

catch (IOException ex) {

ex.printStackTrace();

}

}

}

class Ob implements Serializable {

private int number;

private String text;

public Ob(int number, String text) {

this.number = number;

this.text = text;

}

public Ob() {

}

public int getNumber() {

return number;

}

public String getText() {

return text;

}

public void setNumber(int i) {

number = i;

}

public void setText(String s) {

text = s;

}

}

//Client class:

import java.io.*;

import java.net.Socket;

public class Klient {

public Klient() {

}

public static void main(String[] args) {

ObslugaKlienta obsK = new ObslugaKlienta();

obsK.run();

}

}

class ObslugaKlienta extends Thread {

public ObslugaKlienta() {

}

public void run() {

Ob obiektIn = new Ob();

try {

Socket socket = new Socket("localhost", 23);

ObjectInputStream in = new ObjectInputStream(

new BufferedInputStream(socket.getInputStream()));

ObjectOutputStream out = new ObjectOutputStream(

new BufferedOutputStream(socket.getOutputStream()));

while (true) {

obiektIn = (Ob) in.readObject();

System.out.println(obiektIn.getNumber());

}

}

catch(Exception e) {

System.out.println(e);

}

}

}

class Ob implements Serializable {

private int number;

private String text;

public Ob(int number, String text) {

this.number = number;

this.text = text;

}

public Ob() {

}

public int getNumber() {

return number;

}

public String getText() {

return text;

}

public void setNumber(int i) {

number = i;

}

public void setText(String s) {

text = s;

}

}

The server class simply creates 100 Objects Ob and sends them to waiting Client, which prints the field number from it. And this works.

The problem is, in my Serwer class, if I try to start new InputStreamReader, to receive Objects Ob from Client, the app stops working. Even if I am not using it anywhere. (u have to uncomment the declaration to see it - where "here is the problem" statment is)

I think I am missing some theory about sockets. Can anyone clear me with it? And give me a working solution, what should I correct in my code to have both client and server able to exchange Objects?

Im using jdk 1.6, and running both apps from a single machine.

Thank you in advance.

[3979 byte] By [mruka] at [2007-11-27 5:53:26]
# 1
Opening an InputStream on a socket and trying to read from it blocks your main thread. If you want your application to do something else while reading, you need to move the reading into a separate thread. http://java.sun.com/docs/books/tutorial/essential/concurrency/
quittea at 2007-7-12 15:46:30 > top of Java-index,Java Essentials,Java Programming...
# 2

Thank you for the reply.

I reworked my app: I moved sending and receiving to different Threads, but still I can not manage to get output i wanted to. I get the SocketClosed Exception.

Server:

package pakiet;

import pakiet.Send;

import pakiet.Receive;

import java.net.*;

import java.io.*;

public class Serwer {

public Serwer() {

try {

ServerSocket serw = new ServerSocket(23);

System.out.println("Server up and running");

while (true) {

Socket soc = serw.accept();

Obsluga obs = new Obsluga(soc);

obs.start();

}

}

catch (Exception e) {

System.out.println(e);

}

}

public static void main(String[] args)

{

Serwer serwer = new Serwer();

}

}

class Obsluga extends Thread {

private Socket soc;

public Obsluga(Socket soc) {

this.soc = soc;

}

public void run() {

System.out.println("Connection from: " + soc.getInetAddress());

new Receive(soc).start();

new Send(soc).start();

}

}

Client:

package pakiet;

import pakiet.Send;

import pakiet.Receive;

import java.io.*;

import java.net.Socket;

public class Klient {

public Klient() {}

public static void main(String[] args) {

ObslugaKlienta obsK = new ObslugaKlienta();

obsK.start();

}}

class ObslugaKlienta extends Thread {

public ObslugaKlienta() {}

public void run() {

try {

Socket socket = new Socket("localhost", 23);

new Receive(socket).start();

new Send(socket).start();

}

catch(Exception e) {

System.out.println(e);

}

}

}

Send:

package pakiet;

import java.io.BufferedOutputStream;

import java.io.ObjectOutputStream;

import java.net.Socket;

class Send extends Thread {

Socket socket;

public Send(Socket socket) {

this.socket = socket;

}

public void run() {

try {

System.out.println("Thread Send activated");

ObjectOutputStream out = new ObjectOutputStream(

new BufferedOutputStream(socket.getOutputStream()));

int i = 1;

while(i<10) {

Ob obiekt = new Ob(i,"tekst");

out.writeObject(obiekt);

sleep(100);

i++;

}

}

catch(Exception e) {

System.out.println(e);

}}}

Receive:

package pakiet;

import java.io.BufferedInputStream;

import java.io.ObjectInputStream;

import java.net.Socket;

class Receive extends Thread {

Socket socket;

public Receive(Socket socket) {

this.socket = socket;

}

public void run() {

try {

System.out.println("Thread Receive activated");

ObjectInputStream in = new ObjectInputStream(

new BufferedInputStream(socket.getInputStream()));

int i = 1;

while(i<10) {

Ob obiekt = (Ob) in.readObject();

System.out.println(obiekt.getNumber());

sleep(500);

i++;

}

}

catch(Exception e) {

System.out.println(e);

}}}

My goal is to receive Ob numbers simultaneously in both server and client output window. Now i got massage in both client and sever that Thread Send and Receive is activated, but nothing happends afterwards.

I know its a lot of code, so I bolded the parts I consider most important.

mruka at 2007-7-12 15:46:30 > top of Java-index,Java Essentials,Java Programming...
# 3
SocketClosedException means you closed the socket yourself and then tried to use it again. Closing an input or output stream of a socket closes the other stream and the socket.
ejpa at 2007-7-12 15:46:30 > top of Java-index,Java Essentials,Java Programming...
# 4
But i dont use socket.close() anywhere.
mruka at 2007-7-12 15:46:30 > top of Java-index,Java Essentials,Java Programming...
# 5
You coded:out.close();You got:SocketClosedExceptionI wrote:> Closing an input or output stream of a socket closes the other stream and the socket.You wrote:> But i dont use socket.close() anywhere.Look again.
ejpa at 2007-7-12 15:46:30 > top of Java-index,Java Essentials,Java Programming...