Skip to content

Commit 01a0252

Browse files
committed
commit Exercise_31_07
1 parent c9634a6 commit 01a0252

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package exercise_31_07Client;
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.Scanner;
8+
9+
public class Exercise_31_07Client {
10+
11+
public static void main(String[] args) {
12+
System.out.println("Enter a count to get the get the last x pcs of prime numbers from PrimeNumbers.dat: ");
13+
int count = new Scanner(System.in).nextInt();
14+
long[] primes;
15+
16+
try {
17+
Socket socket = new Socket("localhost", 8000);
18+
ObjectInputStream inputFromServer = new ObjectInputStream(socket.getInputStream());
19+
DataOutputStream outputToServer = new DataOutputStream(socket.getOutputStream());
20+
outputToServer.writeInt(count);
21+
System.out.println("Count was sent to server.");
22+
23+
primes = (long[])(inputFromServer.readObject());
24+
25+
int i = 1;
26+
System.out.println("The last " + count + " primes from PrimeNumbers.dat are: ");
27+
for(long number : primes) {
28+
if(i % 10 == 0) {
29+
System.out.println(number);
30+
}
31+
else {
32+
System.out.print(number + " ");
33+
}
34+
i++;
35+
}
36+
}
37+
catch (IOException ex) {
38+
ex.printStackTrace();
39+
}
40+
catch (ClassNotFoundException ex) {
41+
ex.printStackTrace();
42+
}
43+
}
44+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package exercise_31_07Server;
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+
11+
public class Exercise_31_07Server {
12+
13+
public static void main(String[] args) {
14+
try {
15+
ServerSocket serverSocket = new ServerSocket(8000);
16+
System.out.println("Server started!");
17+
Socket socket = serverSocket.accept();
18+
System.out.println("Client has connected.");
19+
20+
DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
21+
ObjectOutputStream outputToClient = new ObjectOutputStream(socket.getOutputStream());
22+
23+
int count = inputFromClient.readInt();
24+
System.out.println("Starting to read out the last " + count + " primes from file...");
25+
26+
long[] primes = getArray(count);
27+
outputToClient.writeObject(primes);
28+
System.out.println("Array with the primes was sent.");
29+
}
30+
catch (IOException ex) {
31+
ex.printStackTrace();
32+
}
33+
}
34+
35+
public static long[] getArray(int countOfPrimes) {
36+
long[] primes = new long[countOfPrimes];
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[i] = 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+
}

0 commit comments

Comments
 (0)