Socket programming. Complement for the programming assignment INFO-0010



Similar documents
Java Network. Slides prepared by : Farzana Rahman

Building a Multi-Threaded Web Server

Network Communication

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

Data Communication I

Network/Socket Programming in Java. Rajkumar Buyya

THE PROXY SERVER 1 1 PURPOSE 3 2 USAGE EXAMPLES 4 3 STARTING THE PROXY SERVER 5 4 READING THE LOG 6

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

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

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

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

Data Communication & Networks G

TCP/IP Networking An Example

Network Technologies

URLs and HTTP. ICW Lecture 10 Tom Chothia

Socket-based Network Communication in J2SE and J2ME

Chapter 11. User Datagram Protocol (UDP)

Outline Definition of Webserver HTTP Static is no fun Software SSL. Webserver. in a nutshell. Sebastian Hollizeck. June, the 4 th 2013

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

Network Programming TDC 561

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

Protocolo HTTP. Web and HTTP. HTTP overview. HTTP overview

Network-Oriented Software Development. Course: CSc4360/CSc6360 Instructor: Dr. Beyah Sessions: M-W, 3:00 4:40pm Lecture 2

Web. Services. Web Technologies. Today. Web. Technologies. Internet WWW. Protocols TCP/IP HTTP. Apache. Next Time. Lecture # Apache.

LESSON Networking Fundamentals. Understand TCP/IP

Creating a Simple, Multithreaded Chat System with Java

Hypertext for Hyper Techs

The Web: some jargon. User agent for Web is called a browser: Web page: Most Web pages consist of: Server for Web is called Web server:

Computer Networks/DV2 Lab

No. Time Source Destination Protocol Info HTTP GET /ethereal-labs/http-ethereal-file1.html HTTP/1.

CS640: Introduction to Computer Networks. Applications FTP: The File Transfer Protocol

Network Programming with Sockets. Process Management in UNIX

Java Programming: Sockets in Java

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

Lesson: All About Sockets

HTTP. Internet Engineering. Fall Bahador Bakhshi CE & IT Department, Amirkabir University of Technology

Chapter 27 Hypertext Transfer Protocol

NETWORK PROGRAMMING IN JAVA USING SOCKETS

Building a Java chat server

Socket Programming in Java

Lecture 2-ter. 2. A communication example Managing a HTTP v1.0 connection. G.Bianchi, G.Neglia, V.Mancuso

HTTP Protocol. Bartosz Walter

Socket Programming. Announcement. Lectures moved to

Internet Technologies. World Wide Web (WWW) Proxy Server Network Address Translator (NAT)

The Application Layer. CS158a Chris Pollett May 9, 2007.

Lecture 28: Internet Protocols

APACHE WEB SERVER. Andri Mirzal, PhD N

The Hyper-Text Transfer Protocol (HTTP)

Objectives of Lecture. Network Architecture. Protocols. Contents

Network Programming using sockets

1. When will an IP process drop a datagram? 2. When will an IP process fragment a datagram? 3. When will a TCP process drop a segment?

ICT SEcurity BASICS. Course: Software Defined Radio. Angelo Liguori. SP4TE lab.

Project #2. CSE 123b Communications Software. HTTP Messages. HTTP Basics. HTTP Request. HTTP Request. Spring Four parts

CTIS 256 Web Technologies II. Week # 1 Serkan GENÇ

Overview. Securing TCP/IP. Introduction to TCP/IP (cont d) Introduction to TCP/IP

1 Recommended Readings. 2 Resources Required. 3 Compiling and Running on Linux

Chapter 3. Internet Applications and Network Programming

CONTENT of this CHAPTER

SSC - Communication and Networking Java Socket Programming (II)

DNS: Domain Names. DNS: Domain Name System. DNS: Root name servers. DNS name servers

An Android-based Instant Message Application

Assignment 4 Solutions

CPS221 Lecture: Layered Network Architecture

1. The Web: HTTP; file transfer: FTP; remote login: Telnet; Network News: NNTP; SMTP.

Internet Concepts. What is a Network?

Introduction to Socket programming using C

Network-based Applications. Pavani Diwanji David Brown JavaSoft

An Overview of Java. overview-1

Transparent Redirection of Network Sockets 1

Mail User Agent Project

Division of Informatics, University of Edinburgh

Lab 4: Socket Programming: netcat part

Security-Assessment.com White Paper Leveraging XSRF with Apache Web Server Compatibility with older browser feature and Java Applet

Research of Web Real-Time Communication Based on Web Socket

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

CS 5480/6480: Computer Networks Spring 2012 Homework 1 Solutions Due by 9:00 AM MT on January 31 st 2012

BASIC ANALYSIS OF TCP/IP NETWORKS

HTTP Caching & Cache-Busting for Content Publishers

PROFESSIONAL. Node.js BUILDING JAVASCRIPT-BASED SCALABLE SOFTWARE. Pedro Teixeira WILEY. John Wiley & Sons, Inc.

First Workshop on Open Source and Internet Technology for Scientific Environment: with case studies from Environmental Monitoring

NETWORKING FEATURES OF THE JAVA PROGRAMMING LANGUAGE

CS105, Spring 2016 Proxy Lab: Writing a Caching Web Proxy See course webpage for due dates

The Open Source Bro IDS Overview and Recent Developments

Computer Networks Practicum 2015

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

Exercises: FreeBSD: Apache and SSL: pre SANOG VI Workshop

Communicating with a Barco projector over network. Technical note

Network Programming. Writing network and internet applications.

FTP client Selection and Programming

Network Models OSI vs. TCP/IP

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

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

ELEN 602: Computer Communications and Networking. Socket Programming Basics

Transport Layer Protocols

CloudOYE CDN USER MANUAL

IP Network Layer. Datagram ID FLAG Fragment Offset. IP Datagrams. IP Addresses. IP Addresses. CSCE 515: Computer Network Programming TCP/IP

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 HTTP protocol Debugging tools

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

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 ( ) ; Sources : http://bit.ly/zlquyj 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.

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

Java.net.URL Internet resources and services are typically identified and located through Uniform Resource Identifier/Locator. beware : parsing them properly is harder than you might think. Many fields are optionals, u=new URL(<string>) creates an internal object from an URL, ready to use accessor methods extract parts you need, e.g. u.getport() make sure you use what is expected by the protocol (e.g. GET /path?query HTTP/1.1, no host, no ref, no port here!)

HTTP example GET /pixs/bilourun3b.gif HTTP/1.0 CRLF User-Agent : Wget/1.12 (linux-gnu) CRLF Accept : */* CRLF Host : dsgametools.sf.net 1 CRLF Connection : Keep-Alive 2 CRLF CRLF HTTP/1.1 200 OK CRLF Server : Apache/2.2.3 (CentOS) CRLF Last-Modified : Wed, 17 Feb 2010 09 :14 :23 GMT CRLF ETag : "3a6-47fc846b079c0" CRLF Cache-Control : max-age=259200 CRLF Expires : Sun, 06 Mar 2011 21 :15 :26 GMT CRLF Content-Type : image/gif CRLF Content-Length : 936 CRLF Date : Thu, 03 Mar 2011 21 :15 :26 GMT CRLF Connection : keep-alive CRLF CRLF GIF89a 3....@h.8..X.0x.8.85 @...P...!...!..NETSCAPE2.0... 1. Mandatory 2. Better use Connection: close for your tests 3. Binary content. Charset settings are important in that case.

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.