Java Network Programming

Slides:



Advertisements
Similar presentations
SOCKET PROGRAMMING WITH MOBILE SOCKET CLIENT DEARTMENT OF COMPUTER SCIENCE IOWA STATE UNIVERSITY.
Advertisements

CSCI Java Networking1 Java Net Classes ClassDescription DatagramPacketThis class represents a datagram packet. DatagramSocketThis class represents.
Lectured by: Nguyễn Lê Duy Lai 2/14/2011HCM City University of Technology1.
Sockets for Clients Socket Basics  Connect to a remote machine  Send data  Receive data  Close a connection  Bind to a port 
Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server.
Programming TCP Clients Version InetAddress Class An IP address identifies uniquely a host in the internet, which consists of 4 numbers (1 byte.
Prepared By E. Musa Alyaman1 Java Network Programming TCP.
Java Sockets Source:
1 L53 Networking (2). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
Programming TCP Clients. InetAddress Class An IP address identifies uniquely a host in the internet, which consists of 4 numbers (1 byte each one) in.
Internet Programming In Java. References Java.sun.com Java552 Many of the programs shown.
Lecture 11 Java Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger and Joonbok Lee.
Client/Server In Java An Introduction to TCP/IP and Sockets.
Programming TCP Clients. InetAddress Class An IP address identifies uniquely a host in the internet, which consists of 4 numbers (1 byte each one) in.
Networking Support In Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
13-Jul-15 Sockets and URLs. 2 Sockets A socket is a low-level software device for connecting two computers together Sockets can also be used to connect.
Networking java.net package, which provides support for networking. Its creators have called Java “programming for the Internet.” Socket :- A network socket.
Networking in Java Representation and Management of Data on the Internet.
Java Networking.
An program As a simple example of socket programming we can implement a program that sends to a remote site As a simple example of socket.
Socket programming 1. getByName import java.net.*; public class GetHostName { public static void main (String args[]) { String host = "
Multicast Sockets What is a multicast socket?
Import java.net.*; import java.io.*; public class LowPortScanner { public static void main(String[] args) { String host = "localhost"; if (args.length.
DBI Representation and Management of Data on the Internet.
1 CSCD 330 Network Programming Winter 2015 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 6 Application.
UDP vs TCP UDP Low-level, connectionless No reliability guarantee TCP Connection-oriented Not as efficient as UDP.
Socket Programming in Java CS587x Lecture 4 Department of Computer Science Iowa State University.
1 What is the Internet? Hosts or end-systems PCs, workstations, servers PDAs, phones, toasters Communication links fiber, copper, radio, satellite Routers.
1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
Lecture 9 Network programming. Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet.
Java Sockets Programming
Java Socket programming. Socket programming with TCP.
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
What is a Network? Computer network Computer network a set of computers using common protocols to communicate over connecting transmission media. a set.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.
Sockets For Clients Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University.
Java Networking. java.net package provides support of networking. Its creators have called Java "programming for the Internet." What makes Java a good.
Networking Java (2/3)
By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Inetaddress Class When establishing a connection across the Internet, addresses.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
Java Programming II Java Network (I) Java Programming II.
1 Lecture 9: Network programming. 2 Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Network Programming with Java java.net.InetAddress: public static void main(String[] args) throws UnknownHostException { InetAddress localAddress = InetAddress.getLocalHost();
1 CSCD 330 Network Programming Winter 2016 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 6 Application.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
Network Programming Communication between processes Many approaches:
Java 13. Networking public class SumTest {
Java.net CS-328 Dick Steflik.
Secure Sockets SSL (Secure Sockets Layer) is a standard security technology for establishing an encrypted link between a server and a client—typically.
Source: Java Sockets Source:
Chapter 03 Networking & Security
Network Programming Introduction
NETWORK PROGRAMMING CNET 441
An Introduction to TCP/IP and Sockets
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Network Programming Introduction
Sockets and URLs 17-Sep-18.
Sockets and URLs 13-Nov-18.
UNIT-6.
Networking.
Sockets and URLs 3-Dec-18.
Java Socket Programming
Programming TCP Sockets
Programming TCP Clients
Presentation transcript:

Java Network Programming Nguyễn Quang Hùng (cập nhật)

LẬP TRÌNH MẠNG TRÊN JAVA Gói java.net InetAddress ServerSocket Socket URL URLConnection DatagramSocket HCMC University of Technology – Faculty of Information Technology

LẬP TRÌNH MẠNG TRÊN JAVA InetAddress class Class mô tả về địa chỉ IP (Internet Protocol) Các phương thức getLocalHost, getByName, hay getAllByName để tạo một InetAddress instance: public static InetAddess InetAddress.getByName(String hostname) public static InetAddess [] InetAddress.getAllByName(String hostname) public static InetAddess InetAddress.getLocalHost() Để lấy địa chỉ IP hay tên dùng các phương thức: getHostAddress() getHostName() HCMC University of Technology – Faculty of Information Technology

Ví dụ 1: Lấy địa chỉ của local/remote host public class Sample1 { public static void main (String[] args) { try { InetAddress localAddr = InetAddress.getLocalHost(); System.out.println( "Local Host Address (Host/IP): " + localAddr.toString() ); InetAddress remoteAddr = InetAddress.getByName("www.vnn.vn"); System.out.println( "Web Server IP: " + remoteAddr.toString() ); } catch (UnknownHostException ex) {ex.printStackTrace(); } }// end main }// End class HCMC University of Technology – Faculty of Information Technology

LẬP TRÌNH MẠNG TRÊN JAVA In địa chỉ IP của proxy.hcmut.edu.vn import java.net.*; class kku{ public static void main (String args[]) { try { InetAddress[] addresses = InetAddress.getAllByName(“proxy.hcmut.edu.vn"); for (int i = 0; i < addresses.length; i++) { System.out.println(addresses[i]); } catch (UnknownHostException e) { System.out.println("Could not find proxy.hcmut.edu.vn"); HCMC University of Technology – Faculty of Information Technology

LẬP TRÌNH MẠNG TRÊN JAVA Các chương trình đọc thêm Lấy tên máy từ một địa chỉ IP. Cho một địa chỉ tìm tên máy. HCMC University of Technology – Faculty of Information Technology

LẬP TRÌNH MẠNG TRÊN JAVA HCMC University of Technology – Faculty of Information Technology

LẬP TRÌNH MẠNG TRÊN JAVA Socket class Class mô tả về socket Tạo một socket Socket(InetAddress address, int port) Socket(String host, int port) Socket(InetAddress address, int port, InetAddress, localAddr, int localPort) Socket(String host, int port, InetAddress, localAddr, int localPort) Socket() HCMC University of Technology – Faculty of Information Technology

LẬP TRÌNH MẠNG TRÊN JAVA Socket class (tiếp theo) Lấy thông tin về một socket InetAddress getInetAddress() : trả về địa chỉ mà socket kết nối đến. int getPort() : trả về địa chỉ mà socket kết nối đến. InetAddress getLocalAddress() : trả về địa chỉ cục bộ. int getLocalPort() : trả về địa chỉ cục bộ. Sử dụng Streams public OutputStream getOutputStream() throws IOException Trả về một output stream cho việc viết các byte đến socket này. public InputStream getInputStream() throws IOException Trả về một input stream cho việc đọc các byte từ socket này. HCMC University of Technology – Faculty of Information Technology

LẬP TRÌNH MẠNG TRÊN JAVA Kết nối đên 1 số webserver import java.net.*; import java.io.*; public class getSocketInfo { public static void main(String[] args) { for (int i = 0; i < args.length; i++) { try { Socket theSocket = new Socket(args[i], 80); System.out.println("Connected to " + theSocket.getInetAddress() + " on port " + theSocket.getPort() + " from port " + theSocket.getLocalPort() + " of " + theSocket.getLocalAddress()); HCMC University of Technology – Faculty of Information Technology

Tài liệu tham khảo java.sun.com/tutorial HCMC University of Technology – Faculty of Information Technology