WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter

Slides:



Advertisements
Similar presentations
Using TCP sockets in Java Created by M Bateman, A Ruddle & C Allison As part of the TCP View project.
Advertisements

Sockets For Servers Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University.
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.
Network Programming Chapter 11 Lecture 6. Networks.
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.
Basic Socket Programming with Java. What is a socket?  Generally refers to a stream connecting processes running in different address spaces (across.
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.
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 2 Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Internet Programming In Java. References Java.sun.com Java552 Many of the programs shown.
COMP1681 / SE15 Introduction to Programming
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.
Networking Support In Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Programming TCP Clients Version InetAddress Class An IP address identifies uniquely a host in the internet, which consists of 4 numbers (1 byte.
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.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
Socket Programming -What is it ? -Why bother ?. Basic Interface for programming networks at transport level It is communication end point Used for inter.
CS 352-Socket Programming & Threads Dept. of Computer Science Rutgers University (Thanks,this slides taken from er06/
DBI Representation and Management of Data on the Internet.
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.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
CS 11 java track: lecture 6 This week: networking basics Sockets Vectors parsing strings.
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.
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 Using JAVA Asma Shakil Semester 1, 2008/2009.
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.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Networking A network represents interconnection of computers that is capable.
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.
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.
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 ( 李德成 )
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.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
Network Programming. These days almost all devices.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit9: Internet programming 1.
Java 13. Networking public class SumTest {
Socket Programming Ameera Almasoud
Object-Orientated Analysis, Design and Programming
MCA – 405 Elective –I (A) Java Programming & Technology
NETWORK PROGRAMMING CNET 441
Networking with Java 2.
An Introduction to TCP/IP and Sockets
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Sockets and URLs 17-Sep-18.
Sockets and URLs 13-Nov-18.
Clients and Servers 19-Nov-18.
Clients and Servers 1-Dec-18.
Networking.
Sockets and URLs 3-Dec-18.
Programming TCP Sockets
Clients and Servers 19-Jul-19.
CS18000: Problem Solving and Object-Oriented Programming
Clients and Servers 13-Sep-19.
Presentation transcript:

WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter

WECPP2 Aims To understand Java networking To appreciate socket programming Define the client socket How to use sockets Overview of server sockets

WECPP3 Review Internet basics TCP/IP protocol (contra UDP/IP) –ensures reliable transmission of data IP address –identifies Internet host (strictly interface) Domain name –user-friendly way of identifying hosts –DNS maps names to IP addresses (and vice-versa) Port –identifies particular service on a host (e.g. HTTP = 80)

WECPP4 Sockets –abstract input-output devices –could apply to any sort of device, but most often referred to in the context of networking: software mechanism for one program to talk to another Two-way connection between two programs –full-duplex link –more than one socket can be associated with a port

WECPP5 Client and server sockets Client socket and server socket need to be programmed in different ways Client socket assumes the server is ready to accept connections Server needs to listen for connection requests

WECPP6 How is a socket used? A new socket is created by a program The socket attempts to connect to a remote host Once the connection has been established, the local machine and the remote machine send and receive data When the transmission is complete, one or both machines closes the connection

WECPP7 Socket Programming Java uses ‘class Sockets’ to interface to TCP sockets (import java.net.*) The constructor method for the class: creates a TCP socket allows the user to specify a host and port attempts to connect to the host Socket connection = new Socket(" 80); Socket connection = new Socket(InetAddress, port);

WECPP8 When connection is established... The client host port is chosen by the system at run time The client port number can be accessed using: connection.getLocalPort() A socket is closed when: –The socket is garbage collected –The program ends –The program explicitly closes it: connection.close()

WECPP9 Errors and exceptions ConnectException –The connection is refused by the remote host. –Host could be busy or there might be no server on the port NoRouteToHostException –The connection has timed out.

WECPP10 Input with sockets For input it is most convenient to create a BufferedReader (java.io) as follows: BufferedReader in = new BufferedReader (new InputStreamReader(socket.getInputStream())); This enables readLine to be used for string input. Data ends when null is returned. Example: String line; while ((line = in.readLine()) != null) { processLine(); }

WECPP11 Output with sockets For output to a stream it is convenient to create a PrintWriter (java.io) object as follows: PrintWriter out = new PrintWriter(socket.getOutputStream(), true); Use method println to output a line. out.println("string"); Closing a socket closes all of the associated streams.

WECPP12 Socket crashes When a program reads from a socket, the call on read blocks (waits) until the data has been obtained But if the remote host crashes, the program is left hanging An alternative is to call setSoTimeout(timeout) –Sets the maximum waiting time (in milliseconds) When the time expires, an InterruptedException is thrown

WECPP13 Port scanner example A port scanner is a program that hunts for working ports on an Internet host –Warning: can be impolite PortScanner.java –Checks for unknown host –Attempts to connect to ports 1 to 256 of specified host –Ignores ports where no socket can be established

WECPP14 Get server time example Connect to server's port 13 Read a line of text (containing time and date) GetRemoteTime.java Of course it cannot connect if no service is running

WECPP15 Class Sockets DescriptionExample constructors public Socket(String host, int port) creates a new Socket object corresponding to destination host and port Socket socket = new Socket(" k", 80); public Socket(InetAddress host, int port) creates a new Socket object corresponding to the InetAddress object and port Socket socket = new Socket(inetAddress, 80);

WECPP16 Class Sockets DescriptionExample Object Method public InetAddress getInetAddress() returns the InetAddress object corresponding to the host that is, or will be, connected to. InetAddress iNE = socket.getInetAddress( ); public int getPort()returns the port number of the host to which the socket is, or will be, connected. int port = socket.getPort();

WECPP17 Class Sockets DescriptionExample Object Method public int getLocalPort() returns the local port number. int port = socket.getLocalPort(); public InputStream getInputStream() returns an input stream that can be used by the program for input. InputStream is = socket.getInputStream(); public OutputStream getOutputStream() returns an output stream that can be used by the program for output. OutputStream os = socket.getOutputStream();

WECPP18 Class Sockets DescriptionExample Object Method public synchronized void close() disconnects a socket. This also closes the streams associated with the socket socket.close(); public String toString()returns a string representation of a socket with remote host name, remote IP address, remote port, local port. String details = socket.toString() public synchronized void setSoTimeout(int ms) sets the time-out to the parameter value (in milliseconds) socket. setSoTimeout(18000 );

WECPP19 Sockets and Server Sockets Socket: –Implemented by class socket –A socket object is a connection to a certain host on a particular port –Socket objects are used for data transfer –A Socket object initiates a connection Server socket: –Implemented by class ServerSocket –A SocketServer object just waits on a particular port for incoming connections from anywhere –A ServerSocket object waits for connections

WECPP20 The algorithm for a server 1 Create ServerSocket object on a particular port ServerSocket server = new ServerSocket(12345, 100); ServerSocket object listens for any connection attempts Socket connection = server.accept(); The server is blocked (waits) until a client attempts to connect When a client attempts to connect, the server wakes up

WECPP21 The algorithm for a server 2 When a client connects, method accept returns a new Socket object representing the connection between the server and the client That socket object's getInputStream and/or getOutputStream methods are called to obtain streams which can be used to send/receive data The server and the client interact using streams according to an agreed protocol When finished, the server, the client, or both close the connection

WECPP22 Daytime server example DaytimeServer.java Listens on port 13 When a connection is accepted it uses the socket's output stream to write the current date and time Then closes the socket and listens for a new connection (forever)

WECPP23 More complex servers A server dealing with one connection cannot respond to further connections –Normally, connection requests are queued May be better to create a new thread to deal with each connection –Allows main thread to keep listening for new connections

WECPP24 Reading Bell and Parr - bonus chapter ming/ starthere.html ming/ starthere.html Deitel, chapter 18