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

Slides:



Advertisements
Similar presentations
Multiplexing/Demux. CPSC Transport Layer 3-2 Multiplexing/demultiplexing application transport network link physical P1 application transport network.
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.
Prepared By E. Musa Alyaman1 Java Network Programming TCP.
Java Threads A tool for concurrency. OS schedules processes Ready Running 200 Blocked A process loses the CPU and another.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
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
Programming Applets How do Applets work ? This is an HTML page This is the applet’s code It has a link to an applet.
1 Networking with Java 2: The Server Side. 2 Some Terms Mentioned Last Week TCP -Relatively slow but enables reliable byte-stream transmission UDP -Fast.
CIS – Spring Instructors: Geoffrey Fox, Bryan Carpenter Computational Science and.
Client/Server In Java An Introduction to TCP/IP and Sockets.
Networking Support In Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CSE 341, S. Tanimoto Java networking- 1 Java Networking Motivation Network Layers Using Sockets A Tiny Server Applets URLs Downloading Images, MediaTracker.
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.
CS 352-Socket Programming & Threads Dept. of Computer Science Rutgers University (Thanks,this slides taken from er06/
CS4273: Distributed System Technologies and Programming I Lecture 5: Java Socket Programming.
Web Security Programming I Building Security in from the Start Except where otherwise noted all portions of this work are Copyright (c) 2007 Google and.
NET0183 Networks and Communications Lecture 31 The Socket API 8/25/20091 NET0183 Networks and Communications by Dr Andy Brooks Lecture powerpoints from.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
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.
Web Design & Development 1 Lec - 21 Umair Javed. Web Design & Development 2 Socket Programming.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Networking COMP # 22.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/11) Java Sockets and Simple Networking Joel Adams and Jeremy Frens.
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.
1 CSCD 330 Network Programming Spring 2014 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 7 Application.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Peter Lee April 8, 2004.
1 CSCD 330 Network Programming Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 9 Client-Server Programming.
Part 4: Network Applications Client-server interaction, example applications.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
1 COMP 431 Internet Services & Protocols Client/Server Computing & Socket Programming Jasleen Kaur February 2, 2016.
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.
Java Server Sockets ServerSocket : Object to listen for client connection requests Throws IOException accept() method to take the client connection. Returns.
Distributed Systems CS Project 1: File Storage and Access Kit (FileStack) Recitation 1, Aug 29, 2013 Dania Abed Rabbou and Mohammad Hammoud.
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.
CS390- Unix Programming Environment CS 390 Unix Programming Environment Topics to be covered: Java Packages Thread Example Java Socket Programming.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Using TCP sockets in Perl Created by M Bateman, A Ruddle & C Allison As part of the TCP View project.
Network Programming: Servers. Agenda l Steps for creating a server Create a ServerSocket object Create a Socket object from ServerSocket Create an input.
Networking Code CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
Java Networking I IS Outline  Quiz #3  Network architecture  Protocols  Sockets  Server Sockets  Multi-threaded Servers.
SOCKET PROGRAMMING WITH JAVA By Collin Donaldson.
Liang, Oreilly, Herbert Schildt, Joseph O’Neil, Simon Roberts, IBM Corp Advanced Java Programming CSE 7345/5345/ NTU 531 Multithreaded/Sockets/Server Welcome.
Network Programming. These days almost all devices.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Echo Networking COMP
Threads in Java Two ways to start a thread
CSE 341, S. Tanimoto Java networking-
Client-server Programming
Networking with Java 2.
An Introduction to TCP/IP and Sockets
PRESENTED To: Sir Abid………. PRESENTED BY: Insharah khan………. SUBJECT:
Clients and Servers 19-Nov-18.
Clients and Servers 1-Dec-18.
Multiplexing/Demux.
Clients and Servers 19-Jul-19.
Clients and Servers 13-Sep-19.
Presentation transcript:

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

Overview TCP socket Client/Server Multithread server Thread pooling server Alternate thread pooling server

TCP Provides Process to process communication –Use tuple of IP address & port Reliable In order Socket is one end-point of a two way connection link

TCP Socket Operations

TCP in Java

Implemented in java.net.* Two main classes –java.net.ServerSocket - for server –java.net.Socket- for client Provides abstractions over –Socket and connect operations –Bind, listen & accept Remember to close the socket at the end

ServerSocket Constructors –ServerSocket (port) –ServerSocket (port, backlog) –ServerSocket (port, backlog, bindAddress) Where –port = the TCP port to listen on –backlog = TCP queue length –bindAddress = interface to use (else all)

Socket Construction –Two important constructors –Socket () – random local port –Socket (host, port) – remote host and port Is the end point for communication –Used in both the client and server –Client connects to ServerSocket –ServerSocket returns Socket for communications

Daytime server import java.net.*; import java.io.* public class DTServer { public static void main (String argv[]) { int dayTimePort = 13; try { ServerSocket dtserver = new ServerSocket (dayTimePort); while (Socket con = dtserver.accept ()) { PrintWriter out = new PrintWriter (con.getOutputStream (), true); Date now = new Date (); out.println (now.toString ()); con.close (); } } catch (Exception e) {} }

Daytime client import java.net.*; import java.io.* public class DTClient { public static void main (String argv[]) { int dayTimePort = 13; try { Socket con = new Socket (argv[0], dayTimePort); InputStream is = con.getInputStream (); BufferedReader br = new BufferedReader (new InputStreamReader (is)); String time = br.readLine (); System.out.println (“The time at “ + argv[0] + “ is “ + time); con.close (); } catch (Exception e) {} }

Issues Can only deal with one request at once Performance –Have to wait until the guy in front has finished with the server

Solution Threads Thread theory not covered here –Basically allows server to do more than one thing at once (see operating system course to find out why this isn’t true) Two ways –Extend java.lang.Thread –Implement java.lang.Runnable Override public void run () public class TThread extend Thread { public void run () { for (int i = 0; i < 100; i++) { System.out.println (i); } public static void main (String argv[]) { new TThread ().start (); } public class RThread implements Runnable { public void run () { for (int i = 0; i < 100; i++) { System.out.println (i); } public static void main (String argv[]) { new Thread (new TThread ()).start (); }

Multithreaded Daytime import java.net.*; import java.io.* public class DTServer { public static void main (String argv[]) { int dayTimePort = 13; try { ServerSocket dtserver = new ServerSocket (dayTimePort); while (Socket con = dtserver.accept ()) { DTThread dtthread = new DTThread (); // create the new thread dtthread.setSocket (con); dtthread.start (); // start the thread now } } catch (Exception e) {} }

Daytime Thread import java.net.*; public class DTThread extends Thread { Socket con = null; public void setSocket (Socket con) { this.con = con; } public void run () { // Important work done here PrintWriter out = new PrintWriter (con.getOutputStream (), true); Date now = new Date (); out.println (now.toString ()); con.close (); }

Issues Performance –Thread creation at client connection –Creating threads takes time Possible deadlock –Due to multithreaded

Thread Pools - Basic import java.net.*; import java.io.* public class DTServer { public static void main (String argv[]) { int dayTimePort = 13; List list = Collections.synchronizedList(new LinkedList()); for (int i = 0; i < 10; i ++) { list.add (new DTThread ()); } // create 10 threads try { ServerSocket dtserver = new ServerSocket (dayTimePort); while (Socket con = dtserver.accept ()) { DTThread dtthread = (DTThread)list.remove (0); dtthread.setSocket (con); dtthread.start (); // start the thread now list.add (new DTThread ()); // create a thread to create the one we just used } } catch (Exception e) {} }

Issues Possible deadlock Resource thrashing Concurrency errors Thread leakage Request overload Performance –Thread creation still has to be performed in the main loop

Pool Alternative Implementation Create pool as before Create thread which makes sure the pool has n thread waiting On connection take thread from the pool –The above thread makes sure there are always threads in the pool waiting to be used

Summary