Introduction to Network Programming Asst. Prof. Chaiporn Jaikaeo, Ph.D. chaiporn.j@ku.ac.th http://www.cpe.ku.ac.th/~cpj Computer Engineering Department Kasetsart University, Bangkok, Thailand
Outline End-to-end communication Direct serial communication Internet communication Simple client-server applications 2
End-to-End Communication Direct communication Communicating devices are directly connected TX TX RX RX GND GND Internet communication Communication is performed over the Internet Internet 3
Client-Server Paradigm Client Server Client Initiates communication Issues command Server Awaits and respond to commands Most common model for Internet applications 4
Our Simple Protocol command response Client Server Request Issued by Response by Server Client Your student ID, followed by \r\n id\r\n Your name, followed by \r\n name\r\n Server responds with "ERROR" when receiving an unknown command. 5
Hands-on Activity 1: Serial Comm. Most basic form of device-to-device communication However, most recent computers do not come with a serial port Use USB-Serial dongle instead TX TX RX RX GND GND USB- USB- Serial Serial Dongle Dongle 6
Python's Serial API Opening a serial port from serial import Serial ser = Serial("/dev/ttyUSB0") Sending data with newline characters ser.write('Hello\r\n') Receiving data (blocking call) Wait until new line characters are received and return the whole line ser.readline() Wait until 100 bytes are received ser.read(100) 7
Internet Comm. - App's Viewpoint Two network applications should interact as if they were directly connected write read App App A B Internet 8
Internet Comm. – Socket API API for developing applications that perform inter- process communication most commonly for communications across a computer network Example functions listen – used by server to wait for contact from client connect – used by client to contact server send – used by either client or server to send data recv – used by either client or server to receive data close – close the connection 9 9
Services Provided by Socket API Connection-oriented, stream-like service Provides virtual stream-oriented pipe Data transfer is reliable No loss, in-order arrival App App A B Internet Both machines use Transmission Control Protocol (TCP) to transfer data 10
Services Provided by Socket API Connectionless, datagram service User must prepare packet of data before sending Data transfer is NOT reliable Loss possible, out-of-order arrival possible App App A B Internet Both machines use User-Datagram Protocol (UDP) to transfer data 11
Port Addressing IP addresses are used to identify hosts (i.e., machines) on the Internet Port numbers are used to distinguish different processes running on the same host Proc4 Ports Proc3 Proc2 Proc1 Proc5 A B Internet IP Address 1 IP Address 2 12
Hands-on Activity 2: Internet Comm. Implement Activity 1 over the Internet using stream-oriented service
TCP Socket: Flow in Python Client Server bind() listen() connect() accept() receive() send() send() recv() close() close() 14
Server: Creating Socket Create a socket object and bind it to port 12345 on any available network interface Listen on any from socket import * Port 12345 interface listen_sock = socket() listen_sock.bind(('0.0.0.0',12345)) listen_sock.listen(1) # max. of 1 client can wait sock,info = listen_sock.accept() 15
Client: Create Socket On another machine, create a client socket and connect to the server's IP and port from socket import * sock = socket() server = (" <server's IP> ", 12345) sock.connect(server) 16
Finding Out Your IP Address Windows – Run ipconfig from a command prompt MacOS/Linux/Unix – Run ifconfig from a terminal 17
Server: Check Client's IP and Port The method getpeername() tells us about client's IP address and port number The method returns a tuple (ip-addr, port) addr,port = sock.getpeername() print "Client is connected from",addr 18
Transferring Data Use send() and recv() methods to send and receive data, respectively E.g., specify the max number of At the server, enter the command bytes to be received msg = sock.recv(1024) The server will block until it receives data At the client, enter the command sock.send('hello') 19
Closing Connection Server: sock.close() listen_sock.close() Client: sock.close() 20
Assignment: Throw me your name I will be running a server on my machine For each of you Create a socket to connect to my machine Then send a line containing <student id> , <first-last name> , <nickname> , <email> Server will close connection immediately 21
Recommend
More recommend