View
228
Download
0
Category
Tags:
Preview:
Citation preview
Introduction to Network Programming
Asst. Prof. Chaiporn Jaikaeo, Ph.D.chaiporn.j@ku.ac.th
http://www.cpe.ku.ac.th/~cpjComputer Engineering Department
Kasetsart University, Bangkok, Thailand
2
Outline End-to-end communication
Direct serial communication Internet communication
Simple client-server applications
End-to-End Communication
Direct communication Communicating devices are directly connected
Internet communication Communication is performed over the Internet
3
TX TXRX RXGND GND
Internet
4
Client-Server Paradigm
Client Initiates communication Issues command
Server Awaits and respond to commands
Most common model for Internet applications
Client Server
Our Simple Protocol
Request Issued by Client Response by Server
id\r\n Your student ID, followed by \r\n
name\r\n Your name, followed by \r\n
5
Server responds with "ERROR" when receiving an unknown command.
Client Server
command
response
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
6
TX TX
RX RX
GND GNDUSB-SerialDongle
USB-SerialDongle
Python's Serial API Opening a serial port
Sending data with newline characters
Receiving data (blocking call) Wait until new line characters are received and return the
whole line
Wait until 100 bytes are received
7
from serial import Serialser = Serial("/dev/ttyUSB0")
ser.write('Hello\r\n')
ser.readline()
ser.read(100)
8
Internet Comm. - App's Viewpoint
Two network applications should interact as if they were directly connected
InternetA
App
B
App
write read
99
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
10
Services Provided by Socket API
Connection-oriented, stream-like service Provides virtual stream-oriented pipe Data transfer is reliable
No loss, in-order arrival
Both machines use Transmission Control Protocol (TCP) to transfer data
InternetA
App
B
App
11
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
Both machines use User-Datagram Protocol (UDP) to transfer data
InternetA
App
B
App
12
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
InternetA B
Proc1 Proc2 Proc3Proc4
Proc5
IP Address 1 IP Address 2
Ports
Hands-on Activity 2: Internet Comm.
Implement Activity 1 over the Internet using stream-oriented service
14
close() close()
recv()
send()
connect()
send()
receive()
TCP Socket: Flow in Python
Client
bind()listen()
Server
accept()
15
Server: Creating Socket Create a socket object and bind it to
port 12345 on any available network interfacefrom socket import *
listen_sock = socket()listen_sock.bind(('0.0.0.0',12345))listen_sock.listen(1) # max. of 1 client can waitsock,info = listen_sock.accept()
Listen on any
interfacePort 12345
16
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)
17
Finding Out Your IP Address
Windows – Run ipconfig from a command prompt MacOS/Linux/Unix – Run ifconfig from a terminal
18
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
19
Transferring Data Use send() and recv() methods to
send and receive data, respectively E.g.,
At the server, enter the command
The server will block until it receives data
At the client, enter the command
sock.send('hello')
msg = sock.recv(1024)
specify the max number
of bytes to be received
20
Closing Connection Server:
Client:
sock.close()listen_sock.close()
sock.close()
21
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
Recommended