Results 1 to 4 of 4
  1. #1
    sarah jain is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default Transferring array b/w client and server

    Hi All,

    I am trying to write a client server program. I want to pass an array of strings from client to sever and I want the array to take input from user. I wrote this program to take string "A B C", but I don't know how to pass arrays....please help

    The server code
    Java Code:
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.lang.ClassNotFoundException;
    import java.lang.Runnable;
    import java.lang.Thread;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Scanner;
    
    public class server1
    {
    	private ServerSocket server;
    	private int port = 7777;
    
    	public server1()
    	{
    		try 
    		{
    			server = new ServerSocket(port);
    		}
    		catch (IOException e)
    		{
    			e.printStackTrace();
    		}
    
    	}
    
    	public static void main(String[] args)
    	{
    		server1 example = new server1();
    		example.handleConnection();
    	}
    
    	public void handleConnection() 
    	{
    		System.out.println("Waiting for client message got...");
    
    // The server do a loop here to accept all connection initiated by the
    
    // client application.
    		while (true) 
    		{
    			try
    			{
    				Socket socket = server.accept();
    				new ConnectionHandler(socket);
    			}
    			catch (IOException e)
    			{
    				e.printStackTrace();
    			}
    		}
    	}
    }
    
    class ConnectionHandler implements Runnable 
    {
    	private Socket socket;
    	public ConnectionHandler(Socket socket)
    	{
    		this.socket = socket;
    		Thread t = new Thread(this);
    		t.start();
    	}
    	public void run()
    	{
    		try
    		{
    // Read a message sent by client application
    			ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
    			String message = (String) ois.readObject();
    			System.out.println("Message Received from client: " + message);
    			b(message);
    			ois.close();
    			socket.close();
    			System.out.println("Waiting for client message is...");
    		} 
    		catch (IOException e) 
    		{
    			e.printStackTrace();
    		} 
    		catch (ClassNotFoundException e) 
    		{
    			e.printStackTrace();
    		}
    	}
    	private void b(String message) {
    		 List<String> list = new ArrayList<String>();
    	        String[] arr = list.toArray(new String[0]);
    	        System.out.println("Array is " + Arrays.toString(arr));
    		
    	}
    }
    Client Program
    Java Code:
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;
    public class client1 {
    
    	public static void main(String[] args) {
    		try {
    // Create a connection to the server socket on the server application
    			Socket socket = new Socket("localhost", 7777);
    			
    // Send a message to the client application
    			ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
    			oos.writeObject("A B C");
    			oos.close();
    		} catch (UnknownHostException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace(); 
    		}
    	}
    }
    Regards,
    Sarah

  2. #2
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    6

    Default

    Java Code:
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.lang.ClassNotFoundException;
    import java.lang.Runnable;
    import java.lang.Thread;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Scanner;
    
    public class server1
    {
    	private ServerSocket server;
    	private int port = 7777;
    
    	public server1()
    	{
    		try 
    		{
    			server = new ServerSocket(port);
    		}
    		catch (IOException e)
    		{
    			e.printStackTrace();
    		}
    
    	}
    
    	public static void main(String[] args)
    	{
    		server1 example = new server1();
    		example.handleConnection();
    	}
    
    	public void handleConnection() 
    	{
    		System.out.println("Waiting for client message got...");
    
    // The server do a loop here to accept all connection initiated by the
    
    // client application.
    		while (true) 
    		{
    			try
    			{
    				Socket socket = server.accept();
    				new ConnectionHandler(socket);
    			}
    			catch (IOException e)
    			{
    				e.printStackTrace();
    			}
    		}
    	}
    }
    
    class ConnectionHandler implements Runnable 
    {
    	private Socket socket;
    	public ConnectionHandler(Socket socket)
    	{
    		this.socket = socket;
    		Thread t = new Thread(this);
    		t.start();
    	}
    	public void run()
    	{
    		try
    		{
    // Read a message sent by client application
    			ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
    			String message[] = (String[]) ois.readObject();
    			//System.out.println("Message Received from client: " + message);
    			//b(message);
                            printArray(message);    
    			ois.close();
    			socket.close();
    			System.out.println("Waiting for client message is...");
    		} 
    		catch (IOException e) 
    		{
    			e.printStackTrace();
    		} 
    		catch (ClassNotFoundException e) 
    		{
    			e.printStackTrace();
    		}
    	}
    	private void b(String message) {
    		 List<String> list = new ArrayList<String>();
    	        String[] arr = list.toArray(new String[0]);
    	        System.out.println("Array is " + Arrays.toString(arr));
    		
    	}
            private void printArray(String[] arr){
               for(String s:arr){
                  System.out.println(s);
                }
    	}
    }

    Java Code:
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;
    public class client1 {
    
    	public static void main(String[] args) {
    		try {
    // Create a connection to the server socket on the server application
    			Socket socket = new Socket("localhost", 7777);
    			
    // Send a message to the client application
    			ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                            String data[]={"Hello","How","Hi"};
    			//oos.writeObject("A B C");
                            oos.writeObject(data);
    			oos.close();
    		} catch (UnknownHostException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace(); 
    		}
    	}
    }
    Swastik

  3. #3
    sarah jain is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    Hey dswastik,

    Thanks for the help, but I want the user to give input. Each time I this code, I want to give different strings as input. But in this case the array will have standard strings "hello", "hi","how".

    Regards,
    Sarah

  4. #4
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    6

    Default

    Hi sarah,

    You just need to change the client code a bit

    Java Code:
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    public class client1 {
    
    	public static void main(String[] args) {
    		try {
    // Create a connection to the server socket on the server application
    			Socket socket = new Socket("localhost", 7777);
    
    // Send a message to the client application
    			ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
    			//oos.writeObject("A B C");
    			String data[]=new String[3];
    			BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    			System.out.println("Enter details for the array");
    			for(int x=0;x<3;x++){
    				System.out.print("Enter word number"+(x+1)+":");
    				data[x]=br.readLine();
    			}
    			oos.writeObject(data);
    			System.out.println("Details sent to server...");
    			oos.close();
    		} catch (UnknownHostException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    Swastik

Similar Threads

  1. Replies: 5
    Last Post: 02-28-2011, 11:42 AM
  2. Replies: 2
    Last Post: 09-24-2010, 12:31 PM
  3. Replies: 2
    Last Post: 03-16-2010, 08:01 AM
  4. Replies: 1
    Last Post: 10-05-2008, 04:12 PM
  5. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 10:18 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
  •