21
Introduction to Network Programming Asst. Prof. Chaiporn Jaikaeo, Ph.D. [email protected] http://www.cpe.ku.ac.th/~cpj Computer Engineering Department Kasetsart University, Bangkok, Thailand

Introduction to Network Programming Asst. Prof. Chaiporn Jaikaeo, Ph.D. [email protected] cpj Computer Engineering Department

Embed Size (px)

Citation preview

Page 1: Introduction to Network Programming Asst. Prof. Chaiporn Jaikaeo, Ph.D. chaiporn.j@ku.ac.th cpj Computer Engineering Department

Introduction to Network Programming

Asst. Prof. Chaiporn Jaikaeo, [email protected]

http://www.cpe.ku.ac.th/~cpjComputer Engineering Department

Kasetsart University, Bangkok, Thailand

Page 2: Introduction to Network Programming Asst. Prof. Chaiporn Jaikaeo, Ph.D. chaiporn.j@ku.ac.th cpj Computer Engineering Department

2

Outline End-to-end communication

Direct serial communication Internet communication

Simple client-server applications

Page 3: Introduction to Network Programming Asst. Prof. Chaiporn Jaikaeo, Ph.D. chaiporn.j@ku.ac.th cpj Computer Engineering Department

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

Page 4: Introduction to Network Programming Asst. Prof. Chaiporn Jaikaeo, Ph.D. chaiporn.j@ku.ac.th cpj Computer Engineering Department

4

Client-Server Paradigm

Client Initiates communication Issues command

Server Awaits and respond to commands

Most common model for Internet applications

Client Server

Page 5: Introduction to Network Programming Asst. Prof. Chaiporn Jaikaeo, Ph.D. chaiporn.j@ku.ac.th cpj Computer Engineering Department

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

Page 6: Introduction to Network Programming Asst. Prof. Chaiporn Jaikaeo, Ph.D. chaiporn.j@ku.ac.th cpj Computer Engineering Department

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

Page 7: Introduction to Network Programming Asst. Prof. Chaiporn Jaikaeo, Ph.D. chaiporn.j@ku.ac.th cpj Computer Engineering Department

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)

Page 8: Introduction to Network Programming Asst. Prof. Chaiporn Jaikaeo, Ph.D. chaiporn.j@ku.ac.th cpj Computer Engineering Department

8

Internet Comm. - App's Viewpoint

Two network applications should interact as if they were directly connected

InternetA

App

B

App

write read

Page 9: Introduction to Network Programming Asst. Prof. Chaiporn Jaikaeo, Ph.D. chaiporn.j@ku.ac.th cpj Computer Engineering Department

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

Page 10: Introduction to Network Programming Asst. Prof. Chaiporn Jaikaeo, Ph.D. chaiporn.j@ku.ac.th cpj Computer Engineering Department

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

Page 11: Introduction to Network Programming Asst. Prof. Chaiporn Jaikaeo, Ph.D. chaiporn.j@ku.ac.th cpj Computer Engineering Department

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

Page 12: Introduction to Network Programming Asst. Prof. Chaiporn Jaikaeo, Ph.D. chaiporn.j@ku.ac.th cpj Computer Engineering Department

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

Page 13: Introduction to Network Programming Asst. Prof. Chaiporn Jaikaeo, Ph.D. chaiporn.j@ku.ac.th cpj Computer Engineering Department

Hands-on Activity 2: Internet Comm.

Implement Activity 1 over the Internet using stream-oriented service

Page 14: Introduction to Network Programming Asst. Prof. Chaiporn Jaikaeo, Ph.D. chaiporn.j@ku.ac.th cpj Computer Engineering Department

14

close() close()

recv()

send()

connect()

send()

receive()

TCP Socket: Flow in Python

Client

bind()listen()

Server

accept()

Page 15: Introduction to Network Programming Asst. Prof. Chaiporn Jaikaeo, Ph.D. chaiporn.j@ku.ac.th cpj Computer Engineering Department

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

Page 16: Introduction to Network Programming Asst. Prof. Chaiporn Jaikaeo, Ph.D. chaiporn.j@ku.ac.th cpj Computer Engineering Department

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)

Page 17: Introduction to Network Programming Asst. Prof. Chaiporn Jaikaeo, Ph.D. chaiporn.j@ku.ac.th cpj Computer Engineering Department

17

Finding Out Your IP Address

Windows – Run ipconfig from a command prompt MacOS/Linux/Unix – Run ifconfig from a terminal

Page 18: Introduction to Network Programming Asst. Prof. Chaiporn Jaikaeo, Ph.D. chaiporn.j@ku.ac.th cpj Computer Engineering Department

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

Page 19: Introduction to Network Programming Asst. Prof. Chaiporn Jaikaeo, Ph.D. chaiporn.j@ku.ac.th cpj Computer Engineering Department

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

Page 20: Introduction to Network Programming Asst. Prof. Chaiporn Jaikaeo, Ph.D. chaiporn.j@ku.ac.th cpj Computer Engineering Department

20

Closing Connection Server:

Client:

sock.close()listen_sock.close()

sock.close()

Page 21: Introduction to Network Programming Asst. Prof. Chaiporn Jaikaeo, Ph.D. chaiporn.j@ku.ac.th cpj Computer Engineering Department

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