Sunday, December 29, 2013

Java exercise - Client and Server example V, pass ArrayList of object in socket

This Java Client/Server example demonstrate how to pass ArrayList of String in socket.

Pass ArrayList of String in socket
Pass ArrayList of String in socket

Java code in host side:
import java.io.IOException;
import java.io.OutputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

public class hostList {
    
    static ArrayList<String> myList;

    public static void main(String srgs[]) {

        int count = 0;
        
        initList();

        //hard code to use port 8080
        try (ServerSocket serverSocket = new ServerSocket(8080)) {
            
            System.out.println("I'm waiting here: " + serverSocket.getLocalPort());
            
            while (true) {
                
                try {
                    Socket socket = serverSocket.accept();
                            
                    count++;
                    System.out.println("#" + count + " from "
                            + socket.getInetAddress() + ":" 
                            + socket.getPort());
                    
                    HostThread myHostThread = new HostThread(socket, count);
                    myHostThread.start();
                    
                } catch (IOException ex) {
                    System.out.println(ex.toString());
                }
            }
        } catch (IOException ex) {
            System.out.println(ex.toString());
        }
    }
    
    private static class HostThread extends Thread{
        
        private Socket hostThreadSocket;
        int cnt;
        
        HostThread(Socket socket, int c){
            hostThreadSocket = socket;
            cnt = c;
        }

        @Override
        public void run() {
            
            OutputStream outputStream;
            try {
                outputStream = hostThreadSocket.getOutputStream();
                try (ObjectOutputStream objectOutputStream = 
                    new ObjectOutputStream(outputStream)) {
                        objectOutputStream.writeObject(myList);
                }
            } catch (IOException ex) {
                System.out.println(ex.toString());
            }finally{
                try {
                    hostThreadSocket.close();
                } catch (IOException ex) {
                    System.out.println(ex.toString());
                }
            }

        }
    }
    
    private static void initList(){
        // init myList with dummy data
        myList =  new ArrayList<String>();
        myList.add("Hello");
        myList.add("Raspberry Pi");
        myList.add("from");
        myList.add("helloraspberrypi.blogspot.com");
    }
}

Java code in client side:
import java.net.Socket;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.ObjectInputStream;
import java.net.UnknownHostException;
import java.util.ArrayList;

class clientList{

    public static void main(String args[])
    { 
        if(args.length == 0){
            System.out.println("usage: java client <port>");
            System.exit(1);
        }
        
        int port = isParseInt(args[0]);
        if(port == -1){
            System.out.println("usage: java client <port>");
            System.out.println("<port>: integer");
            System.exit(1);
        }
        
        try{
            //IP is hard coded
            //port is user entry
            Socket socket = new Socket("192.168.111.109", port);
            
            ArrayList<String> myList;
            
            ObjectInputStream objectInputStream = 
                new ObjectInputStream(socket.getInputStream());
            try{
                Object object = objectInputStream.readObject();
                myList = (ArrayList<String>)object;
                    
                for (String s : myList) {
                    System.out.println(s);
                }
                    
            }catch(ClassNotFoundException ex){
                System.out.println(ex.toString());
            }

            socket.close();            
        }catch(UnknownHostException e){
            System.out.println(e.toString());
        }catch(IOException e){
            System.out.println(e.toString());
        }

    }

    private static int isParseInt(String str){
        
        int num = -1;
        try{
             num = Integer.parseInt(str);
        } catch (NumberFormatException e) {
        }
        
        return num;
    }
    
}




Download source code.



Java exercise - Implement client and server to communicate using Socket, step-by-step.

No comments: