Jan. 20041 Java Networking TCP Yangjun Chen Dept. Business Computing University of Winnipeg.

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

Jan Java Networking UDP Yangjun Chen Dept. Business Computing University of Winnipeg.
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.
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.
Internet Programming In Java. References Java.sun.com Java552 Many of the programs shown.
COMP1681 / SE15 Introduction to Programming
CIS – Spring Instructors: Geoffrey Fox, Bryan Carpenter Computational Science and.
Client/Server In Java An Introduction to TCP/IP and Sockets.
Projekt współfinansowany przez Unię Europejską w ramach Europejskiego Funduszu Społecznego „Networking”
Networking Support In Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
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.
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.
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.
DBI Representation and Management of Data on the Internet.
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.
 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.
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.
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.
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.
1 cs205: engineering software university of virginia fall 2006 Network Programming* * Just enough to make you dangerous Bill Cheswick’s map of the Internet.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Java Socket Programming.
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.
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 ( 李德成 )
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.
Advance Computer Programming Networking Basics – explores the java.net package which provides support for networking. – Also Called “programming for the.
Network Programming. These days almost all devices.
Network Programming Communication between processes Many approaches:
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit9: Internet programming 1.
Java 13. Networking public class SumTest {
Lecture 21 Sockets 1 (Not in D&D) Date.
Network Programming Introduction
NETWORK PROGRAMMING CNET 441
An Introduction to TCP/IP and Sockets
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Network Programming Introduction
Java Network Programming
„Networking”.
CSCD 330 Network Programming
Networking.
CSCD 330 Network Programming
CS18000: Problem Solving and Object-Oriented Programming
Presentation transcript:

Jan Java Networking TCP Yangjun Chen Dept. Business Computing University of Winnipeg

Jan Ports In general, computers only have one Internet address, but computers usually need to communicate with more than one host at a time. Ports map incoming data to a particular process or application running on a computer, With every socket, there needs to be an associated port number Example - regular post. The IP address is like the street address while the port number would represent the house number.

Jan Sever - client communication A socket can be considered as a connection point. IP address port1 portN IP s …... IP c …... Port’1 Port’M IP address SEVER: CLIENT: Socket s (Ip c, Port’sM, port1) Socket c (Ip s, Port1, port’M)

Jan Ports Ports can range in value from 1 to 65535, with ports 1 to 1023 being reserved for root access in Unix. On Windows NT, 95 and Macs, any user can listen on any port. Only one program can listen on a given TCP port at the same time. However, many remote hosts can connect to the same remote port.

Jan Internet Addresses Every computer on the net has an associated four-byte IP address that is unique. Usually represented in dotted quad format like where each byte is an unsigned value in the range of 0 to 255. Since numbers are hard to remember, these numbers are mapped into names like or It's the numerical address that is fundamental, not the name. You can have multiple names that represent the same IP address.

Jan Internet Addresses java. net. InetAddress class are used to manage such addresses. Some are shown here: - - InetAddress getByName(String host) -InetAddress[] getAllByName(String host) -InetAddress getLocalHost() -String getHostName() -byte[] getHostAddress() - boolean isMulticastAddress()

Jan Creating InetAddress Objects The InetAddress class is unusual in that it doesn't have a public constructor, so to create an object you would pass in the host name or string format of the dotted quad address into the static method: InetAddress.getByName( )

Jan Creating InetAddress Objects import java.net.*; class ibm{ public static void min (String args[]) { try ( InetAddress[] addresses = InetAddress.getAllEyName(" for (int i = 0; i<addresses.length;i++) System.out.println(addresses[i]); }//try catch (UnknownHostException e) {} }//main }//class

Jan Creating InetAddress Objects To create an InetAddress that refers to your own machine use the static getLocalHost() method. try { InetAddress me = InetAddress.getLocalHost( ); }//try catch (UnknownHostException e) { System.err.println( ); }//catch

Jan Parsing InetAddress Given an InetAddress, we might want to know the host name or it's IP address as a string or byte array. ImporL java.net.*; public class address{ public static void main(String args[]) { try { InetAddress me = new netAddress.getLocalHost(); System.out.println(me.getHostName()); System.out.println(me.getHostAddress());

Jan Parsing InetAddress byte[] quad = me.getAddress(); for(int i = 0; i < quad.length; i++) System.out.print(quad[i] + ”.”); System.out.println(); }//try catch (UnknownHostException e) { ( System.err.println(e); }//catch }//main }//class

Jan TCP TCP (Transmission Control Protocol) -a connection-based protocol that provides a reliable flow of data between two computers. -It allows retransmission of lost data. -It provides multiple paths through different routers in case one goes down. -Bytes are delivered in the order they are sent. Applications using TCP to communicate - HTTP, FTP, Telnet, etc.

Jan TCP Java networking classes use TCP java.net package provides the functionality for networking -java.net. URL(). URLConnection(). Socket(). SocketServer()

Jan Sockets Host's native networking s/w handles the splitting of data into packets on the transmitting side of a connection, and the reassembly of packets on the receiving side. Java programmers are presented with a higher level abstraction called a socket.

Jan Sockets A socket represents a reliable connection for data transmission between two hosts. Four fundamental operations - 1. Connect to a remote machine - 2. Send data - 3. Receive data - 4. Close the connection

Jan Establishing a Connection Accomplished through the class constructors: -java.net.Socket(). client side implementation - java.net.ServerSocket(). server side implementation Each socket is associated with exactly one host. To connect to a different host, a new socket object must be created.

Jan Sending/Receiving Data Sending and Receiving data is accomplished with input and output streams. - public InputStream getInputStream() - public OutputStream getOutputStream() - both of these classes throw an IOException

Jan Closing a Connection There's a method to close a socket. -public synchronized void close() - this method throws an IOException as well. For a well behaved program, we need to close the I/O streams as well. Close any streams connected to a socket before closing the socket. - OutStream.close() - InStream.close() -Socket.close()

Jan Example: EchoTest(l) import java.io.*; import java.net.*, public class EchoTest { public static void main(String[] args) { String host="truffle"; Socket eSocket = null; DataoutputStream os = null; DataInputstream is = null; DataInputStream stdIn = new DataInpuuStream(System.in);

Jan Example: EchoTest(2) try { eSocket = new Socket(host,7); os = new DataOutputStream(eSocket.getOutputStream()); is = new DataTnputStream(eSocket.getInputStream()); } //try catch (UnknownHostException e) { System.err.println(“Unknown host: "+host); }//catch catch (IOException e) { System.err.println("No I/O connection to: “ + host); }///catch

Jan Example: EchoTest(3) if (eSocket !=null && os!=null && is!=null) { try { String uTnput; while ((uInput=stdIn.readLine()) != null) { os.writeBytes(userlnput); os.writeByte('\n'); System.out.println("echo: +is.readLineo); if (userInput.equals("quit”)) break; }//while os.close(); is.close();

Jan Example: EchoTest(4) eSocket.close(); }//try catch (IOException e) System.err.println("I/O failed on the connection to: “ + host); }//catch }//if }//try }//class

Jan Example: Chat Client(l) import java.io.*; import java.net.*; import RcveData; import SendData; public class chat_client extends Thread { public static void main(String a[]) throws IOException { Socket sock = null; Stringhost = “localhost"; intport9100; Declaring a Socket object

Jan Example: Chat Client(2) if (a.length > 1) { port = Integer.parseInt(a[i]); } //if if (a.length > 0) { host = a[0]; }//if System.out.println("using port “+ port + “connecting to “+ host); try { sock = new Socket(host,port); }//try Establish a connection to the specified bost and port

Jan Example: Chat Client(3) catch (java.net.ConnectException e){ System.out.println(e); System.exit(0); }//catch SendDatasd = new SendData(sock); RcveData rd = new RcveData(sock); sd.start(); rd.start(); }//main }//chat_client Instantiate the supporting classes

Jan Server Sockets A host that responds to a connection. Instead of connecting to a remote host, a server program will wait for others to connect to it.

Jan Accepting Connections 1) A server socket binds to a particular port. 2) It then listens for connection attempts. 3) When it detects an attempt, it will accept the connection. - public Socket accept() Throws IOException The accept () method blocks until a connection is detected. It returns a java. net. Socket Object that is used to perform the communication.

Jan Server Sockets Multiple clients can connect to the same port on a server at any time. The data are distinguished by the port numbers and the client addresses. However, no more than one socket can listen to a particular port at a time. This is why servers tend to be heavily multithreaded. Generally, the server socket listening to the port will accept connections and then pass the actual processing to other threads.

Jan Server Sockets:Queue Incoming connections are stored in a FIFO queue until the server can accept them. If the queue is full, then connection attempts will be refused until space opens up. The default queue length for most systems is between 5 and 50.

Jan Server Sockets:Queue The length of the queue can be adjusted by passing in the size of the queue in the “backlog" parameter. - public ServerSocket(int port,int backlog) Each system has a maximum queue length, so any values for the queue length longer than the maximum will be set to the maximum.

Jan Example: Chat Server(l) //This is a simple chat server written in Java import java.io.*; *import java.net.*; import RcveData; import SendData; public class chat_server extends Thread {

Jan Example: Chat Server(2) public static void main(String a[]) { intblog = 600; intport = 9100; Socketsock = null; if (a.length > 0) { port = Integer.parseInt(a[0]); }//if ServerSocket servsock - null;

Jan Example: Chat Server(3) System.out.println("using port “ + port); try { servsock = new Serversocket(port,blog); }// try catch (java.io.IOException e) { System.out.println(e); System.exit(0); }//catch try { sock = servsock.accept(); } //try

Jan Example: Chat Server(4) catch (java.io.IOException e) { System.out.println(e); System.exit(0); ) }//catch SendData Sd = new SendData(sock); RcveData rd = new RcveData(sock); sd.start(); rd.start(); }//main }//class

Jan Example: Send Data(1) import java.net.*; import java.io.*; public class SendData extends Thread { Socket sock; public SendData(Socket sock) { this.sock = sock; }//SendData constructor public void run() { String line;

Jan Example: Send Data(2) try { outputStreamWriter outw = new outputStreamwriter(sock.getOutputStream()); BufferedWriter sockout=new BufferedWriter(outw); TnputStreamReader inr = new InputStreamReader(System.in); BufferedReader in = new BufferedReader(inr); while ((line = in.readLine()) != null) { sockout.write(line+"\n");

Jan Example: Send Data(3) sockout.flush(); yield(); }//while }//try catch (java.io.IOException e) { System.out.println(e); System.exit(0); }//catch }//run }//SendData

Jan Example: Receive Data(1) import java.net.*; import java.io.*; public class RcveData extends Thread { Socket sock; public RcveData(Socket sock) { this.sock = sock; } public void run() { String line;

Jan Example: Receive Data(2) try { InputStreamReader inr = new InputStreamReader(sock.getInputStream()); BufferedReader in = new BufferedReader(inr); while (line= in.readLine( )) != null) { System.out.print(“Receiving: ”); System.out.println(line); yield(); }//while }//try

Jan Example: Receive Data(3) catch (java.io.IOException e) { system.out.println(e); System.exit(0); }//catch }//run }//RcveData