Network Programming with Java java.net.InetAddress: public static void main(String[] args) throws UnknownHostException { InetAddress localAddress = InetAddress.getLocalHost();

Slides:



Advertisements
Similar presentations
Jan Java I/O Yangjun Chen Dept. Business Computing University of Winnipeg.
Advertisements

Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings.
Java I/O The Cheat Sheet. Reading and Writing The basic architecture of Java IO is pluggable. The idea is to have some very simple classes do very simple.
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.
1 Java Networking – Part I CS , Spring 2008/9.
CIS – Spring Instructors: Geoffrey Fox, Bryan Carpenter Computational Science and.
Client/Server In Java An Introduction to TCP/IP and Sockets.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L22 (Chapter 25) Networking.
© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology 1 Let’s get started. Let’s start by selecting an architecture from among.
Socket Communication Sockets provide an abstraction of two-point communication The two sides only read/write without concern for how data is translated.
Lab 2 Data Streams. Lab 2: Data Streams Introduction Reading from an Input Stream Writing to an Output Stream Filter Streams.
Networking with Java CSc 335 Object-Oriented Programming and Design Spring 2009.
Networking java.net package, which provides support for networking. Its creators have called Java “programming for the Internet.” Socket :- A network socket.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
JAVA Socket Programming Source: by Joonbok Lee, KAIST, 2003.
Greg Jernegan Brandon Simmons. The Beginning…  The problem Huge demand for internet enabled applications and programs that communicate over a network.
Socket Programming -What is it ? -Why bother ?. Basic Interface for programming networks at transport level It is communication end point Used for inter.
Socket programming 1. getByName import java.net.*; public class GetHostName { public static void main (String args[]) { String host = "
SOCKET PROGRAMMING. Client/Server Communication At a basic level, network-based systems consist of a server, client, and a media for communication as.
Misc. Java Alittle more network programming & Using jar files to share your work.
NET0183 Networks and Communications Lecture 31 The Socket API 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from.
Dynamic Arrays Dynamic arrays are arrays that are re- allocated to a larger size. –See Eck Section 8.3 for details. –For instance, what happens with the.
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut.
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.
Li Tak Sing COMPS311F. Case study: consumers and producers A fixed size buffer which can hold at most certain integers. A number of producers which generate.
© Lethbridge/Laganière 2005 Chap. 3: Basing Development on Reusable Technology The Client-Server Architecture A distributed system is a system in.
Java Socket programming. Socket programming with TCP.
1 Network Programming and Java Sockets. 2 Network Request Result a client, a server, and network Client Server Client machine Server machine Elements.
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
Sockets Sockets A socket is an object that encapsulates a TCP/IP connection There is a socket on both ends of a connection, the client side and the server.
Socket-Programming.  Establish contact (connection).  Exchange information (bi-directional).  Terminate contact.
Applications Development Input and Output in Java Topics covered: Some input-output capabilities of Java Example input code Java.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket Programming.
Networking Terminology: ISP (Internet service provider) – dialup, dsl, cable LAN (local area network) IP (internet protocol) address, eg
Java IO. Why IO ? Without I/O, your program is a closed box. Without I/O, your program is a closed box. I/O gives your Java program access to your hard.
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.
1 Netprog 2002 TCP/IP UDP/IP in Java Based on Java Network Programming and Distributed Computing.
Prepared by Dr. Jiying Zhao University of Ottawa Canada.
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.
Socket Programming in Java -First Step of Network Programming-
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
Generic Connection Framework Connection FileConnectionSocketConnectionHTTPConnection InputConnection OutputConnection StreamConnection.
Network Programming. These days almost all devices.
CS202 Java Object Oriented Programming Input and Output Chengyu Sun California State University, Los Angeles.
Network Programming Communication between processes Many approaches:
The Java IO System Different kinds of IO Different kinds of operations
Java 13. Networking public class SumTest {
Network Programming in Java CS 1111 Ryan Layer May 3, 2010
Lecture 21 Sockets 1 (Not in D&D) Date.
Network Programming Introduction
Beyond HTTP Up to this point we have been dealing with software tools that run on browsers and communicate to a server that generates files that can be.
An Introduction to TCP/IP and Sockets
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Java Network Programming
UNIT-6.
Networking.
CS18000: Problem Solving and Object-Oriented Programming
Programming TCP Sockets
CS18000: Problem Solving and Object-Oriented Programming
Based on Java Network Programming and Distributed Computing
Presentation transcript:

Network Programming with Java

java.net.InetAddress: public static void main(String[] args) throws UnknownHostException { InetAddress localAddress = InetAddress.getLocalHost(); System.out.println(localAddress.getHostAddress()); } Know your own IP Address Know IP address of a Host public static void main(String[] args) throws UnknownHostException { InetAddress googleAddress = InetAddress.getByName(" System.out.println(googleAddress.getHostAddress()); }

Java TCP/IP Server & Clients TCP/IP Server: That(a program or a process) listens to a TCP port. Client: What (a program or a process) initiates the connection Server is represented by java.net.ServerSocket Client is represented by java.net.Socket int portNumber = 7896; // TCP port Number ServerSocket server = new ServerSocket(portNumber);// listen on port Socket socket = server.accept(); // Blocks until connection made String destIP = " "; // Target Host IPv4 Address int portNumber = 7896; // TCP port Number Socket socket = new Socket(destIP, portNumber);// connect

Stay & while connected… ServerClient Wait for connection serverSocket.accept(); Socket socket = new Socket(destIP,portNumber); Server’s Output/ Client’s Input Client’s Output/ Server’s Input

Java I/O Some important OutputStream child: DataOutputStream FileOutputStream ByteArrayOutputStream

Java I/O Some important InputStream child: DataInputStream FileInputStream ByteArrayInputStream Read Data From Console (command) DataInputStream dis=new DataInputStream(System.in); String x=dis.readLine(); Read an input integer Example DataInputStream dis=new DataInputStream(System.in); String x=dis.readLine(); int z=Integer.parseInt(x);

File Handling To write some text to a File, follow the steps below… 1. File dir = new File("H:\\soham"); 2. Check if the directory exist and if H:\soham is a Directory and not a file, 3. create a file under the parent directory 4. Extract the raw bytes from the String To write some text to a File, follow the steps below… 1. File dir = new File("H:\\soham"); 2. Check if the directory exist and if H:\soham is a Directory and not a file, 3. create a file under the parent directory 4. Extract the raw bytes from the String if(false==dir.exists() || false==dir.isDirectory()){ dir.mkdirs(); } File outFile = new File(dir, "we.txt"); String outString = "Welcome to File handling"; byte[] data = outString.getBytes();

File Handling 5.Open an OutputStream to the file: 6.Write the raw bytes to the OutputStream, flush and close it FileOutputStream fos = new FileOutputStream(outFile); fos.write(data); fos.flush(); fos.close();

Write to a File : Second approach FileOutputStream fos = new FileOutputStream(outFile); DataOutputStream dos = new DataOutputStream(fos); dos.writeInt(90); dos.flush(); dos.writeUTF(outString); dos.flush(); dos.close(); DataOutputStream provides a wrapper over an OutputStream to write primitive data types, not just byte[]

Read from a File Read byte-by-byte Read Bytes chunk-wise : Faster approach