Results 1 to 4 of 4
  1. #1
    nousername is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default How to send an object over a socket?

    Dear team,

    I followed the tutorial of Sun and I tried to make something similar. I used the Code Samples 8, 9, 10.
    All I want to do is to send an object between two clients that are in the same LAN using sockets.

    For instance:
    A client from 192.168.1.5 should be able to send the object to 192.168.1.6 .
    I tried to "split" the Code Samples but it was impossible since there are errors when compiling.
    Any suggestion about how to send an object over a socket between clients??

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    Post the code you have, descriptions of the behavior you expect and the behavior you are getting, and any errors you are getting.
    Get in the habit of using standard Java naming conventions!

  3. #3
    nousername is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default

    I have 3 classes running on the same machine:

    First class:
    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class SerializedObject implements Serializable {
       private int array[] = null;
    
       public SerializedObject() {
       }
    
       public void setArray(int array[]) {
         this.array = array;
       }
    
       public int[] getArray() {
         return array;
       }
    }
    Second Class:
    Java Code:
    import java.io.*;
    import java.net.*;
    
    public class ArrayClient {
       public static void main(String argv[]) {
          ObjectOutputStream oos = null;
          ObjectInputStream ois = null;
          // two arrays
          int dataset1[] = {3, 3, 3, 3, 3, 3, 3};
          int dataset2[] = {5, 5, 5, 5, 5, 5, 5};
          try {
            // open a socket connection
            Socket socket = new Socket("192.168.1.7", 4000);
            // open I/O streams for objects
            oos = new ObjectOutputStream(socket.getOutputStream());
            ois = new ObjectInputStream(socket.getInputStream());
            // create two serialized objects
            SerializedObject so1 = new SerializedObject();
            SerializedObject so2 = new SerializedObject();
            SerializedObject result = null;
            int outArray[] = new int[7];
            so1.setArray(dataset1);
            so2.setArray(dataset2);
            // write the objects to the server
            oos.writeObject(so1);
            oos.writeObject(so2);
            oos.flush();
            // read an object from the server
            result = (SerializedObject) ois.readObject();
            outArray = result.getArray();
            System.out.print("The new array is: ");
            // after unpacking the array, iterate through it
            for(int i=0;i<outArray.length;i++) {
              System.out.print(outArray[i] + "   ");
            }
            oos.close();
            ois.close();
          } catch(Exception e) {
            System.out.println(e.getMessage());
          }
       }
    }
    And the last class:
    Java Code:
    import java.io.*;
    import java.net.*;
    
    public class ArrayMultiplier extends Thread {
    
       private ServerSocket arrayServer;
     
    
       public static void main(String argv[]) throws Exception {
         new ArrayMultiplier();
       }
    
       public ArrayMultiplier() throws Exception {
         arrayServer = new ServerSocket(4000);
         System.out.println("Server listening on port 4000.");
         this.start();
       } 
    
       public void run() {
         while(true) {
           try {
            System.out.println("Waiting for connections.");
            Socket client = arrayServer.accept();
            System.out.println("Accepted a connection from: "+
    client.getInetAddress());
            Connect c = new Connect(client);
           } catch(Exception e) {}
         }
       }
    }
    
    class Connect extends Thread {
       private Socket client = null;
       private ObjectInputStream ois = null;
       private ObjectOutputStream oos = null;
        
       public Connect() {}
    
       public Connect(Socket clientSocket) {
         client = clientSocket;
         try {
          ois = new ObjectInputStream(client.getInputStream());
          oos = new ObjectOutputStream(client.getOutputStream());
         } catch(Exception e1) {
             try {
                client.close();
             }catch(Exception e) {
               System.out.println(e.getMessage());
             }
             return;
         }
         this.start();
       }
    
       public void run() {
          SerializedObject x = null;
          SerializedObject y = null;
          int dataset1[] = new int[7];
          int dataset2[] = new int[7];
          int result[] = new int[7];
          try {
             x = (SerializedObject) ois.readObject();
             y = (SerializedObject) ois.readObject();
             dataset1 = x.getArray();
             dataset2 = y.getArray();
             // create an array by multiplying two arrays
             for(int i=0;i<dataset1.length;i++) {
               result[i] = dataset1[i] * dataset2[i];
             }
             // ship the object to the client
             SerializedObject output = new SerializedObject();
             output.setArray(result);
             oos.writeObject(output);
             oos.flush();
             // close connections
             ois.close();
             oos.close();
             client.close(); 
          } catch(Exception e) {}       
       }
    }
    It is a simple program that multiplies the elements of two arrays.
    There is not any error on the above codes.
    All I want to do is to leave the first two classes at 192.168.1.7 and use the last class at another location in my LAN (192.168.1.8).
    Any suggestions please?

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

Similar Threads

  1. [Socket] Send audio data
    By Dennis in forum Advanced Java
    Replies: 2
    Last Post: 12-15-2010, 07:31 PM
  2. How to send folder through a connected socket
    By Chris555 in forum Networking
    Replies: 0
    Last Post: 07-18-2009, 11:41 AM
  3. to send float array with TCP socket
    By zeyneqp in forum Networking
    Replies: 6
    Last Post: 08-30-2008, 03:48 AM
  4. Send an array via Socket
    By lene in forum Networking
    Replies: 0
    Last Post: 04-23-2008, 04:29 PM
  5. send files through socket
    By bbq in forum Advanced Java
    Replies: 1
    Last Post: 06-05-2007, 02:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •