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.

