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

Assignmentwrite a console application to meet the following

Assignment Write a console application to meet the following requirements. Create a system for a simple library. The library has a name and a list of books. Each book has a title, author and an int as the id number. Defi ...

Generate code for the given three-address statements

Generate code for the given three-address statements assuming all variables are stored in memory locations. a) x = 1 b) x = a c) x = a + 1 d) x = a + b e) The two statements x = b * c y = a + x

Programming project assessmentinstructions the following

Programming Project Assessment Instructions: The following programming problem can be solved by a program that uses three basic tasks-Input Data, Process Data, and Output Results. To process the data, use file, looping, ...

Object-oriented design for the ordering systemyou will

object-oriented design for the ordering system You will continue development of the object-oriented design for the ordering system. The new content this week will be the Sequence and Collaboration Diagram for the system. ...

Assignmentquestion 1consider the following class the

Assignment Question 1 Consider the following class. The purpose of each block of code within the class is written in comments within the code. But the following class contains a number of errors. Errors can occur at comp ...

Assignmentpart 1 instance data and methods belong to

Assignment Part 1 Instance data and methods belong to objects (which are class members) and static data and methods belong to a class as a whole. Consider the real-life class named StatelnTheUnitedStates. Name some real- ...

Lab stackslab exerciseimplement the following classesclass

Lab: Stacks Lab Exercise: Implement the following classes: Class Course that includes three instance variables: private int ID; // course ID private String Name; // course name private Course next; // link next Your clas ...

Assignmentthe assignment is as follows use the program

Assignment The assignment is as follows: Use the program, Arrays of Pointers, on p. 188 of the text "Ch. 4, of Ivor Horton's Beginning Visual C++ 2010" as a starting point for this assignment. Refer to Appendix C for ful ...

Assignmentwrite a program that calculates the intersection

Assignment Write a program that calculates the intersection of two sets of numbers. The sets can be represented using arrays. The general idea is that A!=0 if i is in the set and A==0 if it is not. Array element A can th ...

Pseudocodes and flowchartxyz company employees work for 8

Pseudocodes and flowchart XYZ company employees work for 8 hours a day and 40 hours in a week. The employees are paid overtime at the rate of half of the salary. Employees who get more than 15000 must pay a supplementary ...

  • 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

WalMart Identification of theory and critical discussion

Drawing on the prescribed text and/or relevant academic literature, produce a paper which discusses the nature of group

Section onea in an atwood machine suppose two objects of

SECTION ONE (a) In an Atwood Machine, suppose two objects of unequal mass are hung vertically over a frictionless

Part 1you work in hr for a company that operates a factory

Part 1: You work in HR for a company that operates a factory manufacturing fiberglass. There are several hundred empl

Details on advanced accounting paperthis paper is intended

DETAILS ON ADVANCED ACCOUNTING PAPER This paper is intended for students to apply the theoretical knowledge around ac

Create a provider database and related reports and queries

Create a provider database and related reports and queries to capture contact information for potential PC component pro