0% found this document useful (0 votes)
8 views

Assignment 7

Uploaded by

Karan Margaje
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Assignment 7

Uploaded by

Karan Margaje
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

7. Memory Management

a) Write a program to increment counter in Shared memory.

Code:

Server.java

package Shared_Memory;

import java.io.*;

import java.net.*;

import java.text.*;

import java.util.*;

/**

* @author Karan

*/

public class server {

public static void main(String args[]) throws IOException{

ServerSocket ss =new ServerSocket(5056);

System.out.println("Server is ready!");

while(true){

Socket s=null;

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

try{

s =ss.accept();

System.out.println("A new client is connected : " + s);

DataInputStream dis = new DataInputStream(s.getInputStream());

DataOutputStream dos = new DataOutputStream(s.getOutputStream());

System.out.println("Assigning new thread for this client");

Thread t = new ClientHandler(s, dis, dos);

t.start();

catch(Exception e)

s.close();

e.printStackTrace();

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

class ClientHandler extends Thread

final DataInputStream dis;

final DataOutputStream dos;

final Socket s;

static int val;

// Constructor

public ClientHandler(Socket s, DataInputStream dis, DataOutputStream dos)

this.s = s;

this.dis = dis;

this.dos = dos;

@Override

public void run()

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

String received;

String toreturn;

while (true)

try {

++val;

dos.writeUTF("Thread: " + val);

received = dis.readUTF();

if(received.equals("Exit"))

System.out.println("Client " + this.s + " sends exit...");

System.out.println("Closing this connection.");

this.s.close();

System.out.println("Connection closed");

break;

} catch (IOException e) {

e.printStackTrace();

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

try

this.dis.close();

this.dos.close();

}catch(IOException e){

e.printStackTrace();

Client.java

package Shared_Memory;

import java.io.*;

import java.net.*;

import java.util.*;

/**

* @author Karan

*/

public class client {

public static void main(String args[]) throws IOException{

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

try{

Scanner scan=new Scanner(System.in);

InetAddress ip=InetAddress.getByName("localhost");

Socket s=new Socket(ip,5056);

DataInputStream dis = new DataInputStream(s.getInputStream());

DataOutputStream dos = new DataOutputStream(s.getOutputStream());

String received = dis.readUTF();

System.out.println(received);

String tosend = scan.nextLine();

dos.writeUTF(tosend);

scan.close();

dis.close();

dos.close();

s.close();

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

catch(Exception e)

e.printStackTrace();

Output:

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020
SIES College of Management Studies TYMCA, Sem-V, Roll No : 24

Subject: MCAL502 [Choice Based] Open Source System for ADC Lab Nov 2020

You might also like