Sei sulla pagina 1di 9

Lab Nr 7

Cod

Client
package com.company;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.util.Scanner;

public class Client {


Socket requestSocket;
ObjectOutputStream out;
ObjectInputStream in;
String message;

Client() {
}

void run() {
try {
//1. creating a socket to connect to the server
requestSocket = new Socket("localhost", 9991);
System.out.println("Connected to localhost in port 9991");
//2. get Input and Output streams
out = new ObjectOutputStream(requestSocket.getOutputStream());
out.flush();
in = new ObjectInputStream(requestSocket.getInputStream());
OutputStream outputStream = requestSocket.getOutputStream();
Scanner sc = new Scanner(System.in);
String ConMess = "";
//3: Communicating with the server
try {
message = (String) in.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("server>" + message);
do {
try {
message = (String) in.readObject();
System.out.println("server>" + message);
if (message.equals("show")) {
do {
message = (String) in.readObject();
System.out.println(message);
} while (!(message.equals("showed")));
} else if (message.equals("add")) {
message = (String) in.readObject();
System.out.println("server>" + message);
message = sc.nextLine();
sendMessage(message);
BufferedImage image = ImageIO.read(new
File("D:\\univer\\Anul
III\\Laboratoare\\PCD\\Laborator6ClientServerSoft\\src\\FileSend\\2.jpg"));

ByteArrayOutputStream byteArrayOutputStream = new


ByteArrayOutputStream();
ImageIO.write(image, "jpg", byteArrayOutputStream);
byte[] size =
ByteBuffer.allocate(4).putInt(byteArrayOutputStream.size()).array();
outputStream.write(size);

outputStream.write(byteArrayOutputStream.toByteArray());
outputStream.flush();
System.out.println("Flushed: " +
System.currentTimeMillis());

try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Closing: " +
System.currentTimeMillis());

} else {
message = sc.nextLine();
sendMessage(message);
}
} catch (ClassNotFoundException classNot) {
System.err.println("data received in unknown format");
}
} while (!message.equals("bye"));
} catch (UnknownHostException unknownHost) {
System.err.println("You are trying to connect to an unknown
host!");
} catch (IOException ioException) {
ioException.printStackTrace();
} finally {
//4: Closing connection
try {
in.close();
out.close();
requestSocket.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}

void sendMessage(String msg) {


try {
out.writeObject(msg);
out.flush();
System.out.println(msg);
} catch (IOException ioException) {
ioException.printStackTrace();
}
}

public static void main(String args[]) {


Client client = new Client();
client.run();
}
}
Server
package com.company;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

public class Server{


ServerSocket ServerSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
String message;
int clientNumber=0;
String conMess="";
String client="";
String log="";
String pass="";
String tempLog="";
String tempPass="";
String com="";
String fileName="";
boolean exit=false;

Server(){}
void run()
{
try{
ServerSocket = new ServerSocket(9991, 10);
System.out.println("Waiting for connection");
connection = ServerSocket.accept();
clientNumber++;
System.out.println("Connection received from " +
connection.getInetAddress().getHostName());
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());
InputStream inputStream = connection.getInputStream();
sendMessage("Connection successful esti client
numar="+clientNumber);
//Scanner sc=new Scanner(System.in);
do{
try{
sendMessage("Ce directorie accesati?");
message = (String)in.readObject();
//System.out.println("client>" + message);
client=message;
switch (client){
case "client1":
log = "client1";
pass = "client1";
sendMessage("Introdu login pentru " + client +
" : ");
tempLog = (String) in.readObject();
//System.out.println("client>" + message);
sendMessage("Introduceti parola pentru " + client
+ " :");
tempPass = (String) in.readObject();
if (tempLog.equals(log) && tempPass.equals(pass))
{
sendMessage("Autentificare Reusita");
while(!exit) {
sendMessage("\"Introduceti una din
variante : show||add||delete||exit\"");
com = (String) in.readObject();
switch (com) {
case "show":
sendMessage("show");
List<String> results = new
ArrayList<String>();
File[] files = new
File("D:\\univer\\Anul
III\\Laboratoare\\PCD\\Laborator6ClientServerSoft\\src\\Client1").listFiles()
;
for (File file : files) {
if (file.isFile()) {

sendMessage(file.getName());

results.add(file.getName());
}
}
sendMessage("showed");
break;
case "add":
sendMessage("add");
sendMessage("Introdu denumirea
imaginii:");
fileName = (String)
in.readObject();
byte[] sizeAr = new byte[4];
inputStream.read(sizeAr);
int size =
ByteBuffer.wrap(sizeAr).asIntBuffer().get();

byte[] imageAr = new byte[size];


inputStream.read(imageAr);

BufferedImage image =
ImageIO.read(new ByteArrayInputStream(imageAr));
System.out.println("Received " +
image.getHeight() + "x" + image.getWidth() + ": " +
System.currentTimeMillis());
ImageIO.write(image, "jpg", new
File("D:\\univer\\Anul
III\\Laboratoare\\PCD\\Laborator6ClientServerSoft\\src\\Client3\\" +
fileName));
sendMessage("Receptionat");
break;
case "delete":
sendMessage("Introdu denumirea
fiserului care doresti sa-l stergi:");
fileName = (String)
in.readObject();
File file = new
File("D:\\univer\\Anul
III\\Laboratoare\\PCD\\Laborator6ClientServerSoft\\src\\Client1\\" +
fileName);
if (file.delete()) {
sendMessage("Fisierul " +
fileName + " a fost sters");
} else {
sendMessage("Fisierul " +
fileName + " nu a fost sters");
}
break;
case "exit":
sendMessage("O zi buna");
exit = true;
break;
default:
sendMessage("Mesaj gresit");
}
}

} else {
sendMessage("Eroare de conexinue!!!");
break;
}
break;
case "client2":
log = "client2";
pass = "client2";
sendMessage("Introdu login pentru " + client +
" : ");
tempLog = (String) in.readObject();
//System.out.println("client>" + message);
sendMessage("Introduceti parola pentru " + client
+ " :");
tempPass = (String) in.readObject();
if (tempLog.equals(log) && tempPass.equals(pass))
{
sendMessage("Autentificare Reusita");
while(!exit) {
sendMessage("\"Introduceti una din
variante : show||add||delete||exit\"");
com = (String) in.readObject();
switch (com) {
case "show":
sendMessage("show");
List<String> results = new
ArrayList<String>();
File[] files = new
File("D:\\univer\\Anul
III\\Laboratoare\\PCD\\Laborator6ClientServerSoft\\src\\Client2").listFiles()
;
for (File file : files) {
if (file.isFile()) {

sendMessage(file.getName());

results.add(file.getName());
}
}
sendMessage("showed");
break;
case "add":
sendMessage("add");
sendMessage("Introdu denumirea
imaginii:");
fileName = (String)
in.readObject();
byte[] sizeAr = new byte[4];
inputStream.read(sizeAr);
int size =
ByteBuffer.wrap(sizeAr).asIntBuffer().get();

byte[] imageAr = new byte[size];


inputStream.read(imageAr);

BufferedImage image =
ImageIO.read(new ByteArrayInputStream(imageAr));
System.out.println("Received " +
image.getHeight() + "x" + image.getWidth() + ": " +
System.currentTimeMillis());
ImageIO.write(image, "jpg", new
File("D:\\univer\\Anul
III\\Laboratoare\\PCD\\Laborator6ClientServerSoft\\src\\Client2\\" +
fileName));
sendMessage("Receptionat");
break;
case "delete":
sendMessage("Introdu denumirea
fiserului care doresti sa-l stergi:");
fileName = (String)
in.readObject();
File file = new
File("D:\\univer\\Anul
III\\Laboratoare\\PCD\\Laborator6ClientServerSoft\\src\\Client2\\" +
fileName);
if (file.delete()) {
sendMessage("Fisierul " +
fileName + " a fost sters");
} else {
sendMessage("Fisierul " +
fileName + " nu a fost sters");
}
break;
case "exit":
sendMessage("O zi buna");
exit = true;
break;
default:
sendMessage("Mesaj gresit");
}
}

} else {
sendMessage("Eroare de conexinue!!!");
break;
}
break;
case "client3":
log = "client3";
pass = "client3";
sendMessage("Introdu login pentru " + client +
" : ");
tempLog = (String) in.readObject();
//System.out.println("client>" + message);
sendMessage("Introduceti parola pentru " + client
+ " :");
tempPass = (String) in.readObject();
if (tempLog.equals(log) && tempPass.equals(pass))
{
sendMessage("Autentificare Reusita");
while(!exit) {
sendMessage("\"Introduceti una din
variante : show||add||delete||exit\"");
com = (String) in.readObject();
switch (com) {
case "show":
sendMessage("show");
List<String> results = new
ArrayList<String>();
File[] files = new
File("D:\\univer\\Anul
III\\Laboratoare\\PCD\\Laborator6ClientServerSoft\\src\\Client3").listFiles()
;
for (File file : files) {
if (file.isFile()) {

sendMessage(file.getName());

results.add(file.getName());
}
}
sendMessage("showed");
break;
case "add":
sendMessage("add");
sendMessage("Introdu denumirea
imaginii:");
fileName = (String)
in.readObject();
byte[] sizeAr = new byte[4];
inputStream.read(sizeAr);
int size =
ByteBuffer.wrap(sizeAr).asIntBuffer().get();

byte[] imageAr = new byte[size];


inputStream.read(imageAr);

BufferedImage image =
ImageIO.read(new ByteArrayInputStream(imageAr));
System.out.println("Received " +
image.getHeight() + "x" + image.getWidth() + ": " +
System.currentTimeMillis());
ImageIO.write(image, "jpg", new
File("D:\\univer\\Anul
III\\Laboratoare\\PCD\\Laborator6ClientServerSoft\\src\\Client3\\" +
fileName));
sendMessage("Receptionat");
break;
case "delete":
sendMessage("Introdu denumirea
fiserului care doresti sa-l stergi:");
fileName = (String)
in.readObject();
File file = new
File("D:\\univer\\Anul
III\\Laboratoare\\PCD\\Laborator6ClientServerSoft\\src\\Client3\\" +
fileName);
if (file.delete()) {
sendMessage("Fisierul " +
fileName + " a fost sters");
} else {
sendMessage("Fisierul " +
fileName + " nu a fost sters");
}
break;
case "exit":
sendMessage("O zi buna");
exit = true;
break;
default:
sendMessage("Mesaj gresit");
}
}

} else {
sendMessage("Eroare de conexinue!!!");
break;
}
break;
default:

}
if (message.equals("bye")){
sendMessage("bye");
conMess="bye";
sendMessage(conMess);
}

// System.out.println("Mesaj catre client:");


// conMess=sc.nextLine();
//sendMessage(conMess);
}
catch(ClassNotFoundException classnot){
System.err.println("Data received in unknown format");
}
}while(!message.equals("bye"));
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
in.close();
out.close();
ServerSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
void sendMessage(String msg)
{
try{
out.writeObject(msg);
out.flush();
System.out.println("server>" + msg);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
public static void main(String args[])
{
Server server = new Server();
while(true){
server.run();
}
}
}

Potrebbero piacerti anche