Skip to content

Commit 04c947f

Browse files
committed
commit Exercise_31_08
1 parent 01a0252 commit 04c947f

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package exercise_31_08Server;
2+
3+
import java.io.DataInputStream;
4+
import java.io.FileNotFoundException;
5+
import java.io.IOException;
6+
import java.io.ObjectOutputStream;
7+
import java.io.RandomAccessFile;
8+
import java.net.ServerSocket;
9+
import java.net.Socket;
10+
import java.util.ArrayList;
11+
12+
public class Exercise_31_08Server {
13+
14+
public static void main(String[] args) {
15+
try {
16+
ServerSocket serverSocket = new ServerSocket(8000);
17+
System.out.println("Server started!");
18+
Socket socket = serverSocket.accept();
19+
System.out.println("Client has connected.");
20+
21+
DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
22+
ObjectOutputStream outputToClient = new ObjectOutputStream(socket.getOutputStream());
23+
24+
int count = inputFromClient.readInt();
25+
System.out.println("Starting to read out the last " + count + " primes from file...");
26+
27+
outputToClient.writeObject(getArrayList(count));
28+
System.out.println("Array with the primes was sent.");
29+
}
30+
catch (IOException ex) {
31+
ex.printStackTrace();
32+
}
33+
}
34+
35+
public static ArrayList<Long> getArrayList(int countOfPrimes) {
36+
ArrayList<Long> primes = new ArrayList<>();
37+
try(RandomAccessFile raf = new RandomAccessFile("PrimeNumbers.dat", "r");){
38+
raf.seek(raf.length() - 8 * countOfPrimes);
39+
for(int i = 0; i < countOfPrimes; i++) {
40+
primes.add(raf.readLong());
41+
}
42+
}
43+
catch (FileNotFoundException ex) {
44+
System.out.println("File not found");
45+
}
46+
catch (IOException ex) {
47+
System.out.println("IOException");
48+
}
49+
return primes;
50+
}
51+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package exercise_31_08_Client;
2+
3+
import java.io.DataOutputStream;
4+
import java.io.IOException;
5+
import java.io.ObjectInputStream;
6+
import java.net.Socket;
7+
import java.util.ArrayList;
8+
import java.util.Scanner;
9+
10+
public class Exercise_31_08_Client {
11+
12+
public static void main(String[] args) {
13+
System.out.println("Enter a count to get the get the last x pcs of prime numbers from PrimeNumbers.dat: ");
14+
int count = new Scanner(System.in).nextInt();
15+
ArrayList<Long> primes;
16+
17+
try {
18+
Socket socket = new Socket("localhost", 8000);
19+
ObjectInputStream inputFromServer = new ObjectInputStream(socket.getInputStream());
20+
DataOutputStream outputToServer = new DataOutputStream(socket.getOutputStream());
21+
outputToServer.writeInt(count);
22+
System.out.println("Count was sent to server.");
23+
24+
primes = (ArrayList<Long>)(inputFromServer.readObject());
25+
26+
int i = 1;
27+
System.out.println("The last " + count + " primes from PrimeNumbers.dat are: ");
28+
for(long number : primes) {
29+
if(i % 10 == 0) {
30+
System.out.println(number);
31+
}
32+
else {
33+
System.out.print(number + " ");
34+
}
35+
i++;
36+
}
37+
System.out.println();
38+
}
39+
catch (IOException ex) {
40+
ex.printStackTrace();
41+
}
42+
catch (ClassNotFoundException ex) {
43+
ex.printStackTrace();
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)