commit 78b57e0eb38aaedfd65ab94ef53ea85b6a234167 Author: PrismRealm Date: Sun Mar 17 23:28:35 2019 +0800 Initial commit diff --git a/Problem 3.16/SocketClient.class b/Problem 3.16/SocketClient.class new file mode 100644 index 0000000..0617585 Binary files /dev/null and b/Problem 3.16/SocketClient.class differ diff --git a/Problem 3.16/SocketClient.java b/Problem 3.16/SocketClient.java new file mode 100644 index 0000000..7f89636 --- /dev/null +++ b/Problem 3.16/SocketClient.java @@ -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); + } + } +} \ No newline at end of file diff --git a/Problem 3.16/SocketServer.class b/Problem 3.16/SocketServer.class new file mode 100644 index 0000000..4cbe636 Binary files /dev/null and b/Problem 3.16/SocketServer.class differ diff --git a/Problem 3.16/SocketServer.java b/Problem 3.16/SocketServer.java new file mode 100644 index 0000000..ffeeab3 --- /dev/null +++ b/Problem 3.16/SocketServer.java @@ -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); + } + } +} diff --git a/Problem 3.18/SocketClient.class b/Problem 3.18/SocketClient.class new file mode 100644 index 0000000..d7e35fc Binary files /dev/null and b/Problem 3.18/SocketClient.class differ diff --git a/Problem 3.18/SocketClient.java b/Problem 3.18/SocketClient.java new file mode 100644 index 0000000..cadc1d5 --- /dev/null +++ b/Problem 3.18/SocketClient.java @@ -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) {} + } + } +} \ No newline at end of file diff --git a/Problem 3.18/SocketServer.class b/Problem 3.18/SocketServer.class new file mode 100644 index 0000000..b97a2ab Binary files /dev/null and b/Problem 3.18/SocketServer.class differ diff --git a/Problem 3.18/SocketServer.java b/Problem 3.18/SocketServer.java new file mode 100644 index 0000000..d359208 --- /dev/null +++ b/Problem 3.18/SocketServer.java @@ -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); + } + } +} diff --git a/Problem 3.20/main.c b/Problem 3.20/main.c new file mode 100644 index 0000000..c26238c --- /dev/null +++ b/Problem 3.20/main.c @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include +#include +#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; +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..9edacb9 --- /dev/null +++ b/README.md @@ -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` 上