Socket programming. Complement for the programming assignment INFO-0010

Similar documents
Assignment, part1: feedback and common mistakes. INFO-0010 Samuel HIARD

COMP 213. Advanced Object-oriented Programming. Lecture 20. Network Programming

Introduction to computer networking

Java Support for developing TCP Network Based Programs

CS September 2017

JAVA Network API. 2 - Connection-Oriented vs. Connectionless Communication

Introduction to Socket Programming

Advanced Java Programming. Networking

Data Communication EDA344, DIT420 Description of Lab 1 and Optional Programming HTTP Assignment

Transport Layer. Chapter 3: Transport Layer

Connecting to a Server Implementing Servers Making URL Connections Advanced Socket Programming

CSC 4900 Computer Networks: Transport Layer

protocols September 15,

CSCD 330 Network Programming

Distributed Systems Recitation 2. Tamim Jabban

CS 4390 Computer Networks. Transport Services and Protocols

OBJECT ORIENTED PROGRAMMING

C19: User Datagram and Multicast

Outlines. Networking in Java. Internet hardware structure. Networking Diagram. IP Address. Networking in Java. Networking basics

Multi-threaded Web Server (Assignment 1) Georgios Georgiadis

Chapter 3 Transport Layer

Chapter 11. Application-Layer Elements Ports

Internet Technology 2/7/2013

CSC 4900 Computer Networks: P2P and Sockets

Networking, Java threads and synchronization. PRIS lecture 4 Fredrik Kilander

Different Layers Lecture 21

Assignment, part 2. Statement and concepts INFO-0010

Networking Basics. network communication.

CSCE 463/612 Networks and Distributed Processing Spring 2018

Joseph Faber Wonderful Talking Machine (1845)

Network Programming. Introduction to Sockets. Dr. Thaier Hayajneh. Process Layer. Network Layer. Berkeley API

JAVA SOCKET PROGRAMMING

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

Transport Layer. The transport layer is responsible for the delivery of a message from one process to another. RSManiaol

Chapter 7 Transport Layer. 7.0 Introduction 7.1 Transport Layer Protocols 7.2 TCP and UDP 7.3 Summary

Workbook A dialog box will appear asking for you to select your "workspace". Leave the default as is, which should be /home/crsid/workspace.

CSEN 503 Introduction to Communication Networks. Mervat AbuElkheir Hana Medhat Ayman Dayf. ** Slides are attributed to J. F.

Pieter van den Hombergh Richard van den Ham. March 17, 2018

Different Layers Lecture 20

Interprocess Communication

Software Practice 1 - Socket

Internet Protocol. Chapter 5 Protocol Layering. Juho Kim Graduate School of Information & Technology Sogang University

Transport Layer. Gursharan Singh Tatla. Upendra Sharma. 1

Outline. Inter-Process Communication. IPC across machines: Problems. CSCI 4061 Introduction to Operating Systems

Application Layer Introduction; HTTP; FTP

TCP and Concurrency. The third assignment at DA

Chapter 3 Transport Layer

The TCP Protocol Stack

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Network. Dr. Jens Bennedsen, Aarhus University, School of Engineering Aarhus, Denmark

Info 408 Distributed Applications programming 2 nd semester of 2017/2018 Credits: 5 Lecturer: Dr. Antoun Yaacoub

: Distributed Systems Principles and Paradigms Assignment 1 Multithreaded Dictionary Server

Computer Networks - A Simple HTTP proxy -

Networking and Security

CSE 461 Module 11. Connections

CSE 461 Module 10. Introduction to the Transport Layer

CSC 401 Data and Computer Communications Networks

Merge Sort Quicksort 9 Abstract Windowing Toolkit & Swing Abstract Windowing Toolkit (AWT) vs. Swing AWT GUI Components Layout Managers Swing GUI

UDP and TCP. Introduction. So far we have studied some data link layer protocols such as PPP which are responsible for getting data

CMSC 332 Computer Networks Transport Layer

OSI Transport Layer. Network Fundamentals Chapter 4. Version Cisco Systems, Inc. All rights reserved. Cisco Public 1

CN1047 INTRODUCTION TO COMPUTER NETWORKING CHAPTER 6 OSI MODEL TRANSPORT LAYER

CSc 450/550 Computer Networks Network Architectures & Client-Server Model

Transport Layer (TCP/UDP)

Outline of Topics. UDP Socket Java Programming. Multicast in Java. Real-time protocol (RTP) XMPP and Jingle protocols. Java I/O and New IO (NIO)

Assignment 2 CISC 650 Computer Networks Fall 2016 Due Date: 10/9/ :59PM ET Total Points: 100

Practical Session #09 Exceptions & Networking. Tom Mahler

Web Technology. COMP476 Networked Computer Systems. Hypertext and Hypermedia. Document Representation. Client-Server Paradigm.

CSC Systems Programming Fall Lecture - XXIII Final Review. Tevfik Koşar. Louisiana State University

Using make. Using Variables in Makefiles. Implicit Rules. Static vs Dynamic Libraries. Libraries. Tevfik Ko!ar

QUIZ: Longest Matching Prefix

Unix Network Programming

C18: Network Fundamentals and Reliable Sockets

Programming Methods Dr Robert Harle

Distributed Systems Recitation 2. Tamim Jabban

Requirements. PA4: Multi-thread File Downloader Page 1. Assignment

CS 10: Problem solving via Object Oriented Programming. Client/Server

Application Protocols and HTTP

Internet and Intranet Protocols and Applications

Introduction to Programming Using Java (98-388)

Suprakash Datta. Office: CSEB 3043 Phone: ext Course page:

TCP/IP Transport Layer Protocols, TCP and UDP

Principles of Software Construction. Introduction to networks and distributed systems School of Computer Science

(Refer Slide Time: 1:09)

Java Networking (sockets)

CSCD 330 Network Programming

Protocols SPL/ SPL

Java Overview An introduction to the Java Programming Language

Tommy Färnqvist, IDA, Linköping University

1 OBJECT-ORIENTED PROGRAMMING 1

Web Server Project. Tom Kelliher, CS points, due May 4, 2011

Data Communication & Computer Networks MCQ S

Interprocess Communication Mechanisms

ощ 'ршорвшэш! цвн-эориэу ощ 'sajbpossv # PIPG DUJ 'ssjmoossv ^ PIPG pipa w н OX ЛЮН VAV

shared storage These mechanisms have already been covered. examples: shared virtual memory message based signals

Lecture 9: Transpor Layer Overview and UDP

BITS-Pilani Hyderabad Campus

CSCD 330 Network Programming Spring 2018

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Homework 4 (document version 1.0) Network Programming using C

Previous lecture: threads G51PRG: Introduction to Programming Second semester Lecture 12 URL

Transcription:

Socket programming Complement for the programming assignment INFO-0010

Outline Socket definition Briefing on the Socket API A simple example in Java Multi-threading and Synchronization Example : HTTP protocol Debugging tools Project overview

What are sockets? Interface to network protocol stack Typically to transport layer Connectionless (Datagram) Socket Connection-Oriented Socket Transport (UDP and TCP) Network Data Link (MAC / LLC) Physical (PHY)

What are sockets? (2) A socket is an end-point of communication which identifies a local process at one end of a communication association A socket is a half association { protocol, local-address, local-port A communication association is identified by two half associations { protocol, local-address, local-port, remote-address, remote-port

Communication models Datagrams (UDP) Message-oriented Connectionless Unreliable No congestion control Connections (TCP) Stream-oriented Requires a connection Reliable (no packet loss, no reordering) Congestion control Raw For trafic generation/capture

UDP vs TCP Conceptually: Application 1000 bytes Sent datagram = received datagram 1000 bytes Application Socket Network Socket Host1 No congestion control No reordering Packet loss Host2 UDP

UDP vs TCP Conceptually: Application 1000 bytes Stream oriented: - May require multiple reads - Order preserved - No loss 512 bytes 488 bytes Application Socket Network Socket Waiting for more flush() (congestion) Host1 Host2 TCP

Connections Implemented by TCP Reliable stream transfer Chunks read may be different from chunks sent But streams are identical Guarantees delivery and ordering provided connection not broken Does congestion control What you have sent to the socket may not have left the box yet! You can use out.flush() to force it Programmer must check how many bytes should be read Convention or application protocol header InputStream can provide readfully()

Sockets' life cycle (syscalls)

Socket's life cycle (Java)

Simple example : client/server chat telnet towards a well-known server, from each client The server duplicates each incoming message to all the clients.

The Client Side a bot import java.i o.*; import java.net.*; class Bot { Use explicit variable names. We mainly focus on the class names in this example. public static void main (String argv [ ] ) throws Exception { Socket s = new Socket ( "localhost",8086) ; OutputStream out = s.getoutputstream () ; InputStream in = s.getinputstream () ; byte msg[ ] = new byte [64] ; out.write ("ANnA joined the channel".getbytes () ) ; while ( true ) { s.close ( ) ; if (in.read(msg) <=0) break ; if (new String(msg).startsWith("ANnA") ) main() should always catch exceptions. Would only make the code harder to read in this example. out.write( "ANnA feels fine, thanks.\n". getbytes ( ) ) ;

The Server Side Incoming Connection class Server { public static void main ( String argv [ ] ) throws Exception { ServerSocket ss = new ServerSocket (8086) ; while ( true ) { Socket ts = ss.accept () ; OutputStream out = ts.getoutputstream() ; InputStream in = ts.getinputstream() ; out.write("hello, this is the echo server". getbytes()) ; byte msg [ ] = new byte [64] ; while ( true ) { int len= in.read(msg); // get bytes (max 64) if (len <=0) break ; // connection closed by peer? out.write(msg,0,len); // send them away. out.flush(); // don t wait for more. ts.close (); What if multiple clients connect simultaneously?

The Server Side multithreading class Server { public static void main ( String argv [ ] ) throws Exception { ServerSocket ss = new ServerSocket (8086) ; while ( true ) { Socket ts = ss.accept () ; Worker w = new Worker(ts); w.start(); //Worker extends Thread We spawn a thread every time a connection arrives That fresh-new thread will deal with the new client. And the main thread can return to the welcoming of incoming clients.

The Server Side defining a thread class Worker extends Thread { Socket s ; Worker ( Socket _s ) { s = _s ; @Override public void run ( ) { try { OutputStream out = s.getoutputstream() ; InputStream in = s.getinputstream() ; out.write("hello, this is the echo server". getbytes()) ; byte msg [ ] = new byte [64] ; while ( true ) { int len= in.read(msg); // get bytes (max 64) if (len <=0) break ; // connection closed by peer? out.write(msg,0,len); // send them away. out.flush(); // don t wait for more. s.close (); //acknowledge end of connection catch ( Exception any ) { System.err.println("worker died " + any) ;

Extending the "Thread" class One way to create thread in Java 1. Define class that extends class called java.lang.thread 2. Create object of that class Must override the public void run ( ); method and state so by adding @Override in the method signature to prevent a warning

Implementing the Runnable interface Another way to create thread in Java 1. Define class that implements interface called java.lang.runnable 2. Create object of class Thread, passing object of above class to Thread constructor N.B. an interface is like a class with method signatures but no method implementations implementing interface is like extending class But, in addition, must provide implementations of all method signatures in specified interface In Runnable there s only one signature public void run ( );

Shared objects What if some objects need to be manipulated by different threads? For instance, we could keep a list (e.g. ArrayList) of all the OutputSteam and turn the "echo" server into a real chat server. Multiple threads will use elements of the list simultaneously Execution is concurrent and non-atomic Consistency is thus not ensured Solution: only one thread at a time can use the elements in the list Deem the sending phase a critical section Implement mutual exclusion over critical section i.e. prevent multiple threads from entering at once

The Server Side shared objects // all is just an ArrayList where each OutputStream is.add() ed // out is the OutputStream corresponding to the Socket from which we are receiving void send ( byte msg [ ], int len ) throws Exception { synchronized ( all ) { for ( Enumeration e = all.elements ( ) ; e.hasmoreelements() ; ) { OutputStream o = (OutputStream ) e.nextelement() ; if ( o!= out ) { o.write(msg,0, len ) ; // send them away. o.flush(); // don t wait for more.

Example : The HTTP protocol GET /index.html HTTP/1.1 [request headers] client HTTP/1.1 200 OK [response headers] <html> <body> </body></html> server Applicative protocol over TCP. Retrieve resources (files, web pages) on a machine Text-oriented protocol (vs. e.g. binary BitTorrent) Options (content-type, length, etc) : cf RFC2616

A few debugging tools telnet (for direct connection) Each bug has its proper catcher. So, use catch and printstacktrace( ) wisely!

strace enetwork f java Server Pid 4199 System calls

strace enetwork f java Server Pid 4199 Pid 4211 Socket N 5 = new ServerSocket(8086) # bytes to send # bytes handled by TCP

Some command lines (examples are better commented on the web). javac t1.java to compile java Server to launch telnet localhost 8086 to test strace -e trace=network -f java Server to track system calls issued by your program netstat -tlp to list server sockets netstat -tcp to list running connections. curl -I http://www.perdu.com shows the headers for a HEAD method curl http://www.perdu.com dumps URL content on standard output.

Guidelines for programming assignment #1 INFO-0010-2/4 2016-2017

Context You will develop a client/server application in order to play Guess Who. Java (1.8) Sockets. Console input/output (no fancy GUI). Imposed protocol. To be realized alone. HARD deadline : March, 24th, 2017 img src : http://www.nanozine.org/guess-who-board-game-characters.html

Game rules 1. A character is selected at random (for instance, Megan). 2. User asks for clues (including Name) that can be answered only by a yes or no answer (e.g. "is he/she wearing a hat?"). 3. The answer is provided to the question (yes or no). 4. If the user guessed the character correctly, go to 5, otherwise go to 2. 5. Game over. User wants a new game? Go to 1, otherwise, quit.

Architecture 5. Updates output 4. Sends response, e.g.: "1 1 3 Yes" 3. Finds answer to the question 1. Creates a question, e.g. "Does he/she have blond hair?" 2. Sends request, e.g.: "1 1 10 hair=blond" Img src for Host and server from GNS3.

Guess Who Protocol (GWP)

(Un)intentional malevolence What happens if I send "1 5 0", "1 1 0" or "2 0 0"? Good behaviour : Send "1 0 11 Bad Request". Bad behaviour : Anything else. Worst case : Trigger exception. Never expect, always check! What happens if I send "1 1 10"? (not for INFO-0010-2) Server waits for the rest of the request, that never comes. If single-threaded, cannot handle new connections. One thread for new connections, then one thread per connection. Use Socket time-outs, close connection if too long (~ 20 seconds).

Extra guidelines List of characters imposed, see.csv file Port number : 20xx, where xx = group ID on submission platform Class named "GuessWhoClient" and "GuessWhoServer". Boolean variable inside "GuessWhoServer.java", named isbeingevaluated, set to false by default, which, when set to true, will make the server always select the first character (Megan). No "package" instruction, no shutdown hooks, limited libraries for import, no file manipulation. Fully functional on student machines (ms8**.montefiore.ulg.ac.be). Short report. Send submission to Montefiore Submission Platform (submit.run.montefiore.ulg.ac.be) before March, 24th 2017!

Automatic testing Provided jar file for automatic testing