Gel vatandas gell.. TikLaa'da gel..
Java
Java ile Emailsender (Socket)
Ara 9th
Selam arkadaşlar, java ile basit bir emailsender yapmıştım paylaşmak istedim. Email hesabınızdan giriş yaptığınızda bu program üzerinden maillerinizi gönderebilirsiniz. Program statik çalışıyor, yani ne cmd’den nede herhangi bir arayüz aracılığı ile değişkenlere değer atanmıyor. Fakat bunu isterseniz çok basit bir şekilde yapabilirsiniz. Umarım işinize yarar.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
import java.io.*; import java.net.*; public class EmailSender { public static void main(String[] args) throws Exception { Socket socket = new Socket("smtp.web.de",25); InputStream is = socket.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String response = br.readLine(); System.out.println(response); if (!response.startsWith("220")) { throw new Exception("220 reply not received from server."); } OutputStream os = socket.getOutputStream(); String command = "HELO localhost\r\n"; System.out.print(command); os.write(command.getBytes("US-ASCII")); response = br.readLine(); System.out.println(response); if (!response.startsWith("250")) { throw new Exception("250 reply not received from server."); } command = "MAIL FROM: qonyali@web.de\r\n"; System.out.print(command); os.write(command.getBytes("US-ASCII")); response = br.readLine(); System.out.println(response); if (!response.startsWith("250")) { throw new Exception("250 reply not received from server."); } command = "RCPT TO: halil@tiklaa.com\r\n"; System.out.print(command); os.write(command.getBytes("US-ASCII")); response = br.readLine(); System.out.println(response); if (!response.startsWith("250")) { throw new Exception("250 reply not received from server."); } command = "DATA\r\n"; System.out.print(command); os.write(command.getBytes("US-ASCII")); response = br.readLine(); System.out.println(response); if (!response.startsWith("354")) { throw new Exception("354 reply not received from server."); } command = "SUBJECT: Email Basligi\r\n"; System.out.print(command); os.write(command.getBytes("US-ASCII")); command = "E-Mail'in icerigi\r\n"; System.out.print(command); os.write(command.getBytes("US-ASCII")); command = ".\r\n"; System.out.print(command); os.write(command.getBytes("US-ASCII")); command = "QUIT\r\n"; System.out.print(command); os.write(command.getBytes("US-ASCII")); response = br.readLine(); System.out.println(response); if (!response.startsWith("250")) { throw new Exception("250 reply not received from server."); } } } |
Java ile Webserver Yapımı (Socket)
Ara 9th
Selam arkadaşlar deneme amaçlı bi Webserver yapmıştım. Sizlerle paylaşmak istedim. Serveri java’da yazdim. WebServer ve HttpRequest class’ları olmak üzere iki class’dan oluşuyor.
Webserver.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.io.*; import java.net.*; import java.util.*; public final class WebServer { public static void main(String argv[]) throws Exception { int port = 3542; ServerSocket listenSocket = new ServerSocket(port); while (true) { Socket connectionSocket = listenSocket.accept(); HttpRequest request = new HttpRequest(connectionSocket); Thread thread = new Thread(request); thread.start(); } } } |
HttpRequest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
import java.io.* ; import java.net.* ; import java.util.* ; final class HttpRequest implements Runnable { final static String CRLF = "\r\n"; Socket socket; // Constructor public HttpRequest(Socket socket) throws Exception { this.socket = socket; } // Implement the run() method of the Runnable interface. public void run() { try { processRequest(); } catch (Exception e) { System.out.println(e); } } private void processRequest() throws Exception { InputStream is = socket.getInputStream(); DataOutputStream os = new DataOutputStream(socket.getOutputStream()); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String requestLine = br.readLine(); System.out.println(); System.out.println(requestLine); String headerLine = null; while ((headerLine = br.readLine()).length() != 0) { System.out.println(headerLine); } StringTokenizer tokens = new StringTokenizer(requestLine); tokens.nextToken(); String fileName = tokens.nextToken(); fileName = "." + fileName; FileInputStream fis = null; boolean fileExists = true; try { fis = new FileInputStream(fileName); } catch (FileNotFoundException e) { fileExists = false; } String statusLine = null; String contentTypeLine = null; String entityBody = null; if (fileExists) { statusLine = "HTTP/1.0 200 OK" + CRLF; contentTypeLine = "Content-type: " + contentType( fileName ) + CRLF; } else { statusLine = "HTTP/1.0 404 Not Found" + CRLF; contentTypeLine = "Content-type: " + "text/html" + CRLF; entityBody = "<HTML>" + "<HEAD><TITLE>Not Found</TITLE></HEAD>" + "<BODY>Not Found</BODY></HTML>"; } os.writeBytes(statusLine); os.writeBytes(contentTypeLine); os.writeBytes(CRLF); if (fileExists) { sendBytes(fis, os); fis.close(); } else { os.writeBytes(entityBody); } System.out.println(); System.out.println(requestLine); os.close(); br.close(); socket.close(); } private static void sendBytes(FileInputStream fis, OutputStream os) throws Exception { byte[] buffer = new byte[1024]; int bytes = 0; while ((bytes = fis.read(buffer)) != -1) { os.write(buffer, 0, bytes); } } private static String contentType(String fileName) { if (fileName.endsWith(".htm") || fileName.endsWith(".html")) { return "text/html"; } if (fileName.endsWith(".jpg") || fileName.endsWith(".jpeg")) { return "image/jpeg"; } if (fileName.endsWith(".gif")) { return "image/gif"; } if (fileName.endsWith(".txt")) { return "text/plain"; } if (fileName.endsWith(".pdf")) { return "application/pdf"; } return "application/octet-stream"; } } |
Bu iki class’ımızı oluşturduktan sonra Webserver.java dosyasında belirlediğimiz port üzerinden sorgumuzu yapabiliriz.
Örneğin : http://127.0.0.1:3542/dosya.txt
HttpRequest.java class’ını biraz daha geliştirebilirz. contentType içinde daha fazla uzantı ekleyebiliriz vs.