Networking Fundamentals - Project 1 Socket Programming
while True:
loops forever
for i in range(0, len(outputdata)): connectionSocket.send(outputdata[i].encode()) connectionSocket.send("\r\n".encode())
used to output all the data in the file all the content of the request file to client
TCP connection
a connection-oriented reliable protocol that uses a 3-way handshake
message = connectionSocket.recv(1024)
client program drops bytes in the string message into the TCP connection, client wait to receive bytes from server -> when characters arrives from the server they are placed in the string message
serverSocket.bind(('', serverPort))
used to associate the server port number, serverPort, with the socket Server socket = welcoming socket (door)
connectionSocket.close()
used to close this socket for the client
serverSocket = socket(AF_INET, SOCK_STREAM)
used to create a TCP socket
from socket import *
used to import socket module
f = open(filename[1:])
used to open the file
outputdata = f.read()
used to output the data and read the contents
filename = message.split()[1]
used to read the filename
connectionSocket.send('\nHTTP/1.1 200 OK\n\n')
used to send an 200 OK header line into the socket
except IOError: connectionSocket.send('\nHTTP/1.1 404 Not Found\n\n') print '404 Not Found'
used to send an error message to the page for a file that doesn't exist at the server
serverPort = 80
used to set the port for the server, browser will assume port 80 if port number is omitted
serverSocket.listen(1)
used to tell socket to wait and listen for client to "knock" on the door (request) -> parameter specifies max number of queued connections (1)
import sys
used to terminate the program
serverSocket.close() sys.exit()
used to terminate the program after sending the corresponding data
connectionSocket, addr = serverSocket.accept()
when client "knocks" on door, program invokes the accept() method for server socket -> creating a new socket in the server called connectionSocket dedicated to the particular client -> client and server complete handshaking creating TCP connection b/w client's clientSocket and server's connectionSocket