Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to send/receive Objects using sockets in java [duplicate]

When I execute my code in the CMD window, it doesn't work in Client mode, exactly at the line:

ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());

the program block on this line but don't stop or signal error :

I have 3 classes: Client, Server, Message

ClientClass:

package client;

// Organize imports     

public class Client {
    public static void main(String[] args) throws UnknownHostException,
            IOException, ClassNotFoundException {
        System.out.println("welcome client");
        Socket socket = new Socket("localhost", 4444);
        System.out.println("Client connected");
        ObjectOutputStream os = new ObjectOutputStream(socket.getOutputStream());
        System.out.println("Ok");
        Message message = new Message(new Integer(15), new Integer(32));
        os.writeObject(message);
        System.out.println("Envoi des informations au serveur ...");

        ObjectInputStream is = new ObjectInputStream(socket.getInputStream());
        Message returnMessage = (Message) is.readObject();
        System.out.println("return Message is=" + returnMessage);
        socket.close();
    }
}

Server

Here is the code of the Server class

package Sockets;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static final int port = 4444;
    private ServerSocket ss = null;

    public void  runServer() throws IOException, ClassNotFoundException{
        ss = new ServerSocket(port);
        System.out.println("le systeme est pret pour accepter les connexions");
        Socket socket = ss.accept();
        ObjectInputStream is = new ObjectInputStream(socket.getInputStream());
        ObjectOutputStream os = new ObjectOutputStream(socket.getOutputStream());

        Message m = (Message) is.readObject();
        doSomething(m);

        os.writeObject(m);
        socket.close(); 
    }

    private void doSomething(Message m) {
        m.setResult(new Integer(m.getA().intValue()*m.getB().intValue()));
    }

    public static void main(String[] args) throws ClassNotFoundException, IOException {
        new Server().runServer();
    }
}

Message

And here is my object , I made it Serializable by implementing the interface Serializable

import java.io.Serializable;

public class Message implements Serializable {
    private static final long serialVersionUID = -5399605122490343339L;

    private Integer A;
    private Integer B;
    private Integer Result; 

    public Message(Integer firstNumber, Integer secondNumber ){
        this.A = firstNumber; 
        this.B = secondNumber;
    }

    public Integer getA() {
        return A;
    }

    public Integer getB() {
        return B;
    }

    public void setResult(Integer X)  {
        Result = X;
    }
}
like image 922
Ismail Addou Avatar asked Jan 02 '15 00:01

Ismail Addou


People also ask

How do you send an object to a socket?

Here, we will see how to send object over network using socket programming. We have to mark class as Serializable by implementing Serializable interface. For Serialization, we have to use class ObjectOutputStream while for Deserialization, we have to use ObjectInputStream.

Can two Java processes communicate if they are both using the socket class?

Java applications operating on multiple JREs can communicate with one another using socket programming. It is applicable for both connection-oriented and connectionless can apply.

How do you send an object in Java?

Example of how to send an Object over a Socket in Java. Socket socket = new Socket("localhost", 7777);


1 Answers

You need to create the ObjectOutputStream before the ObjectInputStream, at both ends, for reasons described in the Javadoc concerning the object stream header.

like image 149
user207421 Avatar answered Oct 21 '22 07:10

user207421