URL Kullanımı Get URL

Similar documents
public static void main(string[] args) throws IOException { sock = new Socket(args[0], Integer.parseInt(args[1]));

COMP 213. Advanced Object-oriented Programming. Lecture 20. Network Programming

CS2307 NETWORKS LAB 1. Programs using TCP Sockets (like date and time server & client, echo server & client, etc.) 2. Programs using UDP Sockets

Network. Dr. Jens Bennedsen, Aarhus University, School of Engineering Aarhus, Denmark

Networking Basics. network communication.

Previous lecture: threads G51PRG: Introduction to Programming Second semester Lecture 12 URL

TCP connections. Fundamentals of Internet Connections Objectives. Connect to an Echo port. java.net.socket

Network Programming. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Distributed Systems COMP 212. Lecture 8 Othon Michail

Sockets and RMI. CS151 Chris Pollett Dec. 5, 2005.

CPSC 441 Tutorial TCP Server. Department of Computer Science University of Calgary

Reading from URL. Intent - open URL get an input stream on the connection, and read from the input stream.

CS September 2017

JAVA - NETWORKING (SOCKET PROGRAMMING)

Object-Oriented Programming in Java

Lecture 3: Socket Programming

Steps for Implementing a Server

Networking Code CSCI 201 Principles of Software Development

Network Programming: Servers

Input from Files. Buffered Reader

IT101. File Input and Output

Java.net Package and Classes(Url, UrlConnection, HttpUrlConnection)

Java for Interfaces and Networks

JAVA 프로그래밍 II. Lab #7 (Networking) TCP Java Socket. TCP vs UDP 2017 학년도 1 학기 년봄학기 5/24/2017 박경신. Client. Server

Lab 1 : Java Sockets

TCP and UDP Socket Programming in JAVA TCP Socket Programming 1. Write code for Client and save in GreetingClient.java

Java Technologies. Lecture VII. Valdas Rapševičius. Vilnius University Faculty of Mathematics and Informatics

CS193j, Stanford Handout #26. Files and Streams

A socket is a software endpoint that establishes bidirectional communication between a server program and one or more client programs.

CS 2113 Software Engineering

Java for Interfaces and Networks (DT3029)

Java Networking (sockets)

Java Development: Do s and Don ts

Multi-threaded Web Server (Assignment 1) Georgios Georgiadis

CSPP : Introduction to Object-Oriented Programming

Introduction to Sockets

Internet Technology 2/7/2013

09-1. CSE 143 Java GREAT IDEAS IN COMPUTER SCIENCE. Overview. Data Representation. Representation of Primitive Java Types. Input and Output.

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O

Requirements. PA4: Multi-thread File Downloader Page 1. Assignment

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20

I/O Streams. Object-oriented programming


Connecting to a Server Implementing Servers Making URL Connections Advanced Socket Programming

CSCD 330 Network Programming Spring 2018

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

Networking with java (socket programming) a brief study

Principles, Models, and Applications for Distributed Systems M

Network Programming Benoît Garbinato

Distributed Programming

Byte and Character Streams. Reading and Writing Console input and output

Techniques of Java Programming: Streams in Java

CS 351 Design of Large Programs Sockets Example

I gave this assignment in my Internet and Intranet Protocols and Applications course:

CSCD 330 Network Programming Winter 2019

Internet and Intranet Applications and Protocols Examples of Bad SMTP Code Prof. Arthur P. Goldberg Spring, 2004

Getting started with Winstone. Minimal servlet container

1.00 Lecture 30. Sending information to a Java program

Special error return Constructors do not have a return value What if method uses the full range of the return type?

} } class ClientHandler extends Thread { private Socket client; private Scanner input; private PrintWriter output;

Distributed Programming - Sockets

"protocol stack! chat. Java

Getting Started in Java. Bill Pugh Dept. of Computer Science Univ. of Maryland, College Park

SERVER/CLIENT NETWORKING AT JAVA PLATFORM

Principles, Models, and Applications for Distributed Systems M

Unit 1 Java Networking

Principles, Models and Applications for Distributed Systems M

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Principles of Software Construction. Introduction to networks and distributed systems School of Computer Science

Lecture 11.1 I/O Streams

CSCD 330 Network Programming

protocols September 15,

Program 20: //Design an Applet program to handle Mouse Events. import java.awt.*; import java.applet.*; import java.awt.event.*;

Java - Applications. The following code sets up an application with a drop down menu, as yet the menu does not do anything.

COMP-202: Foundations of Programming. Lecture 12: Linked List, and File I/O Sandeep Manjanna, Summer 2015

Darshan Institute of Engineering & Technology for Diploma Studies

Network Programming java.net.inetaddress

COMP-202: Foundations of Programming. Lecture 22: File I/O Jackie Cheung, Winter 2015

Writing a Protocol Handler

Topic 10: Network Programming

Object-Oriented Programming Design. Topic : Streams and Files

Web Server Project. Tom Kelliher, CS points, due May 4, 2011

Outlines. Networking in Java. Internet hardware structure. Networking Diagram. IP Address. Networking in Java. Networking basics

Advanced Java Programming. Networking

Chapter 2 Applications and

Programmierpraktikum

CONCURRENCY IN JAVA Course Parallel Computing

CSCD 330 Network Programming

CPSC 441 Tutorial - 11 UDP Socket Programming Department of Computer Science University of Calgary

CSCD 330 Network Programming Spring 2018

CS 355. Computer Networking. Wei Lu, Ph.D., P.Eng.

Today. cisc3120-fall2012-parsons-lectiv.2 2

Java Support for developing TCP Network Based Programs

PART1: Choose the correct answer and write it on the answer sheet:

3. Remote Procedure Call

COMP 213. Advanced Object-oriented Programming. Lecture 19. Input/Output

CSCD 330 Network Programming

Java for Interfaces and Networks (DT3010, HT11)

PROGRAMMAZIONE AVANZATA JAVA E C

Assignment, part1: feedback and common mistakes. INFO-0010 Samuel HIARD

Transcription:

Networking 1

URL Kullanımı Get URL URL info 2

import java.io.*; import java.net.*; public class GetURL { public static void main(string[] args) { InputStream in = null; OutputStream out = null; // Check the arguments if ((args.length!= 1) && (args.length!= 2)) throw new IllegalArgumentException("Wrong number of arguments"); URL url = new URL(args[0]); // Create the URL in = url.openstream(); // Open a stream to it if (args.length == 2) // Get an appropriate output stream out = new FileOutputStream(args[1]); else out = System.out; byte[] buffer = new byte[4096]; int bytes_read; while((bytes_read = in.read(buffer))!= 1) out.write(buffer, 0, bytes_read); catch (Exception e) { System.err.println(e); System.err.println("Usage: java GetURL <URL> [<filename>]"); finally { // Always close the streams, no matter what. in.close(); out.close(); catch (Exception e) { 3

import java.net.*; import java.io.*; import java.util.date; public class GetURLInfo { public static void printinfo(url url) throws IOException { URLConnection c = url.openconnection(); // Get URLConnection from the URL c.connect(); // Open a connection to the URL System.out.println(" Content Type: " + c.getcontenttype()); System.out.println(" Content Encoding: " + c.getcontentencoding()); System.out.println(" Content Length: " + c.getcontentlength()); g System.out.println(" Date: " + new Date(c.getDate())); System.out.println(" Last Modified: " + new Date(c.getLastModified())); System.out.println(" Expiration: " + new Date(c.getExpiration())); if (c instanceof HttpURLConnection) { HttpURLConnection h = (HttpURLConnection) c; System.out.println(" Request Method: " + h.getrequestmethod()); System.out.println(" Response Message: " + h.getresponsemessage()); g System.out.println(" Response Code: " + h.getresponsecode()); information about it. */ public static void main(string[] args) { printinfo(new URL(args[0])); catch (Exception e) { System.err.println(e); System.err.println("Usage: java GetURLInfo <url>"); 4

E posta Gönderme 5

import java.io.*; import java.net.*; public class SendMail { public static void main(string[] args) { if (args.length >= 1) System.getProperties().put("mail.host", args[0]); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("From: "); String from = in.readline(); System.out.print("To: "); String to = in.readline(); System.out.print("Subject: "); String subject = in.readline(); URL u = new URL("mailto:" + to); // Create a mailto: URL URLConnection c = u.openconnection(); // Create a URLConnection for it c.setdoinput(false); // Specify no input from this URL c.setdooutput(true); // Specify we'll do output System.out.println("Connecting..."); // Tell the user what's happening System.out.flush(); // Tell them right now c.connect(); // Connect to mail host PrintWriter out = // Get output stream to mail host new PrintWriter(new OutputStreamWriter(c.getOutputStream())); 6

out.println("from: \"" + from + "\" <" + System.getProperty("user.name") + "@" + InetAddress.getLocalHost().getHostName() + ">"); out.println("to: " + to); out.println("subject: " + subject); out.println(); // blank line to end the list of headers System.out.println("Enter the message. " + "End with a '.' on a line by itself."); String line; for(;;) { line = in.readline(); if ((line == null) line.equals(".")) break; out.println(line); out.close(); System.out.println("Message sent."); System.out.flush(); catch (Exception e) { // Handle any exceptions, print error message. System.err.println(e); System.err.println("Usage: java SendMail [<mailhost>]"); 7

Web Server a bağlanma 8

import java.io.*; import java.net.*; public class HttpClient { public static void main(string[] args) { if ((args.length!= 1) && (args.length!= 2)) throw new IllegalArgumentException("Wrong number of arguments"); OutputStream to_file; if (args.length == 2) to_file = new FileOutputStream(args[1]); else to_file = System.out; URL url = new URL(args[0]); String protocol = url.getprotocol(); if (!protocol.equals("http")) throw new IllegalArgumentException("URL must use 'http:' protocol"); String host = url.gethost(); t() int port = url.getport(); if (port == 1) port = 80; // if no port, use the default HTTP port String filename = url.getfile(); Socket socket = new Socket(host, port); InputStream from_server = socket.getinputstream(); Pi PrintWriter twit to_server = new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); 9

to_server.println("get " + filename); to_server.flush(); // Send it right now! byte[] buffer = new byte[4096]; int bytes_read; while((bytes_read = from_server.read(buffer))!= 1)) to_file.write(buffer, 0, bytes_read); socket.close(); to_file.close(); catch (Exception e) { // Report any errors that arise System.err.println(e); System.err.println("Usage: java HttpClient <URL> [<filename>]"); 10

import java.io.*; import java.net.*; public class HttpMirror { public static void main(string args[]) { int port = Integer.parseInt(args[0]); ServerSocket ss = new ServerSocket(port); for(;;) { Socket client = ss.accept(); BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); PrintWriter out = new PrintWriter(new OutputStreamWriter(client.getOutputStream())); out.println("http/1.0 200 "); // Version & status code out.println("content Type: text/plain"); // The type of data we send out.println(); // End of response headers out.flush(); String line; while((line = in.readline())!= null) { if (line.length() == 0) break; out.println(line); out.close(); in.close(); client.close(); close(); // Loop again, waiting for the next connection catch (Exception e) { System.err.println(e); err System.err.println("Usage: java HttpMirror <port>"); 11

SimpleProxyServer.java import java.io.*; import java.net.*; public class SimpleProxyServer { public static void main(string[] args) throws IOException { if (args.length!= 3) throw new IllegalArgumentException("Wrong number of arguments."); String host = args[0]; int remoteport = Integer.parseInt(args[1]); int localport = Integer.parseInt(args[2]); System.out.println( println("starting proxy for " + host + ":" + remoteport + " on port " + localport); runserver(host, remoteport, localport); // never returns catch (Exception e) { System.err.println(e); System.err.println("Usage: java SimpleProxyServer " + "<host> <remoteport> <localport>"); 12

public static void runserver(string host, int remoteport, int localport) throws IOException { ServerSocket ss = new ServerSocket(localport); final byte[] request = new byte[1024]; byte[] reply = new byte[4096]; while(true) { Socket client = null, server = null; client = ss.accept(); final InputStream from_client = client.getinputstream(); final OutputStream to_client= client.getoutputstream(); server = new Socket(host, remoteport); catch (IOException e) { PrintWriter out = new PrintWriter(new OutputStreamWriter(to_client)); out.println( println("proxy server cannot connect to " + host + ":" + remoteport + ":\n" + e); out.flush(); client.close(); continue; 13

final InputStream from_server = server.getinputstream(); final OutputStream to_server = server.getoutputstream(); Thread t = new Thread() { public void run() { int bytes_read; while((bytes_read = from_client.read(request))!= 1) { to_server.write(request, 0, bytes_read); to_server.flush(); catch (IOException e) { to_server.close(); catch (IOException e) { ; t.start(); intbytes _ read; while((bytes_read = from_server.read(reply))!= 1) { to_client.write(reply, 0, bytes_read); to_client.flush(); catch(ioexception e) { to_client.close(); catch (IOException e) { System.err.println(e); finally { if (server!= null) server.close(); if (client!= null) client.close(); catch(ioexception e) { 14

import java.applet.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; public class Who extends Applet implements ActionListener, Runnable { Button who; // The button in the applet public void init() { who = new Button("Who?"); who.setfont(new Font("SansSerif", Font.PLAIN, 14)); who.addactionlistener(this); who.setactioncommand("who"); this.add(who); public void actionperformed(actionevent e) { if (e.getactioncommand().equals("who")) new Thread(this).start(); public void run() { who.setenabled(false); Frame f = new CloseableFrame("Who's Logged On: Connecting..."); TextArea t = new TextArea(10, 80); t.setfont(new Font("MonoSpaced", Font.PLAIN, 10)); f.add(t, "Center"); f.pack(); f.show(); 15

Socket s = null; PrintWriter out = null; BufferedReader in = null; String hostname = this.getcodebase().gethost(); s = new Socket(hostname, 79); out = new PrintWriter(new OutputStreamWriter(s.getOutputStream())); in = new BufferedReader(new InputStreamReader(s.getInputStream())); out.println(); out.flush(); // Send it now! String line; while((line = in.readline())!= null) { t.append(line); t.append("\n"); f.settitle("who's Logged On: " + hostname); catch (IOException e) { t.append(e.tostring()); f.settitle("who's Logged On: Error"); finally { in.close(); out.close(); s.close(); catch(exception e){ who.setenabled(true); 16