CIS 345 Socket Quiz
gethostname ( )
returns IP name of the host computer running the Python interpreter; Raises exception if computer does not have an IP address returns computer name to get hostName
gethostbyname(hostName)
returns the IP number of the computer whoese IP name is ipName. raises exception if ipName cannot be found returns IP address
listen ()
s.listen ( ) enables the server to accept connections; makes a listening socket. - also specifies the number of unaccepted connections that the system will allow before refusing new connections
PORT
should be an integer from 1- 65,535 if less than 1024, might require superuser priveleges
SOCK_STREAM
socket type for TCP, protocol used to transport messages in the network
What happens when you create a socket object and specify socket type
socket.socket() socket.SOCK_STREAM default protocol(TCP) is called
sockets
used to send messages across a network; provide a form of inter-process communication(IPC)
blocking method
when you call method, code is blocked; doesn't return until it receives a connect request from a client; infinite loop and won't return until something happens. receive and accept are blocking codes
Server ; to set up a listening socket
1. Socket 2. Bind 3. Listen 4. Accept
Server full
1. Socket 2.Bind 3.Listen 4. Accept 5. Receive 6.Send 7.Receive 8. Close extra receive bc client sends closing message
Client
1. socket 2.connect (to establish connection to server) 3. send 4. Receive 5. Close
computers referenced by
IP address and host name
IP address
IPv4 is 4 numbers from 0-255 separated by periods
AF_INET
Internet address family for IPv4
Diagram
NIC has two connection ports: ETH0 & ETH1 ETH0 connects to outside of computer Connect to router Router receives messages in packets and connect to modem modem connects to ISP socket is a pipeline and clients and servers connect to it
TCP
Transmission Control Protocol
after getting client socket object from accept:
an infinite while loop is used to loop over blocking calls to conn.recv(). This reads whatever data the client sends and echoes it back using conn.sendall(). while True: data = conn.recv(1024) if not data: break conn.sendall(data)
Client server applications
applications or processes that can run locally on a single computer or remotely across a network of computers. need: IP Addresses Sockets Threads
Bind
bind is used to associate the socket with a specific network interface (HOST) and port number (PORT); bind depends on address family of socket (AF_INET) s.bind(HOST,PORT) or s.bind(ADDR)
accept ( )
conn, addr = s.accept( ) with conn: print('Connected by', addr) blocks and waits for an incoming connection and when a client connects, it returns a new socket object representing the connection and a tuple holding the address of the client.
socket
connection to the server to communicate between client and server; multiple clients can connect to a server
Client side summary
creates a socket object, connects to server, calls sendall to send its message and recv( ) to read server reply and print
Client
from socket import * HOST='10.153.76.226' #HOST ='localhost' PORT=65000 ADDR=(HOST, PORT) sock= socket(AF_INET,SOCK_STREAM) sock.connect(ADDR) sock.sendall(b' Hello world') data = s.recv(1024) print( 'Received', repr(data) )
server code
from socket import * HOST = 'localhost' PORT = 65000 ADDR = (HOST, PORT) s = socket(AF_INET, SOCK_STREAM) s.bind(ADDR) s.listen (5) conn,addr = s. accept( ) with conn: print ('Connected by', addr) while True: data = conn.recv(1024) if not data: break conn.sendall(data)
Creating a socket object
import socket socket.socket( socket.AF_INET, socket.SOCK_STREAM) as s or from socket import * s = socket(AF_INET, SOCK_STREAM)
Listening socket
listens to connections from clients; when client connects, the server calls accept() to accept the connection
127.0.0.1
loopback address; message you send does not get broadcasted, does not leave your machine. goes to NIC card and loops back to be sent to destination
NIC
network interface card
Port
pipeline into computer; door into house. which room to go in? IP address gets u to a computer port number is where the computer is supposed to communicate through need the port that the server is listening to
Why TCP
reliable- packets dropped in network are detected and retransmitted by sender in order data delivery- data is read by application in order it was written by sender don't have to worry about packet loss, out of order data