Group-A Assignment No. 6

Similar documents
Oral. Total. Dated Sign (2) (5) (3) (2)

STUDY OF SOCKET PROGRAMMING

Ports under 1024 are often considered special, and usually require special OS privileges to use.

Processes communicating. Network Communication. Sockets. Addressing processes 4/15/2013

Elementary TCP Sockets

Review. Preview. Closing a TCP Connection. Closing a TCP Connection. Port Numbers 11/27/2017. Packet Exchange for TCP Connection

Network Communication

Socket Programming. CSIS0234A Computer and Communication Networks. Socket Programming in C

Unix Network Programming

Socket Programming for TCP and UDP

A Socket Example. Haris Andrianakis & Angelos Stavrou George Mason University

Chapter 6. The Transport Layer. Transport Layer 3-1

Outline. Distributed Computing Systems. Socket Basics (1 of 2) Socket Basics (2 of 2) 3/28/2014

NETWORK PROGRAMMING. Instructor: Junaid Tariq, Lecturer, Department of Computer Science

UNIX Sockets. Developed for the Azera Group By: Joseph D. Fournier B.Sc.E.E., M.Sc.E.E.

sottotitolo Socket Programming Milano, XX mese 20XX A.A. 2016/17 Federico Reghenzani

Outline. Distributed Computer Systems. Socket Basics An end-point for a IP network connection. Ports. Sockets and the OS. Transport Layer.

Programming with TCP/IP. Ram Dantu

Tutorial on Socket Programming

TCP: Three-way handshake

EEC-484/584 Computer Networks

Outline. Operating Systems. Socket Basics An end-point for a IP network connection. Ports. Network Communication. Sockets and the OS

Programming Internet with Socket API. Hui Chen, Ph.D. Dept. of Engineering & Computer Science Virginia State University Petersburg, VA 23806

UNIT IV- SOCKETS Part A

Sockets 15H2. Inshik Song

Systems software design NETWORK COMMUNICATIONS & RPC SYSTEMS

Lecture 2. Outline. Layering and Protocols. Network Architecture. Layering and Protocols. Layering and Protocols. Chapter 1 - Foundation

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CSE 124 Discussion Section Sockets Programming 10/10/17

Unix Network Programming Chapter 4. Elementary TCP Sockets 광운대학교컴퓨터과학과 정보통신연구실 석사과정안중현

Hybrid of client-server and P2P. Pure P2P Architecture. App-layer Protocols. Communicating Processes. Transport Service Requirements

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

CS 640: Computer Networking

Socket Programming(2/2)

PA #2 Reviews. set_name, get_name, del_name. Questions? Will be modified after PA #4 ~

CSE/EE 461 Lecture 14. Connections. Last Time. This Time. We began on the Transport layer. Focus How do we send information reliably?

The User Datagram Protocol

Interprocess Communication Mechanisms

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

UDP CONNECT TO A SERVER

Lecture 3 Overview! Last Lecture! TCP/UDP and Sockets introduction!

Socket Programming TCP UDP

Context. Distributed Systems: Sockets Programming. Alberto Bosio, Associate Professor UM Microelectronic Departement

Socket Programming. Dr. -Ing. Abdalkarim Awad. Informatik 7 Rechnernetze und Kommunikationssysteme

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

The BSD UNIX Socket Interface (CS 640 Lecture) Assignment 1. Interprocess Communication (IPC) Work Individually (no groups)

Department of Computer Science

UNIX Sockets. COS 461 Precept 1

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

Lecture 5 Overview! Last Lecture! This Lecture! Next Lecture! I/O multiplexing! Source: Chapter 6 of Stevens book!

CPS 214: Computer Networks. Slides by Adolfo Rodriguez

Types (Protocols) Associated functions Styles We will look at using sockets in C Java sockets are conceptually quite similar

CS 3516: Computer Networks

Project 3. Reliable Data Transfer over UDP. NTU CSIE Computer Networks 2011 Spring

SOCKET PROGRAMMING. What is a socket? Using sockets Types (Protocols) Associated functions Styles

CLIENT-SIDE PROGRAMMING

Agenda. Before we start: Assignment #1. Routing in a wide area network. Protocols more concepts. Internetworking. Congestion control

Network Programming in C: The Berkeley Sockets API. Networked Systems 3 Laboratory Sessions

Socket Programming. #In the name of Allah. Computer Engineering Department Sharif University of Technology CE443- Computer Networks

CSE 333 Section 8 - Client-Side Networking

Memory-Mapped Files. generic interface: vaddr mmap(file descriptor,fileoffset,length) munmap(vaddr,length)

MSc Integrated Electronics Networks Assignment. Investigation of TCP/IP Sockets and Ports. Gavin Cameron

Introduction to Socket Programming

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Application Programming Interfaces

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

Communication. Sockets (Haviland Ch. 10)

Lecture 7. Followup. Review. Communication Interface. Socket Communication. Client-Server Model. Socket Programming January 28, 2005

Socket Programming. What is a socket? Using sockets. Types (Protocols) Associated functions Styles

CS321: Computer Networks Introduction to Application Layer

CSC209H Lecture 10. Dan Zingaro. March 18, 2015

Networked Applications: Sockets. End System: Computer on the Net

Introduction for SPI mapping Service Discovery Interoperability Testing. 20, Sep PWG Fumio Nagasaka

CS 428/528 Computer Networks Lecture 01. Yan Wang

What s an API? Do we need standardization?

Networked Applications: Sockets. Goals of Todayʼs Lecture. End System: Computer on the ʻNet. Client-server paradigm End systems Clients and servers

Session NM056. Programming TCP/IP with Sockets. Geoff Bryant Process software

CSMC 412. Computer Networks Prof. Ashok K Agrawala Ashok Agrawala Set 2. September 15 CMSC417 Set 2 1

CS 351 Week 15. Course Review

COMP/ELEC 429/556 Introduction to Computer Networks

WinSock. What Is Sockets What Is Windows Sockets What Are Its Benefits Architecture of Windows Sockets Network Application Mechanics

Announcements. CS 5565 Network Architecture and Protocols. Queuing. Demultiplexing. Demultiplexing Issues (1) Demultiplexing Issues (2)

Communication Networks ( ) / Spring 2011 The Blavatnik School of Computer Science, Tel-Aviv University. Allon Wagner

Topics for this Week

Networks. Practical Investigation of TCP/IP Ports and Sockets. Gavin Cameron

Network programming(ii) Lenuta Alboaie

Computer Networks Prof. Ashok K. Agrawala

A Client-Server Exchange

Computer Network Lab, SS Fachgebiet Technische Informatik, Joachim Zumbrägel. Overview. Sockets. Sockets in C.

Outline. Option Types. Socket Options SWE 545. Socket Options. Out-of-Band Data. Advanced Socket. Many socket options are Boolean flags

Interprocess Communication

Any of the descriptors in the set {1, 4} have an exception condition pending

Programming Ethernet with Socket API. Hui Chen, Ph.D. Dept. of Engineering & Computer Science Virginia State University Petersburg, VA 23806

A. Basic Function Calls for Network Communications

Redesde Computadores(RCOMP)

Communication Networks ( ) / Fall 2013 The Blavatnik School of Computer Science, Tel-Aviv University

Interprocess Communication Mechanisms

Interprocess Communication Mechanisms

CptS 360 (System Programming) Unit 17: Network IPC (Sockets)

Content 1. SDK Library Overview API Library Introduction API Data Structure HAL Driver Function Type LIB

Transcription:

Group-A Assignment No. 6 R N Oral Total Dated Sign (2) (5) (3) (10) Title : File Transfer using TCP Socket Problem Definition: Use Python for Socket Programming to connect two or more PCs to share a text file 1.1 Prerequisite: Syntax for Python Programming 1.2 Learning Objective: 1. To Understand how communication take Place between client and Server 2. How Data,Files are transferred in Bidirectional Communication 1.3 Theory: 1.3.1 Introduction: Definition of socket: A socket is one end-point of a two-way communication link between two programs running on the network. What is socket? Sockets allow communication between two different processes on the same or different machines. To a programmer a socket looks and behaves much like a low level file descriptor. This is because commands such as read() and write() work with sockets in the same way they do with files and pipes. The differences between sockets and normal file descriptors occur in the creation of a socket and through a variety of special operations to control a socket. Sockets were first introduced in 2.1 BSD and subsequently refined into their current form with 4.2BSD. The sockets feature is now available with most current UNIX system releases. 1.3.2 Brief About Sockets: A Socket is used in client server application frameworks. A server is a process which does some function on request from a client. Most of the application level protocols like FTP,

SMTP and POP3 make use of Sockets to establish connection between client and server and then for exchanging data. 1.3.3 Socket Types: There are four types of sockets available to the users. The first two are most commonly used and last two are rarely used. Processes are presumed to communicate only between sockets of the same type but there is no restriction that prevents communication between sockets of different types. Stream Sockets: Delivery in a networked environment is guaranteed. If you send through the stream socket three items "A,B,C", they will arrive in the same order - "A,B,C". These sockets use TCP (Transmission Control Protocol) for data transmission. If delivery is impossible, the sender receives an error indicator. Data records do no have any boundaries. Datagram Sockets: Delivery in a networked environment is not guaranteed. They're connectionless because you don't need to have an open connection as in Stream Sockets - you build a packet with the destination information and send it out. They use UDP (User Datagram Protocol). Raw Sockets: provides users access to the underlying communication protocols which support socket abstractions. These sockets are normally datagram oriented, though their exact characteristics are dependent on the interface provided by the protocol. Sequenced Packet Sockets: They are similar to a stream socket, with the exception that record boundaries are preserved. This interface is provided only as part of the Network Systems (NS) socket abstraction, and is very important in most serious NS applications. 1.3.4 UNIX Function for Socket Programming a. The socket Function: To perform network I/O, the first thing a process must do is call the socket function, specifying the type of communication protocol desired and protocol family etc.

int socket (int family, int type, int protocol); This call gives you a socket descriptor that you can use in later system calls or it gives you - 1 on error. Family: specifies the protocol family and is one of the constants shown below: Family Description AF_INET IPv4 protocols AF_INET6 IPv6 protocols AF_LOCAL Unix domain protocols AF_ROUTE Routing Sockets AF_KEY Ket socket This tutorial does not talk about other protocols except IPv4. Type: specifies kind of socket you want. It can take one of the following values: Type Description SOCK_STREAM Stream socket SOCK_DGRAM Datagram socket SOCK_SEQPACKET Sequenced packet socket SOCK_RAW Raw socket Protocol: argument should be set to the specific protocol type given below or 0 to select the system's default for the given combination of family and type: Protocol Description IPPROTO_TCP TCP transport protocol IPPROTO_UDP UDP transport protocol IPPROTO_SCTP SCTP transport protocol b. The connect Function: The connect function is used by a TCP client to establish a connection with a TCP server. int connect(int sockfd, struct sockaddr *serv_addr, int addrlen); This call returns 0 if it successfully connects to the server otherwise it gives you -1 on error.

serv_addr is a pointer to struct sockaddr that contains destination IP address and port. addrlen set it to sizeof(struct sockaddr). C. The bind Function: The bind function assigns a local protocol address to a socket.. This function is called by TCP server only. int bind(int sockfd, struct sockaddr *my_addr,int addrlen); This call returns 0 if it successfully binds to the address otherwise it gives you -1 on error. my_addr is a pointer to struct sockaddr that contains local IP address and port. addrlen set it to sizeof(struct sockaddr). D. The listen Function: The listen function is called only by a TCP server and it performs two actions: The listen function converts an unconnected socket into a passive socket, indicating that the kernel should accept incoming connection requests directed to this socket. The second argument to this function specifies the maximum number of connections the kernel should queue for this socket. int listen(int sockfd,int backlog); This call returns 0 on success otherwise it gives you -1 on error. backlog is the number of allowed connections. E. The accept Function:

The accept function is called by a TCP server to return the next completed connection from the front of the completed connection queue. Following is the signature of the call: int accept (int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen); This call returns non negative descriptor on success otherwise it gives you -1 on error. The returned descriptor is assumed to be a client socket descriptor and all read write operations will be done on this description to communicate with the client. cliaddr is a pointer to struct sockaddr that contains client IP address and port. addrlen set it to sizeof(struct sockaddr). F. The send Function: The send function is used to send data over stream sockets or CONNECTED datagram sockets. If you want to send data over UNCONNECTED datagram sockets you must use sendto() function. int send(int sockfd, const void *msg, int len, int flags); This call returns the number of bytes sent out otherwise it will return -1 on error. msg is a pointer to the data you want to send. len is the length of the data you want to send (in bytes). flags is set to 0. G.The recv Function: The recv function is used to receive data over stream sockets or CONNECTED datagram sockets. If you want to receive data over UNCONNECTED datagram sockets you must use recvfrom(). int recv(int sockfd, void *buf, int len, unsigned int flags);

This call returns the number of bytes read into the buffer otherwise it will return -1 on error. buf is the buffer to read the information into. len is the maximum length of the buffer. flags is set to 0. H. The sendto Function: The sendto function is used to send data over UNCONNECTED datagram sockets. Put simply, when you use scoket type as SOCK_DGRAM int sendto(int sockfd, const void *msg, int len, unsigned int flags, const struct sockaddr *to, int tolen); This call returns the number of bytes sent otherwise it will return -1 on error. msg is a pointer to the data you want to send. len is the length of the data you want to send (in bytes). flags is set to 0. to is a pointer to struct sockaddr for the host where data has to be sent. tolen is set it to sizeof(struct sockaddr). I. The recvfrom Function: The recvfrom function is used to receive data from UNCONNECTED datagram sockets. Put simply, when you use scoket type as SOCK_DGRAM int recvfrom(int sockfd, void *buf, int len, unsigned int flags struct sockaddr *from, int *fromlen); This call returns the number of bytes read into the buffer otherwise it will return -1 on error.

buf is the buffer to read the information into. len is the maximum length of the buffer. flags is set to 0. from is a pointer to struct sockaddr for the host where data has to be read. fromlen is set it to sizeof(struct sockaddr). J. The close Function: The close function is used to close the communication between client and server. int close( int sockfd ); This call returns 0 on success otherwise it will return -1 on error. K. The shutdown Function: The shutdown function is used to gracefully close the communication between client and server. This function gives more control in comparison of close function. int shutdown(int sockfd, int how); This call returns 0 on success otherwise it will return -1 on error. how: put one of the numbers: o o o 0 indicates receives disallowed, 1 indicatesthat sends disallowed and 2 indicates that sends and receives disallowed. When how is set to 2, it's the same thing as close().

L.The select Function: The select function indicates which of the specified file descriptors is ready for reading, ready for writing, or has an error condition pending. int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *timeout); This call returns 0 on success otherwise it will return -1 on error. nfds: specifies the range of file descriptors to be tested. The select() function tests file descriptors in the range of 0 to nfds-1 readfds:points to an object of type fd_set that on input specifies the file descriptors to be checked for being ready to read, and on output indicates which file descriptors are ready to read. Can be NULL to indicate an empty set. writefds:points to an object of type fd_set that on input specifies the file descriptors to be checked for being ready to write, and on output indicates which file descriptors are ready to write Can be NULL to indicate an empty set. exceptfds :points to an object of type fd_set that on input specifies the file descriptors to be checked for error conditions pending, and on output indicates which file descriptors have error conditions pending. Can be NULL to indicate an empty set. timeout :poins to a timeval struct that specifies how long the select call should poll the descriptors for an available I/O operation. If the timeout value is 0, then select will return immediately. If the timeout argument is NULL, then select will block until at least one file/socket handle is ready for an available I/O operation. Otherwise select will return after the amount of time in the timeout has elapsed OR when at least one file/socket descriptor is ready for an I/O operation.

The return value from select is the number of handles specified in the file descriptor sets that are ready for I/O. If the time limit specified by the timeout field is reached, select return 0. The following macros exist for manipulating a file descriptor set: FD_CLR(fd, &fdset): Clears the bit for the file descriptor fd in the file descriptor set fdset FD_ISSET(fd, &fdset): Returns a non-zero value if the bit for the file descriptor fd is set in the file descriptor set pointed to by fdset, and 0 otherwise. FD_SET(fd, &fdset): Sets the bit for the file descriptor fd in the file descriptor set fdset. FD_ZERO(&fdset): Initializes the file descriptor set fdset to have zero bits for all file descriptors. The behavior of these macros is undefined if the fd argument is less than 0 or greater than or equal to FD_SETSIZE. 1.3.5 Python File Handling functions a. Reading Keyboard Input: Python provides two built-in functions to read a line of text from standard input, which by default comes from the keyboard. These functions are: raw_input input b. The raw_input Function: The raw_input([prompt]) function reads one line from standard input and returns it as a string (removing the trailing newline). str = raw_input("enter your input: "); print "Received input is : ", str c. The open Function: This function creates a file object, which would be utilized to call other support methods associated with it. file object = open(file_name [, access_mode][, buffering])

Here is paramters' detail: file_name: The file_name argument is a string value that contains the name of the file that you want to access. access_mode: The access_mode determines the mode in which the file has to be opened, i.e., read, write, append, etc. A complete list of possible values is given below in the table. This is optional parameter and the default file access mode is read (r). buffering: If the buffering value is set to 0, no buffering will take place. If the buffering value is 1, line buffering will be performed while accessing a file. If you specify the buffering value as an integer greater than 1, then buffering action will be performed with the indicated buffer size. If negative, the buffer size is the system default(default behavior). Here is a list of the different modes of opening a file: Modes Description r Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode. rb Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode. r+ Opens a file for both reading and writing. The file pointer will be at the beginning of the file. rb+ Opens a file for both reading and writing in binary format. The file pointer will be at the beginning of the file. w Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. wb Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. w+ Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing. wb+ Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing. a Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.

ab Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing. a+ Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing. ab+ Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing. d. The file object attributes: Once a file is opened and you have one file object, you can get various information related to that file. Here is a list of all attributes related to file object: Attribute Description file.closed Returns true if file is closed, false otherwise. file.mode Returns access mode with which file was opened. file.name Returns name of the file. file.softspace Returns false if space explicitly required with print, true otherwise. e. The close() Method: The close() method of a file object flushes any unwritten information and closes the file object, after which no more writing can be done. Python automatically closes a file when the reference object of a file is reassigned to another file. It is a good practice to use the close() method to close a file. fileobject.close(); f. The write() Method: The write() method writes any string to an open file. The write() method does not add a newline character ('\n') to the end of the string: fileobject.write(string); Here, passed parameter is the content to be written into the opened file.

g. The read() Method: The read() method reads a string from an open file. It is important to note that Python strings can have binary data and not just text. fileobject.read([count]); 1.3.6 Operating System Function a. os.listdir() Method The method listdir() returns a list containing the names of the entries in the directory given by path. The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the directory. Syntax os.listdir(path) Parameters path -- This is the directory, which needs to be explored. Return Value This method returns a list containing the names of the entries in the directory given by path. Assignment Question: 1. Difference between UDP and TCP Sockets. 2. Write down algorithm for transfer file between two PCs using TCP Communication.