0

I am creating a java server and a java client. I need to send from server to client and viceversa an object and then an array of objects. How can I achieve this? Do I need to serialize the objects classes?

This is the server:

import java.io.*;

import java.net.*;

public class Server extends Thread {

private final ServerSocket Server;

public static void main(String[] args) throws Exception {

    new Server();

}

public Server() throws Exception {

    Server = new ServerSocket(3000);

    System.out.println("Server started on port 3000.");

    this.start();

}

@Override
public void run() {

    while (true) {            

        try {

            System.out.println("Server is listening to new connections...");

            Socket client = Server.accept();

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

            Connect c = new Connect(client);

        } catch (IOException exception) {

            System.out.println(exception.getMessage());

        }

    }

}

    class Connect extends Thread {

    private Socket client = null;
    BufferedReader in = null;
    PrintStream out = null;

    public Connect(Socket clientSocket) {

        client = clientSocket;

        try {

            in = new BufferedReader(new InputStreamReader(client.getInputStream()));
            out = new PrintStream(client.getOutputStream(), true);

        } catch (IOException mainException) {

            try {

                client.close();

            } catch (IOException exception) {

                System.out.println(exception.getMessage());

            }

        }

        this.start();

    }

    @Override
    public void run() {
try {

out.close();
            in.close();
            client.close();

        } catch (IOException exception) {

            System.out.println(exception.getMessage());

        }

    }

}

This is my client:

import java.io.*;
import java.net.*;

public class Client {

    String remoteAddress    =   "localhost";
    BufferedReader in       =   null;
    PrintStream out         =   null;
    Socket socket           =   null;
    String message          =   null;

    String username         =   null;
    String password         =   null;

    public Client(String username, String password) {

        this.username = username;
        this.password = password;

    }

    public String connect() {

        try {

            // begin a new client connection
            socket = new Socket(remoteAddress, 3000);

            // open I-O channels
            in  = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintStream(socket.getOutputStream(), true);

        } catch (Exception exception) {

            return false;

            System.out.println(exception.getMessage());

        }

        return "ERROR";

    }

    public boolean disconnect() throws IOException {

        // close flushes I-O with the server
        out.close();
        in.close();

        return true;

    }

}

This, instead, is one class:

class Contact {

    private String name;
    private String surname;
    private String telephone;
    private String birthday;

    public String getName() {

        return name;

    }

    public String getSurname() {

        return surname;

    }

    public String getTelephone() {

        return telephone;

    }

    public String getBirthday() {

        return birthday;

    }

    public void setName(String value) {

        name = value;

    }

    public void setSurname(String value) {

        surname = value;

    }

    public void setTelephone(String value) {

        telephone = value;

    }

    public void setBirthday(String value) {

        birthday = value;

    }

}

Currently just the server can send data (objects array or only object) to client but I'm thinking about make both able to do. Also, it'd be nice about to send an object (like the class above), an array of same object and an array of different objects (I can't obtain it with classic arrays, right? May I use ArrayList?)

Thank you.

Bence Kaulics
  • 7,066
  • 7
  • 33
  • 63
blurstream
  • 429
  • 3
  • 13

2 Answers2

2

what about java.io.ObjectOutputStream ? try this http://docs.oracle.com/javase/7/docs/api/java/io/ObjectOutputStream.html

EDIT: here's the example included in the javadoc of the class -slightly modified-, as suggested in the comments:

ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());

oos.writeInt(12345);
oos.writeObject("Today");
oos.writeObject(new Date());

oos.close();

and there should be a java.io.ObjectInputStream on the opposite side.

jambriz
  • 1,273
  • 1
  • 10
  • 25
  • +1 If you use Object Stream, you can't use text Reader?Writers, you should only use the Object Stream to avoid confusion. – Peter Lawrey Oct 07 '14 at 21:12
  • An alternative is to use XMLEncoder/XMLDecoder to serialize objects as text. – Peter Lawrey Oct 07 '14 at 21:13
  • should I send String like so, then? : oos.writeObject(new String("my string")) – blurstream Oct 08 '14 at 07:38
  • as long as they implement the java.io.Serializable interface you can send any object you want, as long as you have the same version of the class in both sides – jambriz Oct 08 '14 at 14:33
  • ok, anyway I need to make a new instance of an object every time, right? I can't use oos.writeObject("string") but I should use oos.writeObject(new String("the string")) right? thank you! – blurstream Oct 11 '14 at 19:39
0

Yes You shoud use serialization for that. In that case You could use ObjectOutpuStream and writeObject() method. So it is very simple to manage this without thinking about counting bits etc. http://www.tutorialspoint.com/java/java_serialization.htm

Mateusz B
  • 19
  • 3