Network Programming Communication between processes Many approaches:

Slides:



Advertisements
Similar presentations
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.
Advertisements

2: Application Layer 1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm.
1 Java Networking – Part I CS , Spring 2008/9.
1 L53 Networking (2). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
1 Overview r Socket programming with TCP r Socket programming with UDP r Building a Web server.
Prepared By E.Musa Alyaman1 Chapter 3 Internet Addressing.
Projekt współfinansowany przez Unię Europejską w ramach Europejskiego Funduszu Społecznego „Networking”
2: Application Layer1 Socket Programming. 2: Application Layer2 Socket-programming using TCP Socket: a door between application process and end- end-transport.
1 Network Layers Application Transport Network Data-Link Physical bits.
Network/Socket Programming in Java Rajkumar Buyya.
Networking java.net package, which provides support for networking. Its creators have called Java “programming for the Internet.” Socket :- A network socket.
2: Application Layer 1 Socket Programming TCP and UDP.
1 Inter-Process Communication: Network Programming using TCP Java Sockets Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept.
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.
CS4273: Distributed System Technologies and Programming I Lecture 5: Java Socket Programming.
SOCKET PROGRAMMING. Client/Server Communication At a basic level, network-based systems consist of a server, client, and a media for communication as.
2: Application Layer1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
DBI Representation and Management of Data on the Internet.
Winter 2002Suprakash Datta1 Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
-1- Georgia State UniversitySensorweb Research Laboratory CSC4220/6220 Computer Networks Dr. WenZhan Song Associate Professor, Computer Science.
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.
Socket Programming Lee, Sooyong
Network Programming and Sockets CPSC 363 Computer Networks Ellen Walker Hiram College (Includes figures from Computer Networking by Kurose & Ross, © Addison.
RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut.
Socket Programming Tutorial. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
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.
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.
What is a Network? Computer network Computer network a set of computers using common protocols to communicate over connecting transmission media. a set.
Socket-Programming.  Establish contact (connection).  Exchange information (bi-directional).  Terminate contact.
Introduction to Sockets “A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/11) Java Sockets and Simple Networking Joel Adams and Jeremy Frens.
2: Application Layer1 Socket programming Socket API Explicitly created, used, released by apps Client/server paradigm Two types of transport service via.
By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress.
1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm r two types of.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Inetaddress Class When establishing a connection across the Internet, addresses.
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.
1 CSCD 330 Network Programming Fall 2013 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 8a Application.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
Data Communications and Computer Networks Chapter 2 CS 3830 Lecture 11 Omar Meqdadi Department of Computer Science and Software Engineering University.
Network Programming. These days almost all devices.
Java 13. Networking public class SumTest {
Java.net CS-328 Dick Steflik.
Socket Programming Ameera Almasoud
Secure Sockets SSL (Secure Sockets Layer) is a standard security technology for establishing an encrypted link between a server and a client—typically.
Topic: Network programming
Network Programming Introduction
NETWORK PROGRAMMING CNET 441
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Network Programming Introduction
Network Programming Introduction
„Networking”.
CSCD 330 Network Programming
UNIT-6.
Socket programming - Java
Networking.
CSCD 330 Network Programming
Networking.
Outline Introduction Networking Basics Understanding Ports and Sockets
Socket Programming.
CSCD 330 Network Programming
Review Communication via paired sockets, one local and one remote
Presentation transcript:

Network Programming Communication between processes Many approaches: low level network API: sockets need to understand networking, addressing, etc. Object oriented approaches: RMI, CORBA Service based approaches: URL class. there are others

The Classes in java.net The Classes ContentHandler DatagramPacket DatagramSocket DatagramSocketImpl HttpURLConnection InetAddress MulticastSocket ServerSocket Socket SocketImpl URL ,URLConnection URLEncoder ,URLStreamHandler The Interfaces ContentHandlerFactory FileNameMap SocketImplFactory URLStreamHandlerFactory Exceptions BindException ConnectException MalformedURLException NoRouteToHostException ProtocolException SocketException UnknownHostException UnknownServiceException

Sockets Sockets API is popular C progamming API. A socket is an abstract communication endpoint. Different kinds of sockets for different kinds of communication: Stream (connected byte stream). Datagram (individual messages). To use sockets you need to understand lots of network stuff, including the OSI reference model, network addressing, byte-ordering, ...

Java Sockets Sockets in Java is much simpler than in C hides some of the details. API is “fit” to be used with TCP/IP, not as generic. Different objects used for different kinds of network communication: Socket (Stream Client) ServerSocket (Stream Server) DatagramSocket (Message oriented).

Socket and ServerSocket (TCP based communication) Establish a connection with peer process. socket provides an IO stream associated with the connection. all Java IO methods/Objects can be used. New exceptions to deal with. Great it what you really need is to communicate using nothing more than a stream. Other endpoint could be written in any language!

DatagramSocket and DatagramPacket (UDP) Message-oriented communication. send a message, receive a message not dependent on Java IO support... Lots of issues: not reliable. order not guaranteed. duplication of messages is possible.

Java Sockets and IP Addresses • Addressing the connection: • Address or name of remote machine • Port number to identify purpose • Port numbers: • Range from 0–65535

Java Sockets and IP Addresses class InetAddress Methods static InetAddress getByName(String); static InetAddress getLocalHost(); byte getAddress(); String getHostAddress(); String getHostName();

Internet Addresses Every computer on the Internet is identified by a unique, four-byte IP address. This is typically written in dotted quad format like 199.1.32.90 where each byte is an unsigned value between 0 and 255. Java's java.net.InetAddress class represents such an address. Among others it contains methods to convert numeric addresses to host names and host names to numeric addresses. public static InetAddress getByName(String host) throws UnknownHostException public static InetAddress[] getAllByName(String host) throws UnknownHostException public static InetAddress getLocalHost() throws UnknownHostException

public boolean isMulticastAddress() public String getHostName() public byte[] getAddress() public String getHostAddress() public int hashCode() public boolean equals(Object obj) public String toString()

TCP (stream) Client vs. Server Clients use Socket object need to specify the address of the server! IP address and port number can get stream associated with the connection. Servers use ServerSocket object need to set server port number. can receive new connections. new connection creates a Socket object.

TCP/IP Server import java.net.*; import java.io.*; public class SimpleServer { public static void main(String args[]) { ServerSocket s = null; Socket s1; String sendString = "Hello Net World!"; int slength = sendString.length(); OutputStream s1out; DataOutputStream dos; // Register your service on port 5432 try { s = new ServerSocket(5432); } catch (IOException e) { }

TCP/IP Server // Run the listen/accept loop forever while (true) { try { // Wait here and listen for a connection s1=s.accept(); // Get a communication stream associated with the socket s1out = s1.getOutputStream(); dos = new DataOutputStream (s1out); // Send your string! (UTF provides machine independence) dos.writeUTF(sendString); // Close the connection, but not the server socket dos.close(); s1out.close(); s1.close(); } catch (IOException e) { } } } }

TCP/IP Client import java.net.*; import java.io.*; public class SimpleClient { public static void main(String args[]) throws IOException { int c; Socket s1; InputStream s1In; DataInputStream dis; // Open your connection to a server, at port 5432 // localhost used here s1 = new Socket("127.0.0.1",5432); // Get an input file handle from the socket and // read the input s1In = s1.getInputStream(); dis = new DataInputStream(s1In); String st = new String (dis.readUTF()); System.out.println(st); // When done, just close the connection and exit dis.close(); s1In.close(); s1.close(); } }

UDP Sockets • Are used for connection-less protocol • Messages are not guaranteed • Are supported in Java technology through the DatagramSocket and DatagramPacket classes

The DatagramPacket DatagramPacket has two constructors: one for receiving data and one for sending data. • DatagramPacket(byte [] recvBuf, int readLength) • DatagramPacket(byte [] sendBuf, int sendLength, InetAddress iaddr, int iport)

The DatagramSocket DatagramSocket has three constructors: • DatagramSocket() • DatagramSocket(int port) • DatagramSocket(int port, InetAddress iaddr)

UDP Server import java.io.*; import java.net.*; import java.util.*; public class UdpServer{ //This method retrieves the current time on the server public byte[] getTime(){ Date d= new Date(); return d.toString().getBytes(); } // Main server loop. public void go() throws IOException { DatagramSocket datagramSocket; // Datagram packet from the client DatagramPacket inDataPacket; // Datagram packet to the client DatagramPacket outDataPacket;

UDP Server // Client return address InetAddress clientAddress; // Client return port int clientPort; // Incoming data buffer. Ignored. byte[] msg= new byte[10]; // Stores retrieved time byte[] time; // Allocate a socket to man port 8000 for requests. datagramSocket = new DatagramSocket(8000); System.out.println("UDP server active on port 8000");

UDP Server while(true) { // Loop forever // Set up receiver packet. Data will be ignored. inDataPacket = new DatagramPacket(msg, msg.length); // Get the message. datagramSocket.receive(inDataPacket); // Retrieve return address information, including // InetAddress and port from the datagram packet just recieved. clientAddress = inDataPacket.getAddress(); clientPort = inDataPacket.getPort(); // Get the current time. time = getTime(); //set up a datagram to be sent to the client using the current time, the //client address and port outDataPacket = new DatagramPacket( time, time.length, clientAddress, clientPort); //finally send the packet datagramSocket.send(outDataPacket); }}

UDP Server public static void main(String args[]) { UdpServer udpServer = new UdpServer(); try { udpServer.go(); } catch (IOException e) { System.out.println("IOException occured with socket."); System.out.println (e); System.exit(1); } } }

UDP Client import java.io.*; import java.net.*; public class UdpClient { public void go() throws IOException, UnknownHostException{ DatagramSocket datagramSocket; // Datagram packet to the server DatagramPacket outDataPacket; // Datagram packet from the server DatagramPacket inDataPacket; // Server host address InetAddress serverAddress; // Buffer space. byte[] msg = new byte[100];

UDP Client // Received message in String form. String receivedMsg; // Allocate a socket by which messages are sent and received. datagramSocket = new DatagramSocket(); // Server is running on this same machine for this example. // This method can throw an UnknownHostException. serverAddress = InetAddress.getLocalHost(); // Set up a datagram request to be sent to the server. //Send to port 8000. outDataPacket = new DatagramPacket(msg, 1, serverAddress, 8000); // Make the request to the server. datagramSocket.send(outDataPacket);

UDP Client // Set up a datagram packet to receive // server's response. inDataPacket = new DatagramPacket(msg, msg.length); // Receive the time data from the server datagramSocket.receive(inDataPacket); // Print the data received from the server receivedMsg = new String(inDataPacket.getData(), 0, inDataPacket.getLength()); System.out.println(receivedMsg); //close the socket datagramSocket.close();} public static void main(String args[]) { UdpClient udpClient = new UdpClient(); try { udpClient.go(); } catch (Exception e) { System.out.println ("Exception occured with socket."); System.out.println (e); System.exit(1); } } }

Sample Talk Client/Server Talker class: threaded(runnable) reads from an input stream, writes to an output stream. TalkServer class: creates ServerSocket, waits for client to connect. creates 2 Talker objects and runs them. TalkClient class: creates Socket, connects to server.