Introduction to Network Programming Asst. Prof. Chaiporn Jaikaeo, - - PowerPoint PPT Presentation

introduction to network programming
SMART_READER_LITE
LIVE PREVIEW

Introduction to Network Programming Asst. Prof. Chaiporn Jaikaeo, - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

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

slide-2
SLIDE 2

2

Outline

 End-to-end communication

 Direct serial communication  Internet communication

 Simple client-server applications

slide-3
SLIDE 3

End-to-End Communication

 Direct communication

 Communicating devices are directly connected

 Internet communication

 Communication is performed over the Internet

3

TX TX RX RX GND GND

Internet

slide-4
SLIDE 4

4

Client-Server Paradigm

 Client

 Initiates communication  Issues command

 Server

 Awaits and respond to commands

 Most common model for Internet applications

Client Server

slide-5
SLIDE 5

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

slide-6
SLIDE 6

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 GND

USB- Serial Dongle USB- Serial Dongle

slide-7
SLIDE 7

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 Serial ser = Serial("/dev/ttyUSB0") ser.write('Hello\r\n') ser.readline() ser.read(100)

slide-8
SLIDE 8

8

Internet Comm. - App's Viewpoint

 Two network applications should interact as

if they were directly connected

Internet

A

App

B

App

write read

slide-9
SLIDE 9

9 9

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

slide-10
SLIDE 10

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

Internet

A

App

B

App

slide-11
SLIDE 11

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

Internet

A

App

B

App

slide-12
SLIDE 12

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

Internet

A B

Proc1 Proc2 Proc3 Proc4 Proc5

IP Address 1 IP Address 2

Ports

slide-13
SLIDE 13

Hands-on Activity 2: Internet Comm.

 Implement Activity 1 over the Internet using

stream-oriented service

slide-14
SLIDE 14

14

close() close() recv() send() connect() send() receive()

TCP Socket: Flow in Python

Client bind() listen() Server accept()

slide-15
SLIDE 15

15

Server: Creating Socket

 Create a socket object and bind it to port

12345 on any available network interface

from socket import * 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()

Listen on any interface Port 12345

slide-16
SLIDE 16

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)

slide-17
SLIDE 17

17

Finding Out Your IP Address

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

slide-18
SLIDE 18

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

slide-19
SLIDE 19

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

slide-20
SLIDE 20

20

Closing Connection

 Server:  Client:

sock.close() listen_sock.close() sock.close()

slide-21
SLIDE 21

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