Initial commit

This commit is contained in:
2019-03-17 23:28:35 +08:00
commit 78b57e0eb3
10 changed files with 188 additions and 0 deletions

Binary file not shown.

View File

@ -0,0 +1,24 @@
import java.net.*;
import java.io.*;
public class SocketClient {
public static void main(String[] args) {
try {
// make connection to server socket
Socket sock = new Socket("127.0.0.1", 6017);
InputStream in = sock.getInputStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(in));
// get a qoute from the socket
String line;
while ( (line = bin.readLine()) != null)
System.out.println(line);
// close the socket connection
sock.close();
}
catch (IOException ioe) {
System.err.println(ioe);
}
}
}

Binary file not shown.

View File

@ -0,0 +1,36 @@
import java.net.*;
import java.io.*;
public class SocketServer {
public static String[] qoutes = {
"Don't cry because it's over, smile because it happened.",
"Be yourself; everyone else is already taken.",
"Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.",
"So many books, so little time.",
"Be who you are and say what you feel, because those who mind don't matter, and those who matter don't mind."
};
public static void main(String[] args) {
try {
int i = 0;
ServerSocket sock = new ServerSocket(6017);
// Listening for connections
while (true) {
Socket client = sock.accept();
PrintWriter pout = new PrintWriter(client.getOutputStream(), true);
// write a qoute to the socket
pout.println(qoutes[i]);
i = (i + 1) % qoutes.length;
// close the socket and resume
// listening for connections
client.close();
}
}
catch (IOException ioe) {
System.err.println(ioe);
}
}
}

Binary file not shown.

View File

@ -0,0 +1,35 @@
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class SocketClient {
public static void main(String[] args) {
Socket sock = null;
Scanner scanner = null;
try {
sock = new Socket("127.0.0.1", 6017);
scanner = new Scanner(System.in);
DataOutputStream outputStream = new DataOutputStream(sock.getOutputStream());
DataInputStream inputStream = new DataInputStream(sock.getInputStream());
while (true) {
try {
System.out.print("Input: ");
String message = scanner.nextLine();
outputStream.writeUTF(message);
System.out.println("Echo: " + inputStream.readUTF());
} catch (IOException e) {
System.err.println(e);
}
}
}
catch (IOException ioe) {
System.err.println(ioe);
}
finally {
try {
scanner.close();
sock.close();
} catch (IOException e) {}
}
}
}

Binary file not shown.

View File

@ -0,0 +1,37 @@
import java.net.*;
import java.io.*;
public class SocketServer {
public static void main(String[] args) {
ServerSocket sock = null;
Socket client = null;
try {
sock = new ServerSocket(6017);
client = sock.accept();
DataInputStream inputStream = new DataInputStream(client.getInputStream());
DataOutputStream outputStream = new DataOutputStream(client.getOutputStream());
while (true) {
try {
String message = inputStream.readUTF();
System.out.println("Client: " + message);
outputStream.writeUTF(message);
} catch (IOException e) {
if (e.getMessage().equals("null")) {
break;
}
}
}
inputStream.close();
outputStream.close();
client.close();
sock.close();
}
catch (IOException ioe) {
try {
sock.close();
client.close();
} catch (IOException e) {}
System.err.println(ioe);
}
}
}

52
Problem 3.20/main.c Normal file
View File

@ -0,0 +1,52 @@
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#define SIZE 65536
int main(int argc, char *argv[]) {
int pipefd[2]; // pipefd[0] represents input stream id, pipefd[1] represents output stream id.
char buffer[SIZE]; // The buffer of main process.
char childBuffer[SIZE]; // The buffer of sub-process.
// If the number of arguments is not correct.
if (argc != 3) {
perror("Main: main [target] [destination].\n");
return 1;
}
char *srcFile = argv[1]; // Source
char *dstFile = argv[2]; // Destination
// Building a pipe. If the returned integer is less than 0,
if (pipe(pipefd) < 0) {
printf("An error occured when createing the pipe: %s\n", strerror(errno));
return 1;
}
pid_t pid = fork(); // Create a sub-process.
if (pid == 0) { // pid == 0 represents it's a sub-process.
close(pipefd[1]); // Close pipefd[1]
ssize_t file_size = read(pipefd[0], childBuffer, sizeof(childBuffer)); // Get the size of child
close(pipefd[0]); // Close pipefd[0] since it is unable to use.
int dst_file_fd = open(dstFile, O_CREAT | O_WRONLY); // Read a file in writeonly mode. If the file does not exit, create a new one.
write(dst_file_fd, childBuffer, file_size); // Write data from buffer into fd.
close(dst_file_fd); // Close file
}
else if (pid > 0) { // pid > 0 represents it's a main process.
close(pipefd[0]); // Close pipefd[0]
int target_file_fd = open(srcFile, O_RDONLY); // Open a file in readonly mode.
ssize_t file_size = read(target_file_fd, buffer, sizeof(buffer)); // Get size of the file
write(pipefd[1], buffer, file_size); // Write data from buffer into pipefd[1]
close(pipefd[1]); // Close pipefd[1]
close(target_file_fd); // Close file
}
else { // When error occur
printf("An error occured when forking child process. %s\n", strerror(errno));
return 1;
}
return 0;
}

4
README.md Normal file
View File

@ -0,0 +1,4 @@
Operating System Second Assignment
===
Programming Problems 3.16, 3.18, 3.20 對應至目錄 `Problem 3.16`, `Problem 3.18``Problem 3.20`