EX 2
Program:
Server Program :
import java.net.*;
import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Server {
public static void main(String[] args) {
ServerSocket server = null;
Socket socket = null;
try {
server = new ServerSocket(4000);
System.out.println("Server waiting for image...");
while (true) { // Listen for multiple clients
try {
socket = server.accept();
System.out.println("Client connected: " + socket.getInetAddress());
InputStream in = socket.getInputStream();
DataInputStream dis = new DataInputStream(in);
int len = dis.readInt();
System.out.println("Image Size: " + len + " bytes (" + len / 1024 + " KB)");
byte[] data = new byte[len];
dis.readFully(data);
System.out.println("Image received. Displaying...");
InputStream ian = new ByteArrayInputStream(data);
BufferedImage bImage = ImageIO.read(ian);
// Display the image in a JFrame
JFrame f = new JFrame("Server - Received Image");
ImageIcon icon = new ImageIcon(bImage);
JLabel l = new JLabel(icon);
f.getContentPane().add(l);
f.pack();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // Dispose, don't exit all
f.setVisible(true);
// Close client-specific streams and socket
ian.close();
dis.close();
in.close();
socket.close();
System.out.println("Client connection closed.");
} catch (EOFException e) {
System.out.println("Client disconnected unexpectedly. No data received.");
} catch (IOException e) {
System.out.println("IOException with client: " + e.getMessage());
} finally {
try {
if (socket != null && !socket.isClosed()) {
socket.close();
}
} catch (IOException ioException) {
System.out.println("Error closing client socket: " + ioException.getMessage());
}
}
}
} catch (IOException e) {
System.out.println("Server error: " + e.getMessage());
e.printStackTrace();
} finally {
try {
if (server != null && !server.isClosed()) {
server.close();
}
} catch (IOException ioException) {
System.out.println("Error closing server socket: " + ioException.getMessage());
}
}
}
}
Client Program:
import javax.imageio.ImageIO;
import java.io.*;
import java.net.Socket;
import java.awt.image.BufferedImage;
public class client {
public static void main(String[] args) {
Socket socket = null;
BufferedImage img = null;
String filePath = "D:\\kkkk.jpg"; // Assuming the image is in the same directory
try {
// Connect to server on localhost and port 4000
socket = new Socket("localhost", 4000);
System.out.println("Client is running.");
// Read the image from disk.
System.out.println("Reading image from disk.");
File file = new File(filePath);
if (!file.exists()) {
System.out.println("Error: Image file not found at " + filePath);
return; // Exit if the file doesn't exist
}
img = ImageIO.read(file);
if (img == null) {
System.out.println("Error: Could not read image from file.");
return; // Exit if the image couldn't be read
}
// Convert image to byte array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, "jpeg", baos);
byte[] imageBytes = baos.toByteArray();
baos.close();
// Send the image to the server
System.out.println("Sending image to server. Size: " + imageBytes.length + " bytes.");
OutputStream out = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
dos.writeInt(imageBytes.length); // Send image size first
dos.write(imageBytes, 0, imageBytes.length); // Send image bytes
System.out.println("Image sent to server.");
// Close streams and socket.
dos.close();
out.close();
socket.close();
} catch (IOException e) {
System.out.println("IOException: " + e.getMessage());
e.printStackTrace();
} catch (Exception e) {
System.out.println("General Exception: " + e.getMessage());
e.printStackTrace();
} finally {
try {
if (socket != null && !socket.isClosed()) {
socket.close();
}
} catch (IOException ioException) {
System.out.println("Error closing socket: " + ioException.getMessage());
}
}
}
}
OUTPUT: