· 8 years ago · Dec 15, 2016, 04:08 AM
1public static void main(String[] args) throws IOException {
2 FileOutputStream fos;
3 BufferedOutputStream bos;
4 OutputStream output;
5 DataOutputStream dos;
6 int len;
7 int smblen;
8 InputStream in;
9 boolean flag = true;
10 DataInputStream clientData;
11 BufferedInputStream clientBuff;
12
13 ServerSocket serverSocket = new ServerSocket(5991);
14 serverSocket.setSoTimeout(500000);
15 System.out.println("Waiting for client on port " + serverSocket.getLocalPort());
16
17 Socket clientSocket = null;
18 clientSocket = serverSocket.accept();
19 System.out.println("Just connected to " + clientSocket.getRemoteSocketAddress());
20
21 while (true){
22 while(flag==true) {
23 in = clientSocket.getInputStream();
24 clientData = new DataInputStream(in);
25 clientBuff = new BufferedInputStream(in);
26
27 int fileSize = clientData.read();
28 if (fileSize != 0)
29 System.out.println("Receiving " + fileSize + " files.n");
30
31 //Store filenames and file sizes from client directory
32 ArrayList<File>files=new ArrayList<File>(fileSize);
33 ArrayList<Integer>sizes = new ArrayList<Integer>(fileSize);
34
35 //Server accepts filenames
36 for (int count=0; count<fileSize; count ++){
37 File ff=new File(clientData.readUTF());
38 files.add(ff);
39 }
40
41 for (int count=0; count<fileSize; count ++){
42 sizes.add(clientData.readInt());
43 }
44
45 for (int count=0; count<fileSize; count++) {
46
47 if (fileSize - count == 1) {
48 flag = false;
49 }
50
51 len = sizes.get(count);
52
53 output = new FileOutputStream("/home/pp/Desktop/inResources/" + files.get(count));
54 dos = new DataOutputStream(output);
55 bos = new BufferedOutputStream(output);
56
57 byte[] buffer = new byte[1024];
58
59 bos.write(buffer, 0, buffer.length);
60
61 while (len > 0 && (smblen = clientData.read(buffer)) > 0) {
62 dos.write(buffer, 0, smblen);
63 len = len - smblen;
64 dos.flush();
65 }
66 dos.close();
67
68 System.out.println("File " + files.get(count) + " with " + sizes.get(count) + " bytes recieved.");
69 }
70
71 }
72
73 if (flag == false) {
74 System.out.println("nTransfer completed. Closing socket...");
75 serverSocket.close();
76 break;
77 }
78 }
79}
80
81public static void main(String[] args) throws IOException {
82 String serverName = "192.168.1.12"; //IP address
83 int port = 5991;
84
85 Socket sock = new Socket(serverName, port);
86 System.out.println("Connected to " + serverName + " on port " + port + "n");
87
88
89 File myFile = new File("C:\Users\inter2\Desktop\noobs\outResources");
90 File[] files = myFile.listFiles();
91
92 OutputStream os = sock.getOutputStream();
93 DataOutputStream dos = new DataOutputStream(os);
94
95 //Sending total number of files in folder
96 dos.writeInt(files.length);
97
98 //Sending file names
99 for (int count=0; count<files.length; count++) {
100 dos.writeUTF(files[count].getName());
101 }
102
103 //Sending file sizes
104 for (int count=0; count<files.length; count++) {
105 int filesize = (int) files[count].length();
106 dos.writeInt(filesize);
107 }
108
109 //Sending of files
110 for (int count=0; count<files.length; count ++) {
111 int filesize = (int) files[count].length();
112 byte [] buffer = new byte [filesize];
113
114 FileInputStream fis = new FileInputStream(files[count].toString());
115 BufferedInputStream bis = new BufferedInputStream(fis);
116
117 //Sending file name and file size to the server
118 bis.read(buffer, 0, buffer.length);
119
120 dos.write(buffer, 0, buffer.length);
121 dos.flush();
122
123
124 System.out.println("Sending file " + files[count].getName() + " with " + filesize + " bytes.");
125 }
126
127 System.out.println("n" + files.length + " files successfully transfered.");
128 sock.close();
129}