[go: up one dir, main page]

0% found this document useful (0 votes)
73 views38 pages

Lec8 EEE13 2s1617

This document discusses TCP sockets and how to handle connections using TCP. It provides examples of building TCP servers and clients in Python. It explains how TCP establishes dedicated connections between clients and servers using sockets, and how this is different than UDP which does not have dedicated connections. It also discusses how to handle multiple simultaneous connections in TCP using threading or select() to handle multiple file descriptors asynchronously.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views38 pages

Lec8 EEE13 2s1617

This document discusses TCP sockets and how to handle connections using TCP. It provides examples of building TCP servers and clients in Python. It explains how TCP establishes dedicated connections between clients and servers using sockets, and how this is different than UDP which does not have dedicated connections. It also discusses how to handle multiple simultaneous connections in TCP using threading or select() to handle multiple file descriptors asynchronously.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

EEE 13 LECTURE 8

TCP SOCKETS
TCP TAGLINE

"IT JUST WORKS!"

Some random tech person trying to sell you


something
TCP States
Building Blocks of a TCP Server
Building Blocks of a TCP Client
TCP SERVER

LISTEN FOR INCOMING CONNECTIONS

s.listen(num_of_people_in_queue)

TCP

SOCKET

Image Taken From: http://www.pngall.com/wp-content/uploads/2016/04/Ear-PNG-HD.png


TCP SERVER

ACCEPT INCOMING CONNECTIONS

TCP

SOCKET
TCP SERVER

ACCEPT INCOMING CONNECTIONS

LISTENING

TCP

SOCKET

DEDICATED
 Dedicated CLIENT



TCP
 TCP

SOCKET Unlike UDP… SOCKET
and unlike your ex!
TCP SERVER

ACCEPT INCOMING CONNECTIONS

conn, addr = s.accept()

DEDICATED
 CLIENT

TCP
 TCP

SOCKET SOCKET
TCP SERVER

SAMPLE PYTHON CODE


import socket

TCP_IP = '127.0.0.1'
TCP_PORT = 5005
BUFFER_SIZE = 20 # Normally 1024, but we want fast response

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)

conn, addr = s.accept()


print 'Connection address:', addr
while 1:
data = conn.recv(BUFFER_SIZE)
if not data: break
print "received data:", data
conn.send(data) # echo
conn.close()
TCP SERVER

COMPARING WITH UDP SERVER


import socket

TCP_IP = '127.0.0.1'
TCP_PORT = 5005 import socket
BUFFER_SIZE = 20
s = socket.socket(socket.AF_INET, 
 UDP_IP = "127.0.0.1"

socket.SOCK_STREAM) UDP_PORT = 5005
s.bind((TCP_IP, TCP_PORT))
s.listen(1) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

sock.bind((UDP_IP, UDP_PORT))
conn, addr = s.accept()
print 'Connection address:', addr
while True:

while 1:
data, addr = sock.recvfrom(1024)

data = conn.recv(BUFFER_SIZE)
if not data: break print "received message:", data

print "received data:", data 

conn.send(data) sock.close()
conn.close()

s.close()

TCP UDP
TCP CLIENT

ESTABLISHING A DEDICATED CONNECTION

I WANT TO CONNECT
WITH YOU 192.168.11.1

CLIENT

TCP

SOCKET
TCP

SOCKET
TCP CLIENT

ESTABLISHING A DEDICATED CONNECTION

s.connect((TCP_IP, TCP_PORT)) I WANT TO


CONNECT WITH YOU
192.168.11.11 

PORT 6971

CLIENT

TCP

SOCKET
TCP

SOCKET
TCP CLIENT

SAMPLE CLIENT CODE


import socket

TCP_IP = '127.0.0.1'
TCP_PORT = 5005
BUFFER_SIZE = 1024
MESSAGE = "Hello, World!"

s = socket.socket(socket.AF_INET, 

socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(MESSAGE)
data = s.recv(BUFFER_SIZE)
s.close()

print "received data:", data


TCP CLIENT

COMPARING WITH UDP CLIENT


import socket
import socket

TCP_IP = '127.0.0.1' UDP_IP = "127.0.0.1"



TCP_PORT = 5005 UDP_PORT = 5005

BUFFER_SIZE = 1024 MESSAGE = "Hello, World!"
MESSAGE = "Hello, World!"
print "UDP target IP:", UDP_IP

print "UDP target port:", UDP_PORT

s = socket.socket(socket.AF_INET, 

print "message:", MESSAGE
socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT)) sock = socket.socket(socket.AF_INET, 

s.send(MESSAGE) socket.SOCK_DGRAM)
data = s.recv(BUFFER_SIZE)
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
s.close()

print "received data:", data

TCP UDP
MULTIPLE CONNECTIONS

HOW TO HANDLE MULTIPLE CONNECTIONS IN UDP?

> UDP handles multiple connections by default



> Remember, UDP does not establish a dedicated
connection

> UDP doesn’t care where it sends and receives, it just


does it
MULTIPLE CONNECTIONS

IN TCP, IT’S NOT THAT SIMPLE

> TCP has dedicated sockets for each client that connects
to it

> Handling multiple file descriptors will require


synchronization (Recall: Handling two pipes for
bidirectional communication between parent and child)
MULTIPLE CONNECTIONS

HANDLING MULTIPLE FILE DESCRIPTORS

Method 1: fork() or threads



> What if you want to maintain over 100 connections?

> Resource heavy!

Method 2: File descriptor multiplexing



> Handle file descriptors one at a time in an
asynchronous fashion (Recall: Signals)
> Not as "straightforward" as signals
MULTIPLE CONNECTIONS

SAMPLE CODE FOR A THREAD-BASED SERVER


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(10)

def clientthread(conn):
conn.send('Welcome to the server. Type something and hit enter\n') #send only takes string
while True:
data = conn.recv(1024)
reply = 'OK...' + data
if not data:
break
conn.sendall(reply)
conn.close()
while 1:
conn, addr = s.accept()
print 'Connected with ' + addr[0] + ':' + str(addr[1])
start_new_thread(clientthread ,(conn,))
s.close()
MULTIPLE CONNECTIONS

SELECT()
CLIENT 4

CLIENT 3
REACTOR
 CLIENT 1
LOOP

CLIENT 2
MULTIPLE CONNECTIONS

SAMPLE CODE FOR A SELECT-BASED SERVER


CONNECTION_LIST = []
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("0.0.0.0", PORT))
server_socket.listen(10)
CONNECTION_LIST.append(server_socket)
while 1:
read_sockets,write_sockets,error_sockets = select.select(CONNECTION_LIST,[],[])
for sock in read_sockets:
if sock == server_socket:
sockfd, addr = server_socket.accept()
CONNECTION_LIST.append(sockfd)
print "Client (%s, %s) connected" % addr
else:
try:
data = sock.recv(RECV_BUFFER)
if data:
sock.send('OK ... ' + data)
except:
broadcast_data(sock, "Client (%s, %s) is offline" % addr)
print "Client (%s, %s) is offline" % addr
sock.close()
CONNECTION_LIST.remove(sock)
continue
server_socket.close()
MULTIPLE CONNECTIONS

WHAT CAN’T TCP HANDLE?

> TCP is inefficient for Internet of Things

> TCP is inefficient for streaming on intermittent


connections
QUESTIONS?
EXAM 1 ON FEB 2X, 6-8PM

EARLY EXAM 4:30-6:30PM

ROOM IS LC2
SEE YOU NEXT
WEEK :D

You might also like