1 Programming Section 9 James King 12 August 2003.

Slides:



Advertisements
Similar presentations
Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings.
Advertisements

Network Programming Chapter 11 Lecture 6. Networks.
Advanced Java Class Network Programming. Network Protocols Overview Levels of Abstraction –HTTP protocol: spoken by Web Servers and Web Clients –TCP/IP:
Socket Programming.
User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive.
Prepared By E. Musa Alyaman1 User Datagram Protocol (UDP) Chapter 5.
1 Java Networking – Part I CS , Spring 2008/9.
 Pearson Education, Inc. All rights reserved. 1 CH24 Networking : OBJECTIVES In this chapter you will learn:  To understand Java networking.
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.
McGraw-Hill©The McGraw-Hill Companies, Inc., 2004 Application Layer PART VI.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L22 (Chapter 25) Networking.
1 School of Computing Science Simon Fraser University CMPT 771/471: Internet Architecture and Protocols Socket Programming Instructor: Dr. Mohamed Hefeeda.
© 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.
Networking Support In Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
2: Application Layer1 Socket Programming. 2: Application Layer2 Socket-programming using TCP Socket: a door between application process and end- end-transport.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 25 Networking.
Julia Ljunbjörk and Anita Mugenyi. What is a socket? Like a house Between the layers.
Chapter 26 Client Server Interaction Communication across a computer network requires a pair of application programs to cooperate. One application on one.
TCP Sockets Reliable Communication. TCP As mentioned before, TCP sits on top of other layers (IP, hardware) and implements Reliability In-order delivery.
2: Application Layer 1 Socket Programming TCP and UDP.
JAVA Socket Programming Source: by Joonbok Lee, KAIST, 2003.
CS 352-Socket Programming & Threads Dept. of Computer Science Rutgers University (Thanks,this slides taken from er06/
SOCKET PROGRAMMING. Client/Server Communication At a basic level, network-based systems consist of a server, client, and a media for communication as.
Computer Networks  Network - A system of computers interconnected in order to share information.  Data transmission - consists of sending and receiving.
TCP/IP protocols Communication over Internet is mostly TCP/IP (Transmission Control Protocol over Internet Protocol) TCP/IP "stack" is software which allows.
Chapter 17 Networking Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William Stallings.
NET0183 Networks and Communications Lecture 31 The Socket API 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from.
1 Chapter Client-Server Interaction. 2 Functionality  Transport layer and layers below  Basic communication  Reliability  Application layer.
TCP/IP: Basics1 User Datagram Protocol (UDP) Another protocol at transport layer is UDP. It is Connectionless protocol i.e. no need to establish & terminate.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
Jozef Goetz, Application Layer PART VI Jozef Goetz, Position of application layer The application layer enables the user, whether human.
1 CSC111H Client-Server: An Introduction Dennis Burford
-1- Georgia State UniversitySensorweb Research Laboratory CSC4220/6220 Computer Networks Dr. WenZhan Song Associate Professor, Computer Science.
Vassil Roussev 2 A socket is the basic remote communication abstraction provided by the OS to processes. controlled by operating system.
ICOM 6115©Manuel Rodriguez-Martinez ICOM 6115 – Computer Networks and the WWW Manuel Rodriguez-Martinez, Ph.D. Lecture 26.
 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.
Application Layer 2-1 Chapter 2 Application Layer Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Addison-Wesley March 2012.
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.
1 Networking Chapter Distributed Capabilities Communications architectures –Software that supports a group of networked computers Network operating.
Chapter 2 Applications and Layered Architectures Sockets.
© 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.
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.
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.
Networking Terminology: ISP (Internet service provider) – dialup, dsl, cable LAN (local area network) IP (internet protocol) address, eg
ECEN “Internet Protocols and Modeling”, Spring 2012 Course Materials: Papers, Reference Texts: Bertsekas/Gallager, Stuber, Stallings, etc Class.
TCP/IP (Transmission Control Protocol / Internet Protocol)
1 Client-Server Interaction. 2 Functionality Transport layer and layers below –Basic communication –Reliability Application layer –Abstractions Files.
Distributed systems (NET 422) Prepared by Dr. Naglaa Fathi Soliman Princess Nora Bint Abdulrahman University College of computer.
Computer Communication: An example What happens when I click on
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 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.
CSE 501N Fall ‘09 22: Introduction to Networking November Nick Leidenfrost.
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
1 Network Communications A Brief Introduction. 2 Network Communications.
Distributed Computing & Embedded Systems Chapter 4: Remote Method Invocation Dr. Umair Ali Khan.
1 K. Salah Application Layer Module K. Salah Network layer duties.
Network Programming. These days almost all devices.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 33 Networking.
Network Programming Communication between processes Many approaches:
Socket Programming original by Joonbok Lee KAIST heavily adapted by /Jens.
Object-Orientated Analysis, Design and Programming
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.
Client server programming
Presentation transcript:

1 Programming Section 9 James King 12 August 2003

2 Internet and Network programming in TCP/IP Networking concepts Hello World in TCP Hello World in UDP Sending objects across the network Accepting multiple concurrent clients

3 Network programming Network concepts Networks allow several machines to communicate together. Within a room or department Internet Many different hardware and software technologies Ethernet, ATM, modem, ADSL, TCP/IP, IPX etc.. Enabling sharing of resources (files, printers etc) See computer networks modules for more detail We will look at how to write programs that transfer data using Internet Protocol – the internet standard

4 Internet Protocols IP provides the protocols to connect the internet together The only internet protocol Most common network protocol in small networks too... Relatively reliable and proven technology Provides two types of communication short packets called datagrams (UDP)  maximum packet size 64Kbytes  packets may not arrive in order  some packets may not arrive  Some packets may be duplicated Streams of bytes (TCP)  send any amount of data  data is sequenced (in order no duplicates) and reliable

5 Network programming Implementing network connections Each computer on network or internet has unique IP address which is made up of 4 numbers separated by. e.g always means yourself Each computer has a number of connection points available called ports e.g. port 6666 applications listen to one or more ports for data applications can not share the same port

6 Machine Network programming Implementing network connections Network/Internet Actual Flow of Data Operating System Application Network Layer socket 1234 Machine Operating System Application Network Layer socket 665 Apparent Flow of Data

7 TCP/IP to send Data across a network Networking Section

8 Simple TCP Networked Hello World Aim send the string “Hello World” from the client to the server using TCP/IP Server must be accepting on the port before the client can send Client must connect to the server before it can send Client and server should close the connection before exiting

9 Network programming Implementation of the TCP Server ServerSocket sv=new ServerSocket(6666); System.out.println("waitng for client"); Socket s=sv.accept(); InputStream i=s.getInputStream(); DataInputStream oi=new DataInputStream(i); String h=oi.readUTF(); System.out.println(h); port to listen to connects on wait for a connect. Conversation with client will use a temporary socket s read the one line message from the temporary socket display message on console

10 Implementation of the TCP Client Socket sv=new Socket("localhost",6666); OutputStream i=sv.getOutputStream(); DataOutputStream oi=new DataOutputStream(i); oi.writeUTF("Hello World"); i.flush(); oi.flush(); force sending the message by flushing any buffers write the message to the socket Java takes care of converting the string into something that can be send across a network port to connect to machine to connect to

11 What happens in what order Server creates main socket and blocks inside accept() waiting for a connection from the client Client creates socket and connects to server Server accept() returns with a temporary socket to talk to client on Server calls readUTF() and blocks Client sends a message using writeUTF() Server receives message and readUTF() returns the message as string Server prints message on console

12 UDP to send Data across a network Networking Section

13 Simple UDP Networked Hello World Aim send the string “Hello World” from the client to the server using UDP/IP there are no streams in UDP instead we have to make a UDP packet UDP packets can only contain an array of bytes so we have to convert the string to bytes before we can put it in a UDP packet To read the contents of a UDP packet we have to reverse this process UDP in C or C++ is much easier than in Java – fault of Java and not UDP or the internet

14 Implementation of a UDP server byte [] bytes=new byte[65536]; DatagramSocket sv=new DatagramSocket(6666); DatagramPacket p=new DatagramPacket(bytes,bytes.length); sv.receive(p); bytes=p.getData(); String h=new String(bytes,0,p.getLength()); System.out.println(h); create storage for the incoming data create a UDP socket on port 6666 Build a datagram packet and attach the storage to it receive a UDP packet and copy it into our DatagramPacket extract the bytes from the datagram convert the bytes into a string

15 Implementation of a UDP Client byte [] bytes="Hello World".getBytes(); DatagramSocket sv=new DatagramSocket(); DatagramPacket p=new DatagramPacket(bytes, bytes.length, InetAddress.getByName("localhost"), 6666); sv.send(p); convert the string hello world into an array of bytes create a datagram socket create a datagram packet, attach the data we want to send and the destination address send the packet

16 What happens in what order Server creates socket Server creates a byte array to store the incoming data Server builds a datagram packet to store the incoming datagram server calls receive and blocks waits for a datagram Client creates socket client marshals message into a byte array and puts the byte array into a Datagram packet Client sends message to server using send Server receives message and receive unblocks Server unmarshals the data from the datagram packet and converts it into String Server prints String on console

17 Serialisation to send Objects across a network Networking Section

18 Sending Instances of Classes across a network You can send your own instances of classes across a network using Java serialisation serialisation is the process of making a copy of the attributes that can be handled in a stream The class must implement Serializable The class must have a constructor with no parameters By default all attributes in the class are sent across the network You can implement your own read and write methods

19 Hello World Network Class public class Hello implements Serializable { private String hello=""; public Hello(String h) { hello=h; } public Hello() { hello=""; } public String message() { return hello; }

20 Sending Objects across the net using TCP Server Code ServerSocket sv=new ServerSocket(6666,2); Socket s=sv.accept(); InputStream i=s.getInputStream(); ObjectInputStream oi=new ObjectInputStream(i); Hello h=(Hello)oi.readObject(); System.out.println(h.message()); We use a ObjectInputStream instead of a DataInputStream We read an object using readObject and type cast it into what was sent instead of readUTF we ask the object to return its message

21 Sending Objects across the net using TCP Client Code Socket sv=new Socket("localhost",6666); OutputStream i=sv.getOutputStream(); ObjectOutputStream oi=new ObjectOutputStream(i); Hello h=new Hello("Hello World"); oi.writeObject(h); We use a ObjectOutputStream instead of a DataOutputStream We use writeObject instead of writeUTF