Wednesday, June 5, 2013

Java client/server application


AIM: Write a java  that implements a simple client/server application. The client sends data to a server. The server receives the data, uses it to produce a result and then sends the result back to the client. The client displays the result on the console. For ex: The data sent from the client is the numbers and the result produced by the server is the addition of that numbers
Client program:
import java.io.*;
import java.net.*;
public class AddClient
 {
            public static void main(String args[])
             {
                        Socket addSocket = null;
                        DataOutputStream strmToServer = null;
                        DataInputStream strmFromServer = null;
                        final String server = "localhost";
                        int i, currentSum, num;
                        try
                         {
                                    addSocket = new Socket(server, 8000);
                                    strmToServer = new DataOutputStream(addSocket.getOutputStream());
                                    strmFromServer = new DataInputStream(addSocket.getInputStream());
                                    System.out.println("Connected to Server ...");
                        }
                        catch (UnknownHostException ex)
                        {
                                    System.out.println("Unable to find server: " + server);         
                                    System.exit(1);
                        }
                        catch (IOException ex)
                        {
                        System.out.println("Unable to open I/O stream to server: " + server);
                        System.exit(1);
                        }
                        try
                        {
                                    strmToServer.writeInt(args.length-1);
                                    currentSum = Integer.parseInt(args[0]);
                                    for (i=1; i<args.length; i++)
                                    {
                                                num = Integer.parseInt(args[i]);
                                                strmToServer.writeInt(currentSum);
                                                strmToServer.writeInt(num);
                                                currentSum = strmFromServer.readInt();
                                    }
                                    System.out.println("Sum: " + currentSum);
                                    strmFromServer.close();
                                    strmToServer.close();
                                    addSocket.close();
                        }
                        catch (Exception ex)
                        {
                                    ex.printStackTrace();
                         }
            }
}
Server program:

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

public class AddServer
 {
            public static void main(String args[])
            {
                        Socket socket = null;
                        ServerSocket serverSocket = null;
                        DataOutputStream strmToClient = null;
                        DataInputStream strmFromClient = null;
                        int i, numAdditions, sum, num1, num2;
                        try
                        {
                                    serverSocket = new ServerSocket(8000);
                                    System.out.println("Server is starting ...");
                        }
                        catch (Exception ex)
                        {
                                    System.out.println("Unable to start server");
                                    System.exit(1);
                        }
                        try
                        {
                                    while (true)
                                    {
                                                socket = serverSocket.accept();
                                                System.out.println("Serving client: " + socket.getInetAddress());
                                                strmToClient = new DataOutputStream(socket.getOutputStream());
                                                strmFromClient = new DataInputStream(socket.getInputStream());
                                                numAdditions = strmFromClient.readInt();
                                                for (i=0; i<numAdditions; i++)
                                                {
                                                            num1 = strmFromClient.readInt();
                                                            num2 = strmFromClient.readInt();
                                                            System.out.println(num1 + " + " + num2);
                                                            sum = num1 + num2;
                                                            strmToClient.writeInt(sum);
                                                }
                                                socket.close();
                                    }
                        }
                        catch (IOException ex)
                        {
                                    System.out.println("Unable to open I/O stream to server: " + ex);
                                    System.exit(1);
                        }
            }
}
Output:

No comments:

Post a Comment