Lector: Aliyev H.U. Lecture №5 Telecommunication network software design with.NET. Using streams for network programming TASHKENT UNIVERSITY OF INFORMATION.

Slides:



Advertisements
Similar presentations
Reading and Writing Text Files Svetlin Nakov Telerik Corporation
Advertisements

Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
Formal Language, chapter 4, slide 1Copyright © 2007 by Adam Webber Chapter Four: DFA Applications.
Serial I/O - Programmable Communication Interface
Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
Avishai Wool lecture Introduction to Systems Programming Lecture 8 Input-Output.
Files & Streams. Files Introduction Files are used for long-term retention of large amounts of data, even after the program that created the data terminates.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Guide To UNIX Using Linux Third Edition
ASP.NET Programming with C# and SQL Server First Edition
File and Streams There are many ways of organizing records in a file. There are many ways of organizing records in a file. The most common type of organization.
MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition.
Fundamentals of Python: From First Programs Through Data Structures
Understanding Input/Output (I/O) Classes Lesson 5.
Windows Programming Using C# Windows Services, Serialization, and Isolated Storage.
331: STUDY DATA COMMUNICATIONS AND NETWORKS.  1. Discuss computer networks (5 hrs)  2. Discuss data communications (15 hrs)
1 Input/Output. 2 Principles of I/O Hardware Some typical device, network, and data base rates.
Streams and File I/O Chapter 14. I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or.
Lecture Set 12 Sequential Files and Structures Part B – Reading and Writing Sequential Files.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 8-1: I/O Management Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
Segmentation & O/S Input/Output Chapter 4 & 5 Tuesday, April 3, 2007.
Lesson №12 Telecommunication network software design for HTTP and Internet Lector: Aliev H.U. TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES THE DEPARTMENT.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Lector: Aliyev H.U. Lecture №6 Design of client-server communication software. TCP-based network programming TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
Cis303a_chapt03-2a.ppt Range Overflow Fixed length of bits to hold numeric data Can hold a maximum positive number (unsigned) X X X X X X X X X X X X X.
Internet Applications and Network Programming Dr. Abraham Professor UTPA.
 2006 Pearson Education, Inc. All rights reserved Files and Streams.
(More) Interfacing concepts. Introduction Overview of I/O operations Programmed I/O – Standard I/O – Memory Mapped I/O Device synchronization Readings:
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
SE-1020 Dr. Mark L. Hornick 1 File Input and Output.
Files and Streams File Types, Using Streams, Manipulating Files SoftUni Team Technical Trainers Software University
Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.
Machine data - All data in computers based on digital electronics Boolean data - The Boolean type represents the values: true and false. Numeric data.
Introduction to Exception Handling and Defensive Programming.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 RMI.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
Operating Systems COMP 4850/CISG 5550 File Systems Files Dr. James Money.
Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Lector: Aliyev H.U. Lecture №10 Multicast network software design TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES THE DEPARTMENT OF DATA COMMUNICATION.
UDP Client-Server. The Socket Class The Socket class provides a set of methods and properties for network communications. The Socket class allows you.
Object Oriented Software Development 10. Persistent Storage.
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the file.
CE Operating Systems Lecture 13 Linux/Unix interprocess communication.
CS360 Windows Programming
Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
CMSC 202 Text File I/O. Aug 8, Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with.
Object Serialization.  When the data was output to disk, certain information was lost, such as the type of each value.  If the value "3" is read from.
Chapter 5 Input/Output 5.1 Principles of I/O hardware
CSC 298 Streams and files.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the file.
Files and Streams Lec Introduction  Files are used for long-term retention of large amounts of data, even after the program that created the.
( ) 1 Chapter # 8 How Data is stored DATABASE.
Defiana Arnaldy, M.Si File and Stream I/O Defiana Arnaldy, M.Si
I/O Basics.
CS703 - Advanced Operating Systems
DIGITAL DATA COMMUNICATION TECHNIQUES
Files and Streams Lect3 CT1411.
Chapter 17 Binary I/O Dr. Clincy - Lecture.
Operating Systems Chapter 5: Input/Output Management
Files & Streams.
Files and Streams Lect10 GC201 12/1/2015.
Introduction to Data Structure
CMSC 202 Exceptions.
Exceptions and networking
Presentation transcript:

Lector: Aliyev H.U. Lecture №5 Telecommunication network software design with.NET. Using streams for network programming TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES THE DEPARTMENT OF DATA COMMUNICATION NETWORKS AND SYSTEMS

Streams in.NET networking A stream is an abstract representation of a serial device for storing and retrieving data one byte at a time- the underlying device can be a file, a printer, or a network socket for example. Through this abstraction, different devices can be accessed with the same process, and similar code can be used to read data from a file input stream as can be used to read data from a network input stream for example. Furthermore, the programmer's need to worry about the actual physical mechanism of the device is removed. In this chapter we'll look at the following: Streams in.NET The Stream class and its members The FileStream class and other Stream-derived classes Reading to and writing from binary and text files Serialization

Streams in.NET The.NET Framework provides a rich set of classes for performing operations on various types of streams. Stream is the main class, an abstract class from which all other stream- related classes derive. Since a stream is an abstraction of data as a sequence of bytes, to manipulate these sequences of bytes you have to perform some basic operations such as reading, writing, or seeking. With the Stream class you can perform binary I/O operations on a stream. With TextReader and TextWriter you can perform character I/O operations, whereas with BinaryReader and BinaryWriter you can perform I/O operations on primitive types.

Synchronous and Asynchronous I/O There are two ways to perform operations on a stream, synchronous or asynchronous-your choice will depend upon the requirements of your application. As we will see in a moment, the Stream class provides methods for both synchronous and asynchronous operations, but first let's discuss some of the advantages and disadvantages of each type of operation. Synchronous I/O By default, all operations on streams are performed synchronously-this is the simplest way to perform I/O operations. The disadvantage of synchronous I/O is that it blocks processing until the I/O operation is complete-then the application is allowed to continue processing. Asynchronous I/O In asynchronous I/O, other tasks can be performed while the I/O operation is being completed. When the I/O operation completes, the operating system notifies the caller. Therefore, a separate notification mechanism is required for asynchronous I/O. This method is useful when an application needs to continue performing other tasks while processing large amounts of data from a stream, or needs to work with slow devices whose rate of access would otherwise slow down the application.

The Stream Class The Stream class in the System.IO namespace is the base class for all the other stream classes, and provides the functionality for performing fundamental operations on stream. If you understand the functionality of the Stream class, then you can easily understand the other derived classes too-the key thing is to learn how to create the different types of stream with each class. In short, all the Stream- derived classes represent various types of stream with common or derived methods from the Stream class, and some extra methods for performing operations on that specific type of stream. The diagram below illustrates Stream, the derived classes, and various other classes provided for performing operations on stream.

The Stream Class

The main classes derived from Stream ClassPurpose FileStreamUses files as backing storage. The most widely used Stream class. BufferedStreamUses a buffer as backing storage. Used as intermediate storage for improving performance. MemoryStreamUses memory as backing storage and performs faster I/O operations when compared to other streams. NetworkStreamDoes not have any backing storage. Used with other streams for transferring data across a network. CryptoStreamCryptoStream is used with other stream classes for performing encryption-decryption on streams.

The NetworkStream Class Across a network, the data transferred between locations is in the form of a continuous flow or stream. For handling such streams,.NET has a special class-NetworkStream in the System.Net.Sockets namespace, which is used for sending and receiving data through network sockets. NetworkStream is an unbuffered stream and does not support random access to data-you cannot change the position within the stream and therefore the use of Seek() and Position throws an exception. The CanSeek property always returns false for a NetworkStream object. As NetworkStream is unbuffered, BufferedStream is usually used along with this class as an intermediate storage me

The important members of NetworkStream PropertyDescription DataAvailableReturns a Boolean value indicating whether data is available in the stream for reading or not. A value of true indicates that data is available in the stream. ReadableUsed to get or set a Boolean value indicating whether read access is given to the stream or not. This property works in the same way as the CanRead property in other streams. SocketReturns the underlying Socket. WriteableUsed for checking whether the stream can be written to or not. A value of true indicates that the stream is writeable. This property works in the same way as the CanWrite property in other streams.

The NetworkStream Class Each NetworkStream constructor requires at least a Socket-in addition you can specify a Boolean value indicating ownership of a stream and/or a value from the FileAccess enumeration we saw earlier to control read and write permissions. Setting the ownership flag to true gives control of the socket to the NetworkStream object, and by using the Close() method you can close the underlying socket. A NetworkStream can also be retrieved from a TcpClient-we'll spend a whole lesson on TCP, but we'll make use of it here to illustrate NetworkStream in a client-server scenario. The TcpClient.Get Stream() method creates a NetworkStream object, passing in its underlying Socket as the constructor parameter.

The code for the TCP client with NS using System; using System.IO; using System.Text; using System.Net; using System.Net.Sockets; class TcpClient Example { static void Main(string[] args) { try { We create our TcpClient, and connect to the localhost on port Once again, we use the GetStream() method to return the underlying NetworkStream:

The code for the TCP client with NS // Create TCP Client TcpClient client = new TcpClient(); //Connect using hostname and port client.Connect ("localhost", 5001); //Get NetworkStream instance for sending data NetworkStream stm = client.GetStream(); Now we have our NetworkStream, sending the data is the same process as we have used with the other streams:

The code for the TCP client with NS byte[] sendBytes = Encoding.ASCII.GetBytes("This data has come from" + " another place!!!"); stm.Write (sendBytes, 0, sendBytes.Length); Finally, our TcpClient is closed: client.Close(); } catch (Exception e) { Console.WriteLine(e.ToString()); Console.WriteLine("The listener has probably not started"); }

Stream Manipulation We have looked at the different types of streams, how to create them, and how to read to and write from them. However, we've only been able to read and write byte arrays-this is a somewhat cumbersome way of reading data. For example, if we wanted to write some decimal values to a file, we'd have to break these down into bytes ourselves-there must be an easier way! Sure enough, the System.IO namespace provides classes and methods for manipulating different data types in streams. We'll look at the following classes for stream manipulation in this section: Manipulating binary files with BinaryReader and BinaryWriter Manipulating text files with StreamReader and StreamWriter Before we look at these classes, we need to consider encoding-this is something that we've alluded to earlier in the chapter when we were converting to and from byte arrays for transferring data from streams.

Encoding String Data The Encoding class in the System.Text namespace is provided for performing such operations. The Encoding class handles sets of Unicode characters. Unicode is a worldwide character- encoding standard, which allows universal data exchange and improves multilingual text processing. Many languages cannot be represented without Unicode, such as Japanese. The wider range of characters supported by the Unicode format means that Unicode information typically requires 16-bit space instead of the standard 8-bit character strings.

Encoding classes The.NET Framework has several classes derived from the Encoding class for performing encoding between different formats. ClassUse ASCIIEncodingEncodes Unicode characters as single 1-byte ASCII characters. This encoding has limitations because it supports 7-bit character values and is not a good choice for applications that support multilingual text processing. UnicodeEncodingEncodes each Unicode character in 2 bytes. UTF7EncodingEncodes in 7-bit Unicode encoding. UTF (Unicode Translation Format) 7 and 8 are commonly used formats to send Unicode-based data across networks. UTF8EncodingThis class supports 8-bit UTF-8 encoding. This encoding is widely used with applications that support multilingual text processing. XML uses UTF-8 encoding by default.

Serialization Serialization is the process of taking objects and converting their state information into a form that can be stored or transported. The stored or transported object can then be deserialized to recreate the original state of the object. Thus you can serialize an object, transmit this information across a network and then restore the object and its original state from another application or system. Here are some key areas where serialization is a benefit: Availability-a component can be saved in a file and can be made available whenever required. Lifetime-saving the object with its state increases its life. In normal practice, when you close an application all associated objects are destroyed automatically. Use within networked applications-the complex form of the object has been transformed to a format that is suitable for transferring across a network, and possibly through firewalls. Reliability-the saved object can be recreated 'as-it-is'. In this section we'll be looking at two ways of serializing objects: Serializing into XML format Serializing with formatter objects into binary

Q&A?