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

Similar documents
Lesson: All About Sockets

Building a Multi-Threaded Web Server

Creating a Simple, Multithreaded Chat System with Java

Division of Informatics, University of Edinburgh

Java Network Programming. The java.net package contains the Socket class. This class speaks TCP (connection-oriented protocol).

Application Development with TCP/IP. Brian S. Mitchell Drexel University

Socket-based Network Communication in J2SE and J2ME

Java Network. Slides prepared by : Farzana Rahman

Socket Programming in Java

Learning Outcomes. Networking. Sockets. TCP/IP Networks. Hostnames and DNS TCP/IP

Socket programming. Complement for the programming assignment INFO-0010

Transparent Redirection of Network Sockets 1

Network Communication

Introduction to Java. Module 12: Networking (Java Sockets) Prepared by Costantinos Costa for EPL 233. ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 1

Java Programming: Sockets in Java

Advanced Network Programming Lab using Java. Angelos Stavrou

Socket Programming. Announcement. Lectures moved to

Network/Socket Programming in Java. Rajkumar Buyya

TP1 : Correction. Rappels : Stream, Thread et Socket TCP

NETWORK PROGRAMMING IN JAVA USING SOCKETS

String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivepacket.getaddress(); int port = receivepacket.

Transparent Redirection of Network Sockets 1

Network Programming using sockets

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime?

Building a Java chat server

Socket programming. Socket Programming. Languages and Platforms. Sockets. Rohan Murty Hitesh Ballani. Last Modified: 2/8/2004 8:30:45 AM

Limi Kalita / (IJCSIT) International Journal of Computer Science and Information Technologies, Vol. 5 (3), 2014, Socket Programming

Agenda. Network Programming and Java Sockets. Introduction. Internet Applications Serving Local and Remote Users

Java from a C perspective. Plan

Socket Programming. Srinidhi Varadarajan

!"# $ %!&' # &! ())*!$

Assignment 4 Solutions

Data Communication & Networks G

! "# $%&'( ) * ).) "%&' 1* ( %&' ! "%&'2 (! ""$ 1! ""3($

The difference between TCP/IP, UDP/IP and Multicast sockets. How servers and clients communicate over sockets

CSS 543 Program 3: Online Tic-Tac-Toe Game Professor: Munehiro Fukuda Due date: see the syllabus

Manual. Programmer's Guide for Java API

CS11 Java. Fall Lecture 7

CS506 Web Design and Development Solved Online Quiz No. 01

An Android-based Instant Message Application

Client-server Sockets

Java Programming Unit 10. Stock Price Quotes with URL, Sockets, and RMI

An Overview of Java. overview-1

Abhijit A. Sawant, Dr. B. B. Meshram Department of Computer Technology, Veermata Jijabai Technological Institute

Communicating with a Barco projector over network. Technical note

Crash Course in Java

Network Programming. Writing network and internet applications.

JAVA Program For Processing SMS Messages

Files and input/output streams

ODROID Multithreading in Android

ELEN 602: Computer Communications and Networking. Socket Programming Basics

Mail User Agent Project

public static void main(string[] args) { System.out.println("hello, world"); } }

Java Interview Questions and Answers

Database Access from a Programming Language: Database Access from a Programming Language

Database Access from a Programming Language:

Fundamentals of Java Programming

Transport layer protocols. Message destination: Socket +Port. Asynchronous vs. Synchronous. Operations of Request-Reply. Sockets

3.5. cmsg Developer s Guide. Data Acquisition Group JEFFERSON LAB. Version

SSC - Communication and Networking Java Socket Programming (II)

Android Application Development Course Program

Threads & Tasks: Executor Framework

Topics. Producing Production Quality Software. Concurrent Environments. Why Use Concurrency? Models of concurrency Concurrency in Java

Java Server Pages and Java Beans

Summer Internship 2013

JAVA - MULTITHREADING

Introduktion til distribuerede systemer uge 37 - fil og webserver

Vehicle Maintenance and Tracking System (VMTS)

Session NM059. TCP/IP Programming on VMS. Geoff Bryant Process Software

SO_REUSEPORT Scaling Techniques for Servers with High Connection Rates. Ying Cai

CHAPTER 6. Transmission Control Protocol. 6.1 Overview

Multithreaded Programming

PLV Goldstein 315, Tuesdays and Thursdays, 6:00PM-7:50PM. Tuesdays and Thursdays, 4:00PM-5:30PM and 7:50PM 9:30PM at PLV G320

CSc31800: Internet Programming, CS-CCNY, Spring 2004 Jinzhong Niu May 9, Java Servlets

When the transport layer tries to establish a connection with the server, it is blocked by the firewall. When this happens, the RMI transport layer

Massachusetts Institute of Technology 6.005: Elements of Software Construction Fall 2011 Quiz 2 November 21, 2011 SOLUTIONS.

FILE I/O IN JAVA. Prof. Chris Jermaine Prof. Scott Rixner

Java 2 Web Developer Certification Study Guide Natalie Levi

Aspect-Oriented Development of a P2P File Sharing Application

How To Write A Program For The Web In Java (Java)

Chapter 3. Internet Applications and Network Programming

Implementing Network Software

TITLE NETWORK PROGRAMMING (SECURED CLIENT-SERVER CHAT APPLICATION) A PROJECT REPORT PRESENTED ACHILE UGBEDE JOHN CE/2007/165

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

CISC 4700 L01 Network & Client- Server Programming Spring Harold, Chapter 8: Sockets for Clients

XII. Distributed Systems and Middleware. Laurea Triennale in Informatica Corso di Ingegneria del Software I A.A. 2006/2007 Andrea Polini

Design of Cloud based Instant Messaging System on Android Smartphone using Internet

2. Follow the installation directions and install the server on ccc

Capario B2B EDI Transaction Connection. Technical Specification for B2B Clients

Computer Science 483/580 Concurrent Programming Midterm Exam February 23, 2009

CRYPTOGRAPHY 456 ANDROID SECURE FILE TRANSFER W/ SSL

Network Programming with Sockets. Process Management in UNIX

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

Intranet, Extranet, Firewall

Chapter 2 Introduction to Java programming

Coding Standard for Java

Remote Method Invocation

Servlets. Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

The Java I/O System. Binary I/O streams (ascii, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits)

Transcription:

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 Server process socket () bind () listen () accept () Client process socket () Block waiting for a request connect () read () Process request write () write () read ()

TCP in Java Server process ServerSocket () accept () Block waiting for a request Client process Socket () Socket () write () read () Process request read () write ()

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

Constructors ServerSocket 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 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 TThread ().start (); 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