Ask Question, Ask an Expert

+1-415-315-9853

info@mywordsolution.com

Ask Programming Language Expert

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.prepareUTF(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.prepareUTF(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*/}}
}}

Programming Language, Programming

  • Category:- Programming Language
  • Reference No.:- M91510

Have any Question? 


Related Questions in Programming Language

Assignmentthis assignment will be marked out of 100 and

Assignment This assignment will be marked out of 100 and carries 30% of the overall module weighting. Your .java files and report for this part must be uploaded to WebLearn and submitted by 3pm on Wednesday 27th April 20 ...

Write a program to test for the case of a math equation

Write a program to test for the case of a math equation entered via the keyboard using the SWITCH-CASE operator. Types of equations to test for: x + y  x / y   x * y and x - y. The program is to ask for the following: 1. ...

Shell programmingyou have created the directory structure

Shell Programming You have created the directory structure and some base files to be used by the Web server and Web site. For the Web site to be created in a production environment, you need to package your commands in a ...

Purposethe purpose of this assignment is to practice using

Purpose: The purpose of this assignment is to practice using loops, selections, and other basic features of Java in order to solve a real-world problem. Program Description: The Jeddah Broker Company is one of the compan ...

You must do this assignment correctly as described

You must do this assignment correctly as described below.  If you do not follow the directions or break the rules you will receive a 0 score. Simulation of checking tic-tac-toe board for wins by counting X and O in rows, ...

Write a program that calculates several possible tips to

Write a program that calculates several possible tips to give to a waiter at a restaurant. Ask the user to enter the total cost of the meal and then calculate a tip at 10%, 12.5%, 15%, 17.5%, and 20%. Write the original ...

Csis program homeworkwrite a program where the user will

CSIS program homework Write a program where the user will enter a number between 1 and 50 representing a state. The program should display the full name of that state. Assume the states are in alphabetical order, that is ...

Integrative programmingassignment- managing a user account

Integrative Programming Assignment- Managing a User Account List Objective: To manage a user list that can be modified and saved to a text file. Inputs: • Input text file consisting of pairs of usernames and passwords, s ...

Engineering programmingi need this now please accurate

Engineering programming I need this now please accurate answers and must be high rated Problem #1 Write a program that asks the user to input the number of miles and convert the miles to kilometers, and then print the ou ...

Programming assignment- implement a udp client and udp

PROGRAMMING ASSIGNMENT- Implement a UDP client and UDP server that support simple timeout-based retransmission with AIMD congestion control: -The client should send as many packets as are in its CURWIN (current window) a ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

A cola-dispensing machine is set to dispense 9 ounces of

A cola-dispensing machine is set to dispense 9 ounces of cola per cup, with a standard deviation of 1.0 ounce. The manuf

What is marketingbullwhat is marketing think back to your

What is Marketing? • "What is marketing"? Think back to your impressions before you started this class versus how you

Question -your client david smith runs a small it

QUESTION - Your client, David Smith runs a small IT consulting business specialising in computer software and techno

Inspection of a random sample of 22 aircraft showed that 15

Inspection of a random sample of 22 aircraft showed that 15 needed repairs to fix a wiring problem that might compromise

Effective hrmquestionhow can an effective hrm system help

Effective HRM Question How can an effective HRM system help facilitate the achievement of an organization's strate