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.