Skip to content

Commit ade11ed

Browse files
committed
assignment for socket programming.
1 parent a2c1841 commit ade11ed

File tree

1 file changed

+93
-0
lines changed
  • Records/21CYS/CB.EN.U4CYS21018/assignment/12-06-cbenu4cys21018-gokul

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import java.io.FileOutputStream;
2+
import java.io.IOException;
3+
4+
public class Main {
5+
6+
abstract static class FileTransfer {
7+
8+
void sendFile(String fileName) {
9+
// Default implementation for sending a file
10+
System.out.println("Sending file: " + fileName);
11+
}
12+
13+
abstract void saveFile(byte[] fileData, String fileName);
14+
}
15+
16+
public interface FileTransferListener {
17+
void onFileSent(String fileName);
18+
void onFileSaved(String fileName);
19+
}
20+
21+
public static class FileTransferClient extends FileTransfer implements FileTransferListener {
22+
23+
void sendFile(String fileName) {
24+
// Custom implementation for sending a file using socket programming
25+
System.out.println("Sending file over the network: " + fileName);
26+
}
27+
28+
public void onFileSaved(String fileName) {
29+
System.out.println("File saved on the client side: " + fileName);
30+
}
31+
32+
public void onFileSent(String fileName) {
33+
System.out.println("File sent from the client side: " + fileName);
34+
}
35+
36+
void saveFile(byte[] fileData, String fileName) {
37+
// Not applicable for the client side
38+
}
39+
}
40+
41+
public static class FileTransferServer extends FileTransfer implements FileTransferListener {
42+
43+
@Override
44+
void saveFile(byte[] fileData, String fileName) {
45+
try {
46+
FileOutputStream fileOutputStream = new FileOutputStream(fileName);
47+
fileOutputStream.write(fileData);
48+
fileOutputStream.close();
49+
} catch (IOException e) {
50+
System.out.println("Failed to save file: " + fileName);
51+
return;
52+
}
53+
54+
onFileSaved(fileName);
55+
}
56+
57+
void sendFile(String fileName) {
58+
// Not applicable for the server side
59+
}
60+
61+
public void onFileSaved(String fileName) {
62+
System.out.println("File saved on the server side: " + fileName);
63+
}
64+
65+
public void onFileSent(String fileName) {
66+
System.out.println("File sent from the server side: " + fileName);
67+
}
68+
69+
void start() {
70+
// Server initialization and listening for incoming file transfers
71+
System.out.println("Server started. Listening for incoming file transfers.");
72+
}
73+
}
74+
75+
public static void main(String[] args) {
76+
FileTransferClient client = new FileTransferClient();
77+
FileTransferServer server = new FileTransferServer();
78+
79+
// Start the server to listen for incoming file transfers
80+
server.start();
81+
82+
// Demonstrate sending a file from the client to the server
83+
client.sendFile("file.txt");
84+
85+
// Demonstrate saving a file on the server
86+
byte[] fileData = "This has been overwritten from here".getBytes();
87+
server.saveFile(fileData, "file.txt");
88+
89+
// print the contents of the file from server side
90+
System.out.println("File contents on the server side: " + new String(fileData));
91+
92+
}
93+
}

0 commit comments

Comments
 (0)