Tcp-based distributed applications


TCP-based Distributed Applications

1. Change this program so that each client will now send ten integers and receives their sum from the server. In Java, for loops can be easily implemented as follows: for (int i = 0 ; i < 10; i++).

2. Change your program now as follows: the client sends a request containing the message "add 10 20" to the server. This server runs the method specified by the client, i.e., "add" using the given parameters, and sends back the result to the client. In addition, the server sends the client's request along with the client's IP address and port number to a backup server. The methods that the user can specify are: add (+), sub (-), mul (*), and div (/). The backup server stores all the received operations in a backup database (in our case it will be a simple array). Note that your program must allow many clients at the same time.

import java.net.*; import java.io.*;
public class TCPClient {
public static void main (String args[]) { // arguments supply message and hostname
     Socket s = null;
     try{
           int serverPort = 7896;
           s = new Socket(args[1], serverPort);   
           DataInputStream in = new DataInputStream( s.getInputStream());
           DataOutputStream out =new DataOutputStream( s.getOutputStream());
           out.writeUTF(args[0]);          // UTF is a string encoding see Sn. 4.4
           String data = in.readUTF();    // read a line of data from the stream
           System.out.println("Received: "+ data) ;
      }catch (UnknownHostException e) {System.out.println("Socket:"+e.getMessage());
      }catch (EOFException e){System.out.println("EOF:"+e.getMessage());
      }catch (IOException e){System.out.println("readline:"+e.getMessage());
      }finally {if(s!=null) try {s.close();
          }catch (IOException e) {System.out.println ("close:" + e.getMessage());}}}}

    import java.net.*;
import java.io.*;
public class TCPServer {
public static void main (String args[]) {
      try{
    int serverPort = 7896; // the server port
    ServerSocket listenSocket = new ServerSocket(serverPort);
    while(true) {
        Socket clientSocket = listenSocket.accept();
        Connection c = new Connection(clientSocket);
    }
      } catch(IOException e) {System.out.println("Listen socket:"+e.getMessage());}
}}

class Connection extends Thread {
        DataInputStream in;
        DataOutputStream out;
        Socket clientSocket;
public Connection (Socket aClientSocket) {
    try {
    clientSocket = aClientSocket;
    in = new DataInputStream( clientSocket.getInputStream());
    out =new DataOutputStream( clientSocket.getOutputStream());
    this.start();
     } catch(IOException e) {System.out.println("Connection:"+e.getMessage());}
}

public void run(){
     try {                             // an echo server
    String data = in.readUTF();     // read a line of data from the stream
    out.writeUTF(data);
     }catch (EOFException e){System.out.println("EOF:"+e.getMessage());
     } catch(IOException e) {System.out.println("readline:"+e.getMessage());
     } finally{ try {clientSocket.close();}catch (IOException e){/*close failed*/}}
}}

Request for Solution File

Ask an Expert for Answer!!
Programming Languages: Tcp-based distributed applications
Reference No:- TGS01510

Expected delivery within 24 Hours