Ask Question, Ask an Expert


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*; import*;
public class TCPClient {
public static void main (String args[]) { // arguments supply message and hostname
     Socket s = null;
           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());}}}}

public class TCPServer {
public static void main (String args[]) {
    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());
     } 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
     }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

Operating systems assignment-1 binary record sorting in

Operating Systems Assignment- 1. Binary Record Sorting in Ascending Order| You will write a simple sorting program. This program should be invoked as follows: shell% ./fastsort -i inputfile -o outputfile The above line m ...

1 tic tac toein a tic tac toe game two players x and o take

1 TIC TAC TOE In a Tic Tac Toe game two players, X and O, take turns to mark the spaces in a 3X3 grid. The winner will be the player who first succeeds in placing three of his/her marks in a horizontal, vertical, or diag ...

The given machine description will provide the basis for

The given machine description will provide the basis for this assignment. You will create a virtual machine/operating system for the machine described below that will accept programs in the target machine language. The d ...

Questioncomplete tutorial 10 case problem 2 - ridgewood

Question: Complete Tutorial 10 Case Problem 2 - Ridgewood Herald Tribune found on pages 734-736 of your textbook. Complete the entire website assignment and upload your files to your 000WebHost account. After uploading t ...

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, ...

This assignment introduces you to the perl language and cgi

This assignment introduces you to the Perl language and CGI programming. You will use your scripting and html skills to build a Perl program that will gather user input to then generate a personal webpage. This program w ...

The briefyour small development team of ideally three

The Brief Your small development team of (ideally) three people has been asked to implement and test the Human Resource Information System desktop application. Your software product will be a database- backed desktop app ...

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 ...

Programming assignment 1 grocery storethis assignment

Programming Assignment #1: Grocery Store This assignment attempts to serve as a refresher for concepts that I hope you learned in CS 122. In this assignment, you will be building a simple storefront for a small grocery s ...

Pair programming phase 1talent agency user stories1 user

Pair Programming Phase 1 Talent Agency User Stories 1. User Story 1 As a head office administrator I want to be able to produce formatted output of all the information about our talent agencies so that I can easily incor ...

  • 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