Client & Server Sockets & Exceptions Flashcards
This demonstrates that, from an OS perspective, different either a client or a server.
NOPE once a socket is created it is neutral, can be used for both
The host part of the server address can be either
hostname, ip or empty string
‘127.0.0.1’ or ‘localhost’ loopback address
if hostname it used DNS but non-deterministic and may return IPv4 or IPv6
empty string a server can accept IPv4 connections on all available interfaces
Port numbers range
0 - 65535
0-1023 well-known
1024 - 49151 registered
49152 - 65535 dynamic, private and ephemeral
when socket bound to address, cannot be changed (cannot bind twice)
when closed the binding is released but not immediately (TCP WAIT_TIME)
s = socket(AF_INET,SOCK_STREAM)
print(s)
s.bind((SERVER_HOST,SERVER_PORT))
print(s)
after binding we get the laddr
What about binding on the client side?
happens implicitly (to local address with ephemeral port)
how to know client port on client side vs. server port?
cs.getsockname()
cs.getpeername()
If you try to call the listen
method without binding
it will raise an OSError Exception.
regular sockets vs. listening sockets
server client
vs
server
which function accepts the optional backlog parameter
what does it do
listen()
determines the how many connections can be pending before refusing other connections
1 - socket.SOMAXCONN
128 linus
5-200 windows
On Windows, to verify that a socket is listening on a specific port, use the following
command on cmd:
netstat -aon
What method puts the program into blocking mode?
accept()
returns connection (regular socket for send\recv) and address
examples of gaierror exceptions
getaddrinfo(“ “, 80)
gethostbyname(‘gooogggle.com’)
gethostbyaddr(‘127.412.14.10’)
or anything with a weird ip
connect((‘hello’, 70000))
bind((‘hellobitch’, 80000))
what functions could cause timeoutError
at client
connect()
create_connection ()
recv()
at server
accept()
recv()
s = socket()
s.bind((‘localhost’,800000))
overflowError
s.send(‘hello’)
TypeError
in_msg = s.recv(-5)
valueError
print(s.getsockname()[3])
index error
s.receivall(1024)
attributeError