Lecture 11 Socket Programming.

Slides:



Advertisements
Similar presentations
Categories of I/O Devices
Advertisements

Umut Girit  One of the core members of the Internet Protocol Suite, the set of network protocols used for the Internet. With UDP, computer.
Network Programming Chapter 11 Lecture 6. Networks.
Socket Programming.
1 Java Networking – Part I CS , Spring 2008/9.
1 L53 Networking (2). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
Java Socket Support Presentation by: Lijun Yuan Course Number: cs616.
CS3771 Today: network programming with sockets  Previous class: network structures, protocols  Next: network programming Sockets (low-level API) TODAY!
System Programming Practical session 10 Java sockets.
McGraw-Hill©The McGraw-Hill Companies, Inc., 2004 Application Layer PART VI.
1 School of Computing Science Simon Fraser University CMPT 771/471: Internet Architecture and Protocols Socket Programming Instructor: Dr. Mohamed Hefeeda.
2: Application Layer1 Socket Programming. 2: Application Layer2 Socket-programming using TCP Socket: a door between application process and end- end-transport.
CEG3185 Tutorial 4 Prepared by Zhenxia Zhang Revised by Jiying Zhao (2015w)
TCP Sockets Reliable Communication. TCP As mentioned before, TCP sits on top of other layers (IP, hardware) and implements Reliability In-order delivery.
Process-to-Process Delivery:
2: Application Layer 1 Socket Programming TCP and UDP.
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/
Chapter 17 Networking Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William Stallings.
1 Chapter Client-Server Interaction. 2 Functionality  Transport layer and layers below  Basic communication  Reliability  Application layer.
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
Jozef Goetz, Application Layer PART VI Jozef Goetz, Position of application layer The application layer enables the user, whether human.
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
Vassil Roussev 2 A socket is the basic remote communication abstraction provided by the OS to processes. controlled by operating system.
ICOM 6115©Manuel Rodriguez-Martinez ICOM 6115 – Computer Networks and the WWW Manuel Rodriguez-Martinez, Ph.D. Lecture 26.
RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut.
Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming.
1. I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net.
The Socket Interface Chapter 21. Application Program Interface (API) Interface used between application programs and TCP/IP protocols Interface used between.
1 Chapter 28 Networking. 2 Objectives F To comprehend socket-based communication in Java (§28.2). F To understand client/server computing (§28.2). F To.
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
2: Application Layer1 Socket programming Socket API Explicitly created, used, released by apps Client/server paradigm Two types of transport service via.
Socket Programming.
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.
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.
1 Kyung Hee University Chapter 11 User Datagram Protocol.
Socket programming in C. Socket programming Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm.
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.
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.
R Some of these slides are from Prof Frank Lin SJSU. r Minor modifications are made. 1.
SPL/2010 Reactor Design Pattern 1. SPL/2010 Overview ● blocking sockets - impact on server scalability. ● non-blocking IO in Java - java.niopackage ●
1 K. Salah Application Layer Module K. Salah Network layer duties.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 33 Networking.
Network Programming Communication between processes Many approaches:
SOCKET PROGRAMMING Presented By : Divya Sharma.
Socket Programming original by Joonbok Lee KAIST heavily adapted by /Jens.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit9: Internet programming 1.
Socket Programming Ameera Almasoud
Object-Orientated Analysis, Design and Programming
Chapter 9: Transport Layer
Networking COMP
Chapter 3 Internet Applications and Network Programming
MCA – 405 Elective –I (A) Java Programming & Technology
Beyond HTTP Up to this point we have been dealing with software tools that run on browsers and communicate to a server that generates files that can be.
Client-Server Interaction
UNIX Sockets Outline Homework #1 posted by end of day
Socket programming - Java
Process-to-Process Delivery:
CSCD 330 Network Programming
Starting TCP Connection – A High Level View
Socket Programming.
Internet Applications & Programming
Process-to-Process Delivery: UDP, TCP
TCP/IP Sockets in Java: Practical Guide for Programmers
Last Class: Communication in Distributed Systems
Exceptions and networking
Presentation transcript:

Lecture 11 Socket Programming

What is communication Communication = exchanging information. Network communication = exchanging bits between two processes, residing either on the same computer or on different computers connected by a network. General case of enabling two processes to communicate

Encoding send(&object, sizeof(object), destination), send sizeof(object) bytes from the memory used to store the state of object to destination

Encoding issues Byte order –processes communicating may reside on different computers and different architectures representation of unsigned int 1: 00 00 00 01 big endian: 01 00 00 00 MSB is stored at the higher address. little endian:00 00 00 01 MSB appears at the lowest address. Example: integer 1 on a little endian machine, copied byte by byte to a big endian machine, will be turned to 16777216. Convention: All information through the network is in network byte order (big endian)

Encoding issues RTE Types –processes might be running on RTE's (JVM(Java) vs C++(OS)), memory layout of objects in one RTE cannot be interpreted by the other. Compilers: same problem arises, as different compilers may represent different values differently.

Encoding - strings encode data into strings, and send strings over the network. debug-able! see what is sent and received Are strings represented uniformly? no! How to send binary data using strings? use string encoding of binary data (Base64)

UNICODE and UTF-8,16,32 UNICODE:  encoding schemes for correct exchange of strings across architectures portable way (independent of OS, RTEs, languages, compilers)

UTF-8,16,32 Most used encoding schemes are UTF-8, UTF-16 and UTF-32. Number after UTF specifies the width (how many bytes) of each character in the encoding scheme. UTF-32: each character takes exactly 4 bytes (2^32 different characters). UTF-8: characters represented by a single byte. Only 2^8 different characters. UTF-8 allows consecutive bytes to represent a single character. May be represented by 1, 2, 3 or 4 bytes

Course standard Negate the effect endianness - use UTF-8! Send: first encode the string using UTF-8, and then send the string. Receive: first decode data as a UTF-8 string and then convert to native representation

Client Server Architecture Server: a process that is accessible over the network. a computer that runs one or more servers is also called a server Client: a process that initiates a connection to a server. Host: any computer with a network presence – that is, connected to a network

Client Server Architecture Server: a process that is accessible over the network. a computer that runs one or more servers is also called a server Client: a process that initiates a connection to a server. Host: any computer with a network presence – that is, connected to a network IP Address: host uniquely identified by IP Address - 32 bit number, written in dot notation. Example: 132.72.50.21 Port: host may contain several servers, running side by side, we need some way to distinguish between these servers. A number, 0 - 65535. Socket: interface a process uses with the RTE related to communication. a connection's endpoint, which can be used by a process for sending or receiving information.

Client Server Architecture Clients wish to contact a server, on a different computer, and ask a service. Server is ”always-on”, listening for requests prior to the time a client initiates the communication. world wide web; client – web browser – contacts web server, asks for a web page. service is information inside the web page

Client Server Architecture

Network Communication Models Bi-directional? (phone call vs. a T.V. broadcast) Point-to-point? (two parties talking or a radio broadcast) Reliable? (everything sent reaches its destination or not?) Session-oriented? (phone call vs. a snail mail)

UDP – User Datagram Protocol Unreliable. Data may be lost or arrive in a different order than sent. Connectionless – no connection is made between client and server. Relatively fast. Mostly one-directional. Point to point scenario is possible but not necessarily. Suitable for Voice over IP (VoIP): We need a fast transmission. Lost data is not relevant.

UDP Line Printer Server

Observations buf – is just a byte buffer of size 1<<16 (2^16 which equals 64KB). Packet – is container for a collection of bytes + their size + an address. InetAddress – is container for an IP address.

Observations UDP socket are named DatagramSocket DatagramPacket – UDP packets. Binding – server creates a DatagramSocket, and binds this socket to the requested port. A process is a server if binding a socket.

Observations Do forever semantic – a server follows this loop: Receive a message –(sock.receive(packet)) saves the message into a packet. Calls to sock.receive(packet) blocks until the entire packet is received! Decode the message – Convert to string (packet.getData(), packet.getLength()), assuming the bytes received in the message were a UTF-8 encoded string. Service – here printing the string. Send reply – build a new packet encoding of the string "done", and send encoded string using the sock to the client. sock.send(packet) will block until the packet is sent.

A UDP Line Printer Client - in Java We use the same classes of DatagramSocket and DatagramPacket. the order of operations is inverse in the client: first call send (the client takes the initiative), then call receive (to wait for an answer).

Observations A client must know the address and port of the server (implemented here via the command line arguments). client initializes a Datagram socket, but does not bind! Arbitrary port number will be assigned to this socket by the operating system when the socket is used to send a packet. The client builds a new packet with the line to send, and fills it with the UTF-8 encoded message. After sending the datagram, the client waits for an answer (socket.receive()). receive will return any incoming packet. if you wish to listen for packets from a specific host, you should use connect() beforehand.

Line Printer Client in C++ Using communication means asking services from the RTE. OS API is not object oriented but functional. Low level - a lot of technical code Use Poco and Boost.

TCP - Transmission Control Protocol Reliable, session oriented, bi-directional communication protocol. Only point to point communication. Parties communicate by using a bi-directional data stream. TCP ensures correct transmission of data. Nothing gets lost.

TCP - Transmission Control Protocol Initiating a TCP connection requires the following: server opens a new server socket, binds the server socket to a port and waits for incoming connections. Client opens a new socket and connects this new socket to the server (as with UDP) Client's OS send a request for the server's operating system to initiate a new TCP connection. New TCP connection is uniquely identified by: <server address, server port, client address, client port>. Server gets a new, regular socket (client too) . socket contains an input and output stream.

TCP - Transmission Control Protocol When a TCP server accept(): a new, regular, TCP socket is created. A server socket is basically a socket factory, producing regular TCP sockets. (the new socket generated by the server socket does not use a new port number - OS can distinguish between different TCP streams by checking the 4-tuple we discussed above.)

A TCP Line Printer client in Java A client repeatedly sends lines to the server, which prints these lines. The client does not try to listen for replies from the server.

Observations UDP client we used a single message at a time (datagram, packets), TCP use streams. When send or receive from a stream, the call blocks. Wrap the socket output stream using an OutputStreamWriter, (set to use UTF-8 encoding). encode every string written to the OutputStreamWriter using UTF- 8, and send the resulting byte array to the OutputStream  As TCP is connection oriented. Everything sent through the stream will arrive in the correct order. We use out.flush() to force sending the string to the server. Otherwise, OutputStreamWriter can buffer data in bigger chunks

Try with resources The meaning: Every AutoCloseable object can be allocated in the try-with-resources section: try(Socket socket = new Socket(serverName, port);   BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));   BufferedReader in = new BufferedReader(new InputStreamReader(System.in))){ The meaning: The objects will be closed automatically at the end of the try section. No need to close. If there is and exception during the close(), the exception is handled in the exception catch statement of the “try”. If there is an exception during the try code, and it is caught, the objects will also be closed.

A TCP Line Printer Server in Java

Observations New type of socket called ServerSocket. This is only used for TCP servers. Server binds the socket with a port. Server process visible to the outside world. Accept() method of the ServerSocket is a blocking call. Returns each time a new connection is established.

Observations The accept method returns a new, regular, TCP socket server then starts a new Thread for each new connection, which will take care of this connection. The server then continues to wait for new connections. Each thread handling a connection is simply reading form the input stream until the connection is closed. thread first decode the bytes arriving into Java character assuming UTF-8. It associates a buffered reader with the character to yield a string that ends every time a line ends.

Blocking Sockets reads and writes to a socket, using regular input and output streams, is blocking. call read on input stream (in the server) or we call write on the output stream (in the client), call is suspended until required set of character has been sent or received. main obstacles we need to overcome in order to create scalable servers

A TCP Line Printer client in C++: POCO library abstraction over the OS socket

TCP Server Efficiency The TCP server we presented above is very VERY inefficient and not scalable. As the number of clients rises, the complexity of the server arises in a linear fashion; Each client requires a special, dedicated, thread to handle communication with the client, Next lectures: Reactor design pattern. Use just one thread to handle all connections by waiting for input from all of them concurrently.