Top Banner
Socket Programming
31

Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

Apr 02, 2015

Download

Documents

Eden Hoye
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

Socket Programming

Page 2: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

Basics

• Socket is an interface between application and network– Application creates a socket– Socket type dictates the style of communication

• Once socket is configured, applications– Pass data to the socket for network transmission– Receive data transmitted across the network from the

socket

Page 3: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

Server Program

Page 4: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server.bind((‘ ‘,50000))server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server:

client, address = server.accept() input.append(client)

else: data = s.recv(4096)

print dataserver.close()

Page 5: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server.bind((‘ ‘,50000))server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server:

client, address = server.accept() input.append(client)

else: data = s.recv(4096)

print dataserver.close()

Page 6: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server.bind((‘ ‘,50000))server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server:

client, address = server.accept() input.append(client)

else: data = s.recv(4096)

print dataserver.close()

Page 7: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

Creating Sockets

sockfd = socket(socket_family, socket_type)

socket_family: Network Layer Protocol• AF_INET – IPv4• AF_INET6 – IPv6socket_type: Transport Layer Protocol• SOCK_STREAM – TCP• SOCK_DGRAM – UDP

Page 8: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server.bind((‘ ‘,50000))server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server:

client, address = server.accept() input.append(client)

else: data = s.recv(4096)

print dataserver.close()

Page 9: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server.bind((‘ ‘,50000))server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server:

client, address = server.accept() input.append(client)

else: data = s.recv(4096)

print dataserver.close()

Page 10: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

Binding Sockets

sockfd.bind((host_address, port))

Binds the socket to particular address and port• ‘ ’ indicates “any interface” address

Why no bind called for client??

Page 11: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server.bind((‘ ‘,50000))server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server:

client, address = server.accept() input.append(client)

else: data = s.recv(4096)

print dataserver.close()

Page 12: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server.bind((‘ ‘,50000))server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server:

client, address = server.accept() input.append(client)

else: data = s.recv(4096)

print dataserver.close()

Page 13: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

Listen for Connections

sockfd.listen(backlog)

• Prepares socket to accept connections– backlog: number of pending connections allowed

• Allows sockets to respond to new connections using the three-way handshake

Page 14: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server.bind((‘ ‘,50000))server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server:

client, address = server.accept() input.append(client)

else: data = s.recv(4096)

print dataserver.close()

Page 15: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server.bind((‘ ‘,50000))server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server:

client, address = server.accept() input.append(client)

else: data = s.recv(4096)

print dataserver.close()

Page 16: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

Accept Connections

client, address = sockfd.accept()

• WAITS for a client to establish the connection– client: socket fd for handling the client connection– address: IP address of the client

Page 17: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server.bind((‘ ‘,50000))server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server:

client, address = server.accept() input.append(client)

else: data = s.recv(4096)

print dataserver.close()

Page 18: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server.bind((‘ ‘,50000))server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server:

client, address = server.accept() input.append(client)

else: data = s.recv(4096)

print dataserver.close()

Page 19: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

Receive Data

data = sockfd.recv(sz, [flags])

• WAITS for data on sockfd• Retrieves up to ‘sz’ bytes of data when available

• flags indicate property of recv function-– MSG_DONTWAIT: make recv non-blocking– MSG_PEEK: only peek data; don’t remove from buffer – And many more… (do man recv)

Page 20: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server.bind((‘ ‘,50000))server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server:

client, address = server.accept() input.append(client)

else: data = s.recv(4096)

print dataserver.close()

Page 21: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server.bind((‘ ‘,50000))server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server:

client, address = server.accept() input.append(client)

else: data = s.recv(4096)

print dataserver.close()

Page 22: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

Close Socket

sockfd.close()

Close the connection by sending FIN

Page 23: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server.bind((‘ ‘,50000))server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server:

client, address = server.accept() input.append(client)

else: data = s.recv(4096)

print dataserver.close()

Page 24: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

Wait for Input

• accept and recv are blocking• Server needs to handle multiple connections• Cannot proceed by blocking on every connection• Need a single function to wait for input on “any” socket fd

Page 25: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server.bind((‘ ‘,50000))server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server:

client, address = server.accept() input.append(client)

else: data = s.recv(4096)

print dataserver.close()

Page 26: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

Wait for Input

r,w,x = select(rlist, wlist, xlist, [timeout])

• rlist: list of file descriptor to wait on for reading• r: file descriptor ready for reading• wlist: list of file descriptor to wait on for writing• w: file descriptor ready for writing• xlist: list of file descriptor to wait on for exceptions• w: file descriptor ready for exception handling

• Waits on any fd in any of these lists until “timeout”

Page 27: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server.bind((‘ ‘,50000))server.listen(30) input = [server]while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server:

client, address = server.accept() input.append(client)

else: data = s.recv(4096)

print dataserver.close()

Add server fd to input list

Wait for read on any

socket fd in input

Handle read on server by

‘accept’Handle read on client by

‘recv’

Add new client fd in input list

Page 28: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

Client Program

Page 29: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

import socket import sys

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.connect((‘10.0.0.1’, 50000))s.send(“Hello Server!”)s.close()

Page 30: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

Summary

Page 31: Socket Programming. Basics Socket is an interface between application and network – Application creates a socket – Socket type dictates the style of communication.

1.create

1.create

2. bind

3. listen

4. accept

5. recv

2. connect

3. send

3-way handshake

data

Client Server