def receive(): # Establish connection global buffer global bufferStatus global maxLen global gCounter sockRec = co.createSocket(co.portReceiverSend) c, addr = co.allowConn(sockRec) sockSend = co.createConn(co.portReceiverReceive) print('Connected to channel') # Connection established rn = 0 while True: # Wait till frame received print(15 * '-') frame = sockSend.recv(1024).decode() print('Frame received ' + frame) if (frame != '#' and not isValid( frame, rn)): # wrong frame no received send ack for prev print('Invalid frame.Not sending ack') elif (frame != '#'): ackno = frame[0:3] ackNo = int(ackno, 2) buffer[ackNo] = frame[3:7] bufferStatus[ackNo] = gCounter # Send an acknowledgement ack = co.generateAck_gbn(ackno) time.sleep(5) # Send the ack print('Sending ack ' + ack) co.send_frame(ack, c) print("DEBUG:", sum(bufferStatus), maxLen * gCounter) if (sum(bufferStatus) >= maxLen * gCounter): file = open("output.txt", "a") print("".join(buffer), end='', file=file) file.close() rn = (rn + 1) % (co.window_size + 1) buffer = co.window_size * [''] gCounter = gCounter + 1 else: # For valid frame maxLen = maxLen - 1 ack = '#' time.sleep(5) # Send the ack print('Sending ack ' + ack) co.send_frame(ack, c) print(str(bufferStatus)) print(str(buffer)) print(15 * '-') # Close the sockets sockSend.close() sockRec.close()
def receive(): # Establish connection sockRec = co.createSocket(co.portReceiverSend) c, addr = co.allowConn(sockRec) sockSend = co.createConn(co.portReceiverReceive) print('Connected to channel') # Connection established rn = 0 while True: # Wait till frame received print(15 * '-') frame = sockSend.recv(1024).decode() print('Frame received ' + frame) if (frame == '#'): ack = '#' else: if (isValid(frame, rn) == 0): # wrong frame no received send ack for prev print('Invalid frame') print('Sending ack for previous frame') ackno = (rn) % 2 elif (isValid(frame, rn) == 1): print('Error in frame') continue else: # Valid frame ackno = (rn + 1) % 2 # For valid data frame rn = (rn + 1) % 2 # Send an acknowledgement ack = co.generateAck(ackno) # Send the ack print('Sending ack ' + ack) co.send_frame(ack, c) if (frame == '#'): # Means end frame break print(15 * '-') # Close the sockets sockSend.close() sockRec.close()
def send_all(list_of_frames): sockSend = co.createSocket( co.portSenderSend) # sender socket to send data to channel c, addr = co.allowConn(sockSend) sockRec = co.createConn( co.portSenderReceive) # Socket to receive data from channel sockRec.settimeout(timeoutTime) print('Connected to channel') # implementing stop and wait arq sn = 0 i = 0 while (i < (len(list_of_frames))): # While there are frames send print(15 * '-') canSend = True sn = (i) % 2 stored_frame = list_of_frames[i] if (stored_frame != '#'): stored_frame = co.prepare_frame(list_of_frames[i], sn) print('Sending frame ' + str(i) + ' ' + stored_frame) co.send_frame(stored_frame, c) canSend = False try: ack = sockRec.recv(1024).decode() except Exception as e: # Resend so repeat this iteration of loop print('Timeout.. Resending') continue if (ack == '#'): break print('Ack received ' + ack) if (ack and isValid(ack, sn)): # Wrong acknowledgement print('Correct ack received') canSend = True i = i + 1 elif (not isValid(ack, sn)): # invalid ack so resend print('Wrong ack.. resending') print(15 * '-') # Close the sockets sockSend.close() sockRec.close()
import common as co import socket # Socket to send data to channel s_rec = co.createConn(co.port_recsend) print('Connected to channel') # Function to receive a frame def receive_frame(): while (True): # Receive the frame frame = s_rec.recv(1024).decode() print('Frame received ' + frame) receive_frame()
import common as co import time import socket import random import time import threading isBusyChannel = 0 # Socket to send data to channel sockSend = co.createConn(co.portSenderReceive) time.sleep(1) # Socket to send data to channel sockSignal = co.createConn(co.portSenderSignal) print('Connected to channel') timeSlot = 2 # Function to send frames def send_frame(list_of_frames): i = 0 while (True): # Sense the channel and check if flag is 1 then dont send time.sleep(2) if (isBusyChannel == 0): # Channel is free print('Sending frame ' + str(i)) co.send_frame(list_of_frames[i], sockSend)
# This is the channel process # First create the required sockets import common as co import random import time import threading probas = 10 randSendF = 2 # Random probability of sending frame or not randSendAck = 2 randErrF = -1 randErrAck = -1 # **************** SENDER ***************************** sockSenderReceive = co.createConn( co.portSenderSend) # Socket to receive data from sender sockSenderSend = co.createSocket( co.portSenderReceive) # Socket to send data to sender senderSend, addr = co.allowConn(sockSenderSend) print('Channel connected to sender') #******************************************************* #****************** RECEIVER ************************** sockReceiverReceive = co.createConn( co.portReceiverSend) # Socket to receive data from receiver # sockReceiverReceive.settimeout(11) sockReceiverSend = co.createSocket( co.portReceiverReceive) # Socket to send data to receiver receiverSend, addr = co.allowConn(sockReceiverSend)
import common as co import time import socket import random import time import threading isBusyChannel = 0 sockSend = co.createConn( co.portSenderReceive) # Socket to send data to channel time.sleep(1) sockSignal = co.createConn( co.portSenderSignal) # Socket to send data to channel print('Connected to channel') probab = 10 p = 4 timeSlot = 2 # Function to send frames def send_frame(list_of_frames): i = 0 while (True): # Sense the channel and check if flag is 1 then dont send if (isBusyChannel == 0): # Channel is free # Send the frame with a probability p pr = random.randint(0, probab)
elif(args[i].lower()=='getother'): if(i==len(args)-2): # Error case return 0,req else: req.append({'method':'getother','key':args[i+2],'username':args[i+1]}) i=i+2 elif(args[i].lower()=='upgrade'): req.append({'method':'upgrade'}) else: return 0,req i=i+1 return 1,req sockClient=co.createConn(port=int(sys.argv[2]),ip=sys.argv[1]) uname=input('Enter a username: '******'Usage:') print('get key : To get value corresponding to a key') print('put key value : To insert a value corresponding to a key') print('upgrade : To upgrade user status') print('getother username key : To get value of another user (only allowed if manager)') while(True): # Take input request=input('>> ') if(request.lower()=='exit'): break
receiveThread.join() # Close the sockets sockSend.close() sockRec.close() timeoutTime = 17 frame_size = 4 sw = co.window_size sf = 0 sn = 0 # stored_buffer=[ '' for i in range(sw)] stored_buffer = {i: '' for i in range(sw)} print('Demonstrating Go Back N ARQ') list_of_frames = co.readfile('input.txt', frame_size) print(list_of_frames) list_of_frames.append('#') # Attach a blank frame sockSend = co.createSocket( co.portSenderSend) # sender socket to send data to channel sender, addr = co.allowConn(sockSend) sockRec = co.createConn( co.portSenderReceive) # Socket to receive data from channel sockRec.settimeout(timeoutTime) print('Connected to channel') send_all(list_of_frames)
import common as co import time import socket import random import time import threading busy_channel = 0 # Socket to send data to channel s_send = co.createConn(co.port_sendrec) time.sleep(1) # Socket to send data to channel s_signal = co.createConn(co.port_sendsignal) print('Connected to channel') p_prob = 10 p = 4 timeSlot = 2 # Function to send frames def send_frame(frames_list): i = 0 while (True): # Sense the channel and check if flag is 1 then dont send if (busy_channel == 0): # Channel is free
import common as co import socket sockReceive=co.createConn(co.portReceiverSend) # Socket to send data to channel print('Connected to channel') # Function to receive a frame def receive_frame(): while(True): # Receive the frame frame=sockReceive.recv(1024).decode() print('Frame received '+frame) receive_frame()