Lecture 10 Networking.

Slides:



Advertisements
Similar presentations
Taekyung Kim 0x410 ~ 0x International Standards Organization (ISO) is a multinational body dedicated to worldwide agreement on international.
Advertisements

© Janice Regan, CMPT 128, CMPT 371 Data Communications and Networking Socket Programming 0.
COEN 445 Communication Networks and Protocols Lab 4
Data Communications and Networking (Third Edition)
Network Programming in Python Modified from Steve Holden Dan Rubenstein DK Moon Mehmet Hadi Gunes.
Socket Programming.
1 Java Networking – Part I CS , Spring 2008/9.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
CSE/EE 461 Getting Started with Networking. Basic Concepts  A PROCESS is an executing program somewhere.  Eg, “./a.out”  A MESSAGE contains information.
Client Server Model The client machine (or the client process) makes the request for some resource or service, and the server machine (the server process)
Networks and Client/Server Applications. Basics of Client/Server One host computer can have several servers Several clients can connect to a server Client.
Fundamentals of Python: From First Programs Through Data Structures
Socket Programming -What is it ? -Why bother ?. Basic Interface for programming networks at transport level It is communication end point Used for inter.
Assignment 3 A Client/Server Application: Chatroom.
Elementary TCP Sockets
 Socket  The combination of an IP address and a port number. (RFC 793 original TCP specification)  The name of the Berkeley-derived application programming.
HOW WEB SERVER WORKS? By- PUSHPENDU MONDAL RAJAT CHAUHAN RAHUL YADAV RANJIT MEENA RAHUL TYAGI.
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.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Internet Applications and Network Programming Dr. Abraham Professor UTPA.

Introduction to Network Programming with Sockets Network Programming Kansas State University at Salina.
Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming.
The Socket Interface Chapter 21. Application Program Interface (API) Interface used between application programs and TCP/IP protocols Interface used between.
CS 158A1 1.4 Implementing Network Software Phenomenal success of the Internet: – Computer # connected doubled every year since 1981, now approaching 200.
CSE/EE 461 Getting Started with Networking. 2 Basic Concepts A PROCESS is an executing program somewhere. –Eg, “./a.out” A MESSAGE contains information.
Socket Programming Lab 1 1CS Computer Networks.
Socket Programming Introduction. Socket Definition A network socket is one endpoint in a two-way communication flow between two programs running over.
Socket Programming.
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Networking A network represents interconnection of computers that is capable.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
LECTURE 10 Networking. NETWORKING IN PYTHON Many Python applications include networking – the ability to communicate between multiple machines. We are.
LECTURE 11 Networking Part 2. NETWORKING IN PYTHON At this point, we know how to write a simple TCP client and simple TCP server using Python’s raw sockets.
LECTURE 2 Python Basics. MODULES So, we just put together our first real Python program. Let’s say we store this program in a file called fib.py. We have.
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.
1 K. Salah Application Layer Module K. Salah Network layer duties.
Network Programming. These days almost all devices.
Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 33 Networking.
SOCKET PROGRAMMING Presented By : Divya Sharma.
Socket Programming original by Joonbok Lee KAIST heavily adapted by /Jens.
Chapter 9: Transport Layer
Echo Networking COMP
Lecture 11 Networking Part 2.
Instructor Materials Chapter 9: Transport Layer
CSCE 313 Network Socket MP8 DUE: FRI MAY 5, 2017
Boots Cassel Villanova University
Sockets and Beginning Network Programming
Lecture 2 Python Basics.
MCA – 405 Elective –I (A) Java Programming & Technology
Networks and Client/Server Applications
TCP/IP Networking An Example
Client/Server Example
Transport layer API: Socket Programming
Topic 5: Communication and the Internet
The Internet and HTTP and DNS Examples
UNIX Sockets Outline Homework #1 posted by end of day
Process-to-Process Delivery:
27.
Starting TCP Connection – A High Level View
Network Programming in Python
Server-side Programming CSE 333 Summer 2018
Chapter 3 Socket API © Bobby Hoggard, Department of Computer Science, East Carolina University These slides may not be used or duplicated without permission.
Internet Applications & Programming
Python Network Programming
Exceptions and networking
Presentation transcript:

Lecture 10 Networking

Networking in python Many Python applications include networking – the ability to communicate between multiple machines. We are going to turn our attention now to the many methods of network programming available in Python. Sockets SocketServer Twisted But first, we must start with some networking fundamentals.

Networking fundamentals In the client-server model, the client sends out a request to a server. The server processes the request and sends a response back to the client. The classic example is a web browser sending a request for a webpage to a webserver. The webserver processes the request and returns the webpage to the browser. client client server client

Networking fundamentals The coordination of the communication process is defined by the protocol being used. For example, the web browser and webserver communication is defined by the TCP/IP communication protocol. This protocol defines how data should be packetized, addressed, transmitted, routed, and received over the Internet. client client server client

Networking fundamentals A socket is simply an endpoint of communication. They provide an abstraction for the details of communication. An application can interface with a socket object, which hides the details of the lower level network protocols. Python has a socket module which provides access to the BSD socket interface. We will start with this simple, low-level way of creating a networked application. client server

The socket module socket.socket([family, [type]]) creates a socket object. Remember, this is one endpoint of a two-way communication link. The family argument is the address family. The default is socket.AF_INET, which tells the socket to support the IPv4 protocol (32-bit IP addresses). Other choices include: socket.AF_INET6 for IPv6 protocol (128-bit IP address). socket.AF_UNIX for Unix Domain Sockets. The type argument is the type of socket. socket.SOCK_STREAM for connection-oriented sockets (TCP). socket.SOCK_DGRAM for datagram sockets (UDP). In this class, we will only deal with TCP/IPv4 sockets. These are the default arguments.

The socket module So, let’s create our first socket. Now we have an IPv4 TCP socket which we can use to connect to other machines. from socket import * s = socket(AF_INET, SOCK_STREAM) # or socket()

The socket module We’ve just created a socket object, which has a selection of standard methods available. s.connect(addr) creates a connection between the socket s and the specified address addr. The addr argument is a tuple containing the host name (as a string) and port number (as an int). s.send(string) sends a string to the address to which the socket is currently connected. The return value is the number of bytes sent. There is no guarantee that the whole message was sent – you should double check the return value!

The socket module The following code connects to the www.fsu.edu server and requests the page index.html. This is essentially what happens behind the scenes when you enter the address www.fsu.edu/index.html into your browser. The port number 80 is standard for http requests and the string we constructed is just a simple http GET request. from socket import * s = socket() s.connect(("www.fsu.edu", 80)) s.send("GET /index.html HTTP/1.0\n\n")

The socket module s.recv(bufsize) receives and returns up to bufsize bytes of data from the address to which the socket is currently connected. s.close() closes the connection. Sockets are also automatically closed when garbage collected. The connect(), send(), recv() and close() functions are all we need to write a simple TCP client. Clients simply create connections as necessary, send and receive data, and then close when they are finished with a transaction. Your browser is a client program – it connects to a server when it needs to and loses the connection when it’s done.

The socket module Now, we’re not only sending a page request, but we receive 2048 bytes of data (in reality – the page is much bigger) and close the connection. The contents of data is simply 2048 bytes of the html of the page we requested. from socket import * s = socket() s.connect(("www.fsu.edu", 80)) s.send("GET /index.html HTTP/1.0\n\n") data = s.recv(2048) s.close() print data

The socket module To create a server, we can also use socket objects. We will just treat them as if they’re the other endpoint of the connection. s.bind(addr) binds the socket object to an address addr. As before, addr is a tuple containing the hostname and port. s.listen(backlog) tells the socket to begin listening for connections. The backlog argument specifies how many connections to queue before connections become refused (default is zero). socket.gethostname() returns a string containing the local machine’s primary public hostname.

The socket module Here we are creating a simple TCP server which is listening for connections on port 9000. If our server is already connected to another machine, we will queue up to 5 connections before we start refusing connections. from socket import * s = socket() h = socket.gethostname() s.bind((h, 9000)) s.listen(5)

The socket module Here we are creating a simple TCP server which is listening for connections on port 9000. If our server is already connected to another machine, we will queue up to 5 connections before we start refusing connections. from socket import * s = socket() h = socket.gethostname() s.bind((h, 9000)) s.listen(5) We listen on a relatively high port number like 9000 because it is not reserved. Well-known services (HTTP, SFTP, etc) should listen on conventional port numbers. We could also bind to localhost: s.bind(("localhost", 9000)) s.bind(("127.0.0.1", 9000)) Or bind to any address the machine has: s.bind(("", 9000))

The socket module s.accept() passively waits until a connection is made. The return value is a pair (conn, address) where conn is a new socket object usable to send and receive data on the connection, and address is the address bound to the socket on the other end of the connection.

The socket module Here we’re creating a simple TCP echo server. The empty string argument to bind signifies that we’re listening for connections to every hostname the machine happens to have. Notice that we use a new, unique socket object c to communicate with the client. We have a major practical problem however. What do you think it is? from socket import * s = socket(AF_INET, SOCK_STREAM) s.bind(("",9000)) s.listen(5) while True: c,a = s.accept() print "Received connection from", a c.send("Hello " + str(a[0])) c.close()

The socket module Clients Servers Some useful utility functions: Connect when they need and close after a transaction. connect(), send() and recv(), close() Servers Listen for connections and communicate with each client using a new, unique socket object. bind(), listen(), loop: accept(), send() and recv(), close() Some useful utility functions: socket.gethostbyname("www.python.org")  returns IP socket.gethostbyaddr("82.94.237.218")  returns name

The socket module Sending and receiving data is often done in stages – these functions are bound to the capacity of the network buffers. s.send(string) returns the number of bytes sent. s.recv(max) may receive fewer bytes than the specified max. Returns an empty string when the connection is closed. s.sendall(string) blocks until all data has been transmitted. None is returned on success.

The socket module There are some additional options you can specify. s.setblocking(flag) uses flag to determine whether socket operations should be blocking or not. By default, all socket operations are blocking because they will wait for something to happen. For example, recv() will block until some data comes in. s.settimeout(t) sets a timeout of t seconds on all blocking socket operations. If t > 0, socket operations that exceed their time will raise a timeout exception. If t is None, the socket operation never times out. s.makefile() returns a file object which can be used to read and write to the socket object as a file object. VERY useful!

The socket module So, we’ve already mentioned that there’s an issue with this code. Practically speaking, this isn’t a good server because it can only handle one connection at a time. Incoming connections must wait for the previous connection request to be serviced before they are accepted. They might get refused outright, which is even worse. from socket import * s = socket(AF_INET, SOCK_STREAM) s.bind(("",9000)) s.listen(5) while True: c,a = s.accept() print "Received connection from", a c.send("Hello " + str(a[0])) c.close()

The threading module To solve this one-connection-at-a-time issue, we’ll introduce some concurrency tools. The Python standard library includes a nice threading module which allows us to implement threading – running multiple operations concurrently within the same program space. Simplest usage: Creates a thread t which will execute worker_function with arguments arg1 and arg2 when the thread is started. import threading t = threading.thread(target=worker_function, args=(arg1,arg2))

Threaded tcp server import threading from socket import * def handle_client(c): ... whatever ... c.close() s = socket(AF_INET,SOCK_STREAM) s.bind(("",9000)) s.listen(5) while True: c,a = s.accept() t = threading.Thread(target=handle_client, args=(c,)) t.start() This threaded version of a our simple TCP server can handle multiple connections at once. As soon as a connection is accepted, the socket object is passed to a handling function in another thread. This allows the main thread to continue accepting new connections even when the first connection is still being served.

Blackjack_server.py Let’s create a threaded TCP server which allows a client to play Blackjack. Our little Blackjack game using the following simple rules: Dealer and Player both start with two cards in their hand. One of the dealer’s cards is hidden, the other is visible. The Player must try to add cards to their hand until they are as close to a combined value of 21 without going over. This is done by successively “hitting” until they are satisfied with their hand. Afterwards, the Dealer must add cards to his hand while the combined value is less than 17. The winner is the one whose cards have a combined total closest to 21 without going over.

TELNET tool To test out your server applications, you do not need to build a client application to interface with it. Instead, use the telnet tool. $ telnet hostname port will connect you to the server listening on hostname:port and you can interact with it as if you were a client application. For example, $ telnet localhost 9000 will allow you to connect and send information to a server listening locally on port 9000.