UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.

Slides:



Advertisements
Similar presentations
Network Programming and Java Sockets
Advertisements

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.
Socket Programming.
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
1 Java Networking – Part I CS , Spring 2008/9.
WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter
1 L53 Networking (2). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
Java Socket Support Presentation by: Lijun Yuan Course Number: cs616.
CS3771 Today: network programming with sockets  Previous class: network structures, protocols  Next: network programming Sockets (low-level API) TODAY!
Client/Server In Java An Introduction to TCP/IP and Sockets.
© 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.
Network Programming and Java Sockets 1 Dr. Christian Vecchiola Postdoctoral Research Fellow Cloud Computing and Distributed Systems.
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)
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.
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. Client/Server Communication At a basic level, network-based systems consist of a server, client, and a media for communication as.
TCP/IP protocols Communication over Internet is mostly TCP/IP (Transmission Control Protocol over Internet Protocol) TCP/IP "stack" is software which allows.
NET0183 Networks and Communications Lecture 31 The Socket API 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
Network Programming CSE 132. iClicker/WUTexter Question How many of the following statements are true? In the model-view-controller paradigm, a swing.
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.
Internet Applications and Network Programming Dr. Abraham Professor UTPA.
 TCP (Transport Control Protocol) is a connection-oriented protocol that provides a reliable flow of data between two computers.  TCP/IP Stack Application.
RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
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.
1 Chapter 28 Networking. 2 Objectives F To comprehend socket-based communication in Java (§28.2). F To understand client/server computing (§28.2). F To.
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.
Web Design & Development 1 Lec - 21 Umair Javed. Web Design & Development 2 Socket Programming.
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 Using JAVA Asma Shakil Semester 1, 2008/2009.
Socket-Programming.  Establish contact (connection).  Exchange information (bi-directional).  Terminate contact.
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
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 Networking A network represents interconnection of computers that is capable.
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.
Prepared by Dr. Jiying Zhao University of Ottawa Canada.
Java Programming II Java Network (I) Java Programming II.
Distributed Systems CS Project 1: File Storage and Access Kit (FileStack) Recitation 1, Aug 29, 2013 Dania Abed Rabbou and Mohammad Hammoud.
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.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
SOCKET PROGRAMMING WITH JAVA By Collin Donaldson.
1 Network Communications A Brief Introduction. 2 Network Communications.
Network Programming. These days almost all devices.
Network Programming Communication between processes Many approaches:
Java 13. Networking public class SumTest {
Lecture 21 Sockets 1 (Not in D&D) Date.
MCA – 405 Elective –I (A) Java Programming & Technology
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Network Programming Introduction
CSCD 330 Network Programming
UNIT-6.
Networking.
Outline Introduction Networking Basics Understanding Ports and Sockets
Presentation transcript:

UNIT-6

Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server

Networking is interconnection among computers either by using cables or satellite.  The term network programming refers to writing programs that execute across multiple devices (computers), in which the devices are all connected to each other using a network. The system that receives the service is a client. The system that provides the service is called server. Three requirements to establish a network: Hardware: it includes the computers, cables, modems, hubs etc.. Software: includes programs to communicate between clients and servers Protocol: represents a way to establish connection and helps in sending and receiving data in a standard format. Basics of Networking

 The java.net package contains a collection of classes and interfaces which are used to implement programs to communicate across network.  The java.net provides support for the two common network protocols:  TCP: Transmission Control Protocol, which allows for reliable communication between two applications. TCP is typically used over the Internet Protocol, which is referred to as TCP/IP.  UDP: User Datagram Protocol, a connection-less protocol that allows for packets of data to be transmitted between applications.

Contd … IP Address (Internet Protocol Address )  It is an unique identifier which is assigned to device (computer) to identify itself and communicate with other devices in the Internet Protocol network.  Networks using the TCP/IP protocol route messages based on the IP address of the destination.  The format of an IP address is a 32-bit numeric address written as four numbers separated by periods.  Each number can be 0 to 255. For example: could be an IP address.  Otherwise, IP Address is used to identify host.

Port  Generally a host has many applications.  Port is used to identify the specific application.  It is a 16-bit identifier.  A port represents an endpoint for network communications.  Port numbers allow different applications on the same computer to utilize network resources without interfering with each other. Application WWW Telnet Port

Socket  A socket represents a logical connecting point between a server and a client, so that communication can be done through that point. It is an object used for network programming.  Sockets are bidirectional, meaning that either side of the connection is capable of both sending and receiving data.  A socket is bound to a specific port number.  Network communication using Sockets is very much similar to performing file I/O.  The streams used in file I/O operation are also applicable to socket-based I/O.

The following steps occur when establishing a TCP connection between two computers using sockets: 1. The server instantiates a ServerSocket object, denoting which port number communication is to occur on. That is it provides a mechanism for the server program to listen for clients and establish connections with them. 2. The server invokes the accept() method of the ServerSocket class. This method waits until a client connects to the server on the given port. 3. After the server is waiting, a client instantiates a Socket object, specifying the server name and port number to connect to.

3. The constructor of the Socket class attempts to connect the client to the specified server and port number. If communication is established, the client now has a Socket object capable of communicating with the server. 4. On the server side, the accept() method returns a reference to a new socket on the server that is connected to the client's socket. 5. After the connections are established, communication can occur using I/O streams. Each socket has both an OutputStream and an InputStream. The client's OutputStream is connected to the server's InputStream, and the client's InputStream is connected to the server's OutputStream. 5. TCP is a two way communication protocol, so data can be sent across both streams at the same time.

Classes used to create communication between client and server:  The java.net.ServerSocket class is used by server applications to obtain a port and listen for client requests.  It is used to implement Server.  The java.net.Socket class represents the socket that both the client and server use to communicate with each other.  It is used to implement Client.  The client obtains a Socket object by instantiating one, whereas the server obtains a Socket object from the return value of the accept() method.

ServerSocket Constructors  public ServerSocket() throws IOException Creates an unbound server socket. When using this constructor, use the bind() method when you are ready to bind the server socket.  public ServerSocket(int port) throws IOException Attempts to create a server socket bound to the specified port. An exception occurs if the port is already bound by another application.

ServerSocket Methods  public int getLocalPort() Returns the port that the server socket is listening on.  public Socket accept() throws IOException Waits for an incoming client. This method blocks until either a client connects to the server on the specified port or the socket times out.  public void bind(SocketAddress host, int backlog) Binds the socket to the specified server and port in the SocketAddress object. Use this method if you instantiated the ServerSocket using the no-argument constructor.

Socket Constructors  public Socket() Creates an unconnected socket. Use the connect() method to connect this socket to a server.  public Socket(String host, int port) throws UnknownHostException, IOException. It attempts to connect to the specified server at the specified port. If this constructor does not throw an exception, the connection is successful and the client is connected to the server.

Socket Methods  public void connect(SocketAddress host, int timeout) throws IOException It connects the socket to the specified host. This method is needed only when you instantiated the Socket using the no-argument constructor.  public InputStream getInputStream() throws IOException Returns the input stream of the socket. The input stream is connected to the output stream of the remote socket.  public OutputStream getOutputStream() throws IOException Returns the output stream of the socket. The output stream is connected to the input stream of the remote socket.  public void close() throws IOException Closes the socket, which makes this Socket object no longer capable of connecting again to any server.

Client-Server Communication socket() bind() listen() accept() read() write() Server block process request Client socket() connect() write() establish connection send request read() send response

Implementing a Server 1. Open the Server Socket: ServerSocket server; DataOutputStream os; DataInputStream is; server = new ServerSocket( PORT ); 2. Wait for the Client Request: Socket s = server.accept(); 3. Create I/O streams for communicating to the client is = new DataInputStream( s.getInputStream() ); os = new DataOutputStream( s.getOutputStream() ); 4. Perform communication with client Receive from client: String line = is.readLine(); Send to client: os.write ("Hello\n"); 5. Close Connections: client.close();os.close();is.close();s.close();

Implementing a Client 1. Create a Socket Object: Socket client; DataOutputStream dos; DataInputStream dis; client = new Socket( serveradd, portnumber ); 2. Create I/O streams for communicating with the server. dis = new DataInputStream(client.getInputStream() ); dos = new DataOutputStream( client.getOutputStream() ); 3. Perform I/O or communication with the server: Receive data from the server: String line = is.readLine(); Send data to the server: os.write ("Hello\n"); 4. Close the socket when done: client.close();dos.close();dis.close();

import java.io.*;import java.net.*; class Server{ public static void main(String args[])throws Exception{ ServerSocket ss=new ServerSocket(1212); Socket s=ss.accept(); System.out.println("Connection Established\n"); DataOutputStream dos=new DataOutputStream(s.getOutputStream()); DataInputStream dis=new DataInputStream(s.getInputStream()); DataInputStream kb=new DataInputStream(System.in); while(true){ String str,str1; while((str=dis.readLine())!=null){ System.out.println(str); str1=kb.readLine(); dos.writeUTF(str1); } dos.close();dis.close(); kb.close();ss.close(); s.close();System.exit(0); }

import java.io.*; import java.net.*; class Client{ public static void main(String args[])throws Exception{ Socket s=new Socket("localhost",1212); DataOutputStream dos=new DataOutputStream(s.getOutputStream()); DataInputStream dis=new DataInputStream(s.getInputStream()); DataInputStream kb=new DataInputStream(System.in); String str,str1; while(!(str=kb.readLine()).equals("exit")){ dos.writeBytes(str+"\n"); str1=dis.readUTF(); System.out.println(str1); } dos.close(); dis.close(); kb.close(); s.close(); }

For multithreaded server: while(true) { i. wait for client requests (step 2 above) ii. create a thread with “client” socket as parameter (the thread creates streams (as in step (3) and does communication as stated in (4). Remove thread once service is provided. }

Make your search of the internet easier by using a parallel search engine instead of an individual search engine. A parallel search engine searches multiple search engines simultaneously and returns the results in one list. Duplicate hits are eliminated. Try the great sites listed below!

Multiple Clients Multiple clients can connect to the same port on the server at the same time. Incoming data is distinguished by the port to which it is addressed and the client host and port from which it came. The server can tell for which service (like http or ftp) the data is intended by inspecting the port. It can tell which open socket on that service the data is intended for by looking at the client address and port stored with the data.