uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

commit bc6cbf59b727923f884167b262002bdce3d852f4
parent eacb5dfe252aad9a4d0ceb52cc94eea380ed40a6
Author: Christos Margiolis <christos@margiolis.net>
Date:   Sat, 15 Jan 2022 22:10:23 +0200

new java code

Diffstat:
Ajava_net/net_programming/EchoClient.java | 37+++++++++++++++++++++++++++++++++++++
Ajava_net/net_programming/EchoServer.java | 37+++++++++++++++++++++++++++++++++++++
Ajava_net/net_programming/HTTPServer.html | 31+++++++++++++++++++++++++++++++
Ajava_net/net_programming/HTTPServer.java | 48++++++++++++++++++++++++++++++++++++++++++++++++
Ajava_net/net_programming/ShowIP.java | 23+++++++++++++++++++++++
Ajava_net/net_programming/UDPClient.java | 41+++++++++++++++++++++++++++++++++++++++++
Ajava_net/net_programming/UDPServer.java | 35+++++++++++++++++++++++++++++++++++
7 files changed, 252 insertions(+), 0 deletions(-)

diff --git a/java_net/net_programming/EchoClient.java b/java_net/net_programming/EchoClient.java @@ -0,0 +1,37 @@ +import java.net.*; +import java.io.*; + +public class EchoClient { + public static void main(String[] args) throws IOException { + Socket echosock; + PrintWriter out; + BufferedReader in, stdin; + String input; + String hostname = "127.0.0.1"; + int port = 7777; + + if (args.length == 2) { + hostname = args[0]; + port = Integer.parseInt(args[1]); + } else + System.err.println("usage: java EchoClient [hostname] [port]"); + try { + echosock = new Socket(hostname, port); + out = new PrintWriter(echosock.getOutputStream(), true); + in = new BufferedReader(new InputStreamReader( + echosock.getInputStream())); + stdin = new BufferedReader(new InputStreamReader(System.in)); + + System.out.print("> "); + while ((input = stdin.readLine()) != null) { + out.println(input); + System.out.println("echo: " + in.readLine()); + System.out.print("> "); + } + } catch (UnknownHostException e) { + System.err.println("unknown host: " + hostname); + } catch (IOException e) { + System.err.println("io exception: " + e.getMessage()); + } + } +} diff --git a/java_net/net_programming/EchoServer.java b/java_net/net_programming/EchoServer.java @@ -0,0 +1,37 @@ +import java.net.*; +import java.io.*; + +public class EchoServer { + public static void main(String[] args) throws IOException { + ServerSocket srvsock; + Socket cltsock; + BufferedReader in; + PrintWriter out; + String line; + int port = 7777; + + if (args.length == 0) + System.err.println("usage: java EchoServer [port]"); + else + port = Integer.parseInt(args[0]); + System.out.println("[EchoServer] starting"); + try { + srvsock = new ServerSocket(port); + while (true) { + cltsock = srvsock.accept(); + out = new PrintWriter( + cltsock.getOutputStream(), true); + in = new BufferedReader(new InputStreamReader( + cltsock.getInputStream())); + while ((line = in.readLine()) != null) { + if (line.equals("exit")) + break; + out.println(line); + } + } + } catch (IOException e) { + System.err.println("[EchoServer] exception on port " + port); + System.err.println(e.getMessage()); + } + } +} diff --git a/java_net/net_programming/HTTPServer.html b/java_net/net_programming/HTTPServer.html @@ -0,0 +1,31 @@ +<!DOCTYPE html> +<html> +<head> + <title>HTTPServer Test</title> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> +</head> +<body> + Example of GET method<br> + <form action="http://127.0.0.1:8010" method="GET"> + Username<input type="text" name="user"><br> + Password<input type="text" name="pass"><br> + <input type="submit" value="Login"><br> + </form> + + <p>Example of POST method</p> + <form action="http://127.0.0.1:8010" method="POST"> + Username<input type="text" name="user"><br> + Password<input type="text" name="pass"><br> + <input type="submit" value="Login"><br> + </form> + + <p>Example of multi-part request</p> + <form action="http://127.0.0.1:8010" method="POST" enctype="multipart/form-data"> + Type your name<input type="text" name="uname"><br> + File<input type="file" name="attach1"><br> + File<input type="file" name="attach2"><br> + <input type="submit" value="Send to server"><br> + </form> +</body> +</html> diff --git a/java_net/net_programming/HTTPServer.java b/java_net/net_programming/HTTPServer.java @@ -0,0 +1,48 @@ +import java.io.*; +import java.net.*; + +class SocketUtils { + static final int BUF_LEN = 1024; + + public static String getRequest(Socket sock) throws Exception { + StringBuilder reqstr = new StringBuilder(); + byte[] reqbytes = new byte[BUF_LEN]; + InputStream in = sock.getInputStream(); + int n; + + while (true) { + n = in.read(reqbytes, 0, BUF_LEN); + if (n == -1) + break; + reqstr.append(new String(reqbytes, 0, n)); + if (n < BUF_LEN) + break; + } + return reqstr.toString(); + } +} + +public class HTTPServer { + public static void main(String[] args) throws Exception { + ServerSocket srvsock = new ServerSocket(8010); + Socket sock; + String req; + PrintWriter pw; + + while (true) { + System.out.println("[HTTPServer] waiting for client"); + sock = srvsock.accept(); + req = SocketUtils.getRequest(sock); + System.out.println("[HTTPServer] obtained request:\n\n" + req); + pw = new PrintWriter(sock.getOutputStream()); + + pw.println("HTTP/1.0 200 OK"); + pw.println("Content-type: text/html"); + pw.println("Server: Java 6.1\n"); + + pw.println("<html><body>Hello world</body></html>"); + pw.close(); + sock.close(); + } + } +} diff --git a/java_net/net_programming/ShowIP.java b/java_net/net_programming/ShowIP.java @@ -0,0 +1,23 @@ +import java.net.*; + +public class ShowIP { + public static void main(String[] args) throws Exception { + InetAddress[] addr; + String hostname = ""; + + try { + hostname = args[0]; + addr = InetAddress.getAllByName(hostname); + + for (int i = 0; i < addr.length; i++) + System.out.println(i + "\t" + hostname + ": " + + addr[i].toString()); + } catch (ArrayIndexOutOfBoundsException e) { + System.err.println("usage: java ShowIP <hostname>"); + System.exit(1); + } catch (UnknownHostException e) { + System.err.println("unknown host: " + hostname); + System.exit(1); + } + } +} diff --git a/java_net/net_programming/UDPClient.java b/java_net/net_programming/UDPClient.java @@ -0,0 +1,41 @@ +import java.io.*; +import java.net.*; + +public class UDPClient { + public static void main(String[] args) throws Exception { + DatagramSocket cltsock; + DatagramPacket sendpack, recvpack; + InetAddress addr; + BufferedReader input; + String msg, srvresp; + String hostname = "127.0.0.1"; + byte[] senddata = new byte[1024]; + byte[] recvdata = new byte[1024]; + int port = 8888; + + if (args.length == 2) { + hostname = args[0]; + port = Integer.parseInt(args[1]); + } else + System.out.println("usage: java UDPClient [hostname] [port]"); + System.out.print("> "); + input = new BufferedReader(new InputStreamReader(System.in)); + cltsock = new DatagramSocket(); + cltsock.setSoTimeout(1000); + addr = InetAddress.getByName(hostname); + msg = input.readLine(); + senddata = msg.getBytes(); + sendpack = new DatagramPacket(senddata, senddata.length, addr, port); + cltsock.send(sendpack); + recvpack = new DatagramPacket(recvdata, recvdata.length); + + try { + cltsock.receive(recvpack); + srvresp = new String(recvpack.getData()); + System.out.println("server response: " + srvresp); + } catch (SocketTimeoutException e) { + System.err.println("timeout" + e); + } + cltsock.close(); + } +} diff --git a/java_net/net_programming/UDPServer.java b/java_net/net_programming/UDPServer.java @@ -0,0 +1,35 @@ +import java.net.*; + +public class UDPServer { + public static void main(String[] args) throws Exception { + DatagramSocket srvsock; + DatagramPacket sendpack, recvpack; + InetAddress addr; + String msg; + byte[] senddata = new byte[1024]; + byte[] recvdata = new byte[1024]; + int port = 8888, recvport; + + if (args.length == 0) + System.out.println("usage: java UDPServer [port]"); + else + port = Integer.parseInt(args[0]); + System.out.println("[UDPServer] starting"); + srvsock = new DatagramSocket(port); + + while (true) { + recvpack = new DatagramPacket(recvdata, recvdata.length); + srvsock.receive(recvpack); + msg = new String(recvpack.getData(), 0, + recvpack.getLength()); + + System.out.println("your message: " + msg); + addr = recvpack.getAddress(); + recvport = recvpack.getPort(); + senddata = msg.getBytes(); + sendpack = new DatagramPacket(senddata, + senddata.length, addr, recvport); + srvsock.send(sendpack); + } + } +}